开发者

What does this PHP syntax mean and why is this variable NaN?

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").开发者_StackOverflowappend(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:

$(function(){
    function loadPosts() {
        $(".loading").show("fast");
        count += num;
        $.post("load.php", {'page': count}, function(data){
            $("#contents").append(data);
            $(".loading").hide("fast");
        });
    }
    var count;
    var num;
    $("#more").click(loadPosts);
    loadPosts();
    count = 0;
    num = 2;
});

As you can see, count += num will be set before var count = 0 and var num = 2 are run, since loadPosts() 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 undefined to undefined (since neither variable has been set yet), you get NaN: "not a number". To fix this, move the function and variable declarations to the top of the scope, before you call loadPosts:

$(function(){
    function loadPosts() {
        $(".loading").show("fast");
        count += num;
        $.post("load.php", {'page': count}, function(data){
            $("#contents").append(data);
            $(".loading").hide("fast");
        });
    }
    var count = 0;
    var num = 2;
    $("#more").click(loadPosts);
    loadPosts();
});


The ? operator is called the ternary operator, you can look that up for more about it. But you understood it correctly.

Basically the code means: If the variable was set, then use it, otherwise default it to 0. (In php strict mode you can not access an array index that does not exist, so this code avoids that error.)

NaN means not a number - but PHP doesn't use (Edit: rarely uses) that, it was probably sent from Javascript, most likely because you tried to divide a number by 0 (presumably in the context of calculating the page). I don't see anything in the code you posted that does that, but perhaps it happens somewhere else.


VARIABLE = (CONDITION) ? VALUE1 : VALUE2; is short for

if (CONDITION)
    VARIABLE = VALUE1;
else
    VARIABLE = VALUE2;

You can use a similar notation to abbreviate conventional if-else statements:

(CONDITION) ? FUNCTION1 : FUNCTION2; is short for

if (CONDITION)
    FUNCTION1;
else
    FUNCTION2;


isset($_POST['page']) ? $_POST['page'] : "0"; is a ternary if statement. The conditional is the part preceding the ?. The first part after the ? is the value to assign if the expression is true. The part after the : is the assignment resulting from a false value for the expression.


It is called ternary operator.
Your second question - $_POST['page'] and $page are not the same, if you want to check the variable, you need to test $_POST variable.


Your javascript is passing the NaN as a string to your PHP script. You could try this:

$page = isset($_POST['page']) && $_POST['page'] != "NaN" ? $_POST['page'] : "0";

Or you could alter your javascript to do the check that side:

count += num;
if (isNaN(count))
{
    count = 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜