Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我是否从根本上误解了PHP/C类编程语言的工作原理?_Php - Fatal编程技术网

我是否从根本上误解了PHP/C类编程语言的工作原理?

我是否从根本上误解了PHP/C类编程语言的工作原理?,php,Php,请看下面的代码: if ($the_string = a_function_call('someid123') && $another_string = another_function_call('someid123')) { // $the_string and $another_string are now both expected to be the values returned from their respective functions, and thi

请看下面的代码:

if ($the_string = a_function_call('someid123') && $another_string = another_function_call('someid123'))
{
    // $the_string and $another_string are now both expected to be the values returned from their respective functions, and this block of code is expected to be run if both of them are not "falsey".
}
然而

if ($the_string = a_function_call('someid123') && $another_string = another_function_call('someid123'))
{
    // $the_string is now actually a boolean true. Not sure about $another_string.
}

这是一只奇怪的虫子吗?还是预期的行为?我一直认为这就像第一个场景一样,我总是按照这个假设进行编码。

&
=
具有更高的优先级。因此,您的代码被解析为您编写的代码

if ($the_string = (a_function_call('someid123') && ($another_string = another_function_call('someid123'))))
这将执行以下步骤:

  • 调用函数调用()
  • 如果返回真实值,它将调用另一个函数调用(),并将结果分配给另一个字符串()
  • &&
    表达式的真值指定给
    $字符串
  • 测试
    if
    语句中的真值
  • 此优先级允许您编写如下代码:

    $success = function_1() && function_2();
    
    如果
    =
    具有更高的优先级,则仅从
    函数1()
    设置
    $success
    ,而不是组合

    添加括号可以解决问题:

    if (($the_string = a_function_call('someid123')) 
        && ($another_string = another_function_call('someid123')))
    
    PHP还有
    运算符,它们类似于
    &&
    |
    ,但它们的优先级低于赋值;它们的存在就是为了解决这个问题。您经常看到它们在这样的代码中使用:

    $variable = some_function(...) or die("some_function didn't work");
    

    因此,简单地用
    替换
    &&
    也可以解决问题。

    &&
    的优先级高于
    =
    。因此,您的代码被解析为您编写的代码

    if ($the_string = (a_function_call('someid123') && ($another_string = another_function_call('someid123'))))
    
    这将执行以下步骤:

  • 调用函数调用()
  • 如果返回真实值,它将调用另一个函数调用()
  • ,并将结果分配给另一个字符串()
  • &&
    表达式的真值指定给
    $字符串
  • 测试
    if
    语句中的真值
  • 此优先级允许您编写如下代码:

    $success = function_1() && function_2();
    
    如果
    =
    具有更高的优先级,则仅从
    函数1()
    设置
    $success
    ,而不是组合

    添加括号可以解决问题:

    if (($the_string = a_function_call('someid123')) 
        && ($another_string = another_function_call('someid123')))
    
    PHP还有
    运算符,它们类似于
    &&
    |
    ,但它们的优先级低于赋值;它们的存在就是为了解决这个问题。您经常看到它们在这样的代码中使用:

    $variable = some_function(...) or die("some_function didn't work");
    

    因此,简单地用
    替换
    &&
    也可以解决问题。

    关键是运算符优先级。 表达式的计算方法如下(请注意括号):

    $another_string
    另一个函数调用()
    中获取值,然后通过AND运算符根据
    函数调用()返回值进行检查

    添加正确的括号,如下所示,以获得预期结果:

    if (($the_string = a_function_call('someid123')) && ($another_string = another_function_call('someid123')))
    

    请检查。

    键是运算符优先级。 表达式的计算方法如下(请注意括号):

    $another_string
    另一个函数调用()
    中获取值,然后通过AND运算符根据
    函数调用()返回值进行检查

    添加正确的括号,如下所示,以获得预期结果:

    if (($the_string = a_function_call('someid123')) && ($another_string = another_function_call('someid123')))
    

    请检查。

    运算符
    和&
    将返回true或false(布尔值)。无法获取您的答案,它们看起来都一样您的意图是什么?要测试
    函数调用的返回值
    ?如果需要返回值,请将其包装在parenthesis@Viney区别在于注释,而不是代码。
    &&
    运算符将返回true或false(布尔值)。无法获取您的信息,它们看起来都一样您的意图是什么?要测试
    函数调用的返回值
    ?如果需要返回值,请将其包装在parenthesis@Viney区别在于注释,而不是代码。