question marks are hard to search for in google.
What does this mean step by step?
$page = isset($_POST['page'])?$_POST['page']:"0";
I am guessing it means if Post['page'] is set use that value and if not use 0? but i dont get it in detail. Also when I do var_dump($page) i get "NaN" ; even though POST['page'] is not set to anything yet. What’s going on?
–> For full disclosure, I am using this Javascript function to pass the value of page..for a “load more”/pagination functionality.
$(function(){
$("#more").click(loadPosts);
loadPosts();
var count = 0;
var num = 2;
function loadPosts() {
$(".loading").show("fast");
count += num;
$.post("load.php", {'page': count}, function(data){
$("#contents").append(data);
$(".loading").hide("fast");
});
}
});
The
?:syntax is called the ternary operator and does exactly what you think. If the first part (before the?) is truthy, the first option (before the:) is used; if not, the second option is.The error that you’re getting is because you haven’t set your number values yet. This is how the browser reads your code:
As you can see,
count += numwill be set beforevar count = 0andvar num = 2are run, sinceloadPosts()is run before they are set. This is because of something called “hoisting”, where function and variable declarations are moved to the very top of their containing scope.Because you’re trying to add
undefinedtoundefined(since neither variable has been set yet), you getNaN: “not a number”. To fix this, move the function and variable declarations to the top of the scope, before you callloadPosts: