简体   繁体   English

这个PHP语法是什么意思,为什么这个变量是NaN?

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

question marks are hard to search for in google. 问号很难在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 ? 我猜这意味着如果Post['page']设置为使用该值,如果不使用0 but i dont get it in detail. 但我没有详细介绍。 Also when I do var_dump($page) i get "NaN" ; 另外,当我做var_dump($page)我得到"NaN" even though POST['page'] is not set to anything yet. 即使POST['page']尚未设置为任何值。 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. ->为了进行全面披露,我正在使用此Javascript函数传递page ..的值以实现“更多加载” /分页功能。

$(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 ? ? 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.) 基本上,代码意味着:如果设置了变量,则使用该变量,否则将其默认设置为0。(在php严格模式下,您无法访问不存在的数组索引,因此该代码避免了该错误。)

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). NaN的意思不是数字-但PHP不使用(编辑:很少使用)它,它可能是从Javascript发送的,最有可能是因为您试图将数字除以0(大概是在计算页面的上下文中)。 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: 您可以使用类似的符号来缩写常规的if-else语句:

(CONDITION) ? FUNCTION1 : FUNCTION2; is short for 是短的

if (CONDITION)
    FUNCTION1;
else
    FUNCTION2;

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. 如您所见,因为将在设置它们之前运行loadPosts() ,所以loadPosts()在运行var count = 0var num = 2 之前设置count += num 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". 因为您试图将undefined添加到undefined (因为尚未设置任何变量),所以您会得到NaN :“ not a number”。 To fix this, move the function and variable declarations to the top of the scope, before you call loadPosts : 要解决此问题,请在调用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();
});

isset($_POST['page']) ? $_POST['page'] : "0"; is a ternary if statement. 是三元if语句。 The conditional is the part preceding the ? 条件是?之前的部分 . The first part after the ? 第一部分之后? is the value to assign if the expression is true. 是表达式为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. 第二个问题- $_POST['page']$page不同,如果要检查变量,则需要测试$ _POST变量。

Your javascript is passing the NaN as a string to your PHP script. 您的JavaScript正在将NaN作为字符串传递给PHP脚本。 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: 或者您可以更改您的JavaScript来进行检查:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM