什么是 $variable = some_function(arg1, arg2) == 'string';意思是 php?
What does $variable = some_function(arg1, arg2) == 'string'; mean in php?
很抱歉,如果之前有人问过这个问题,但我不知道要搜索什么才能找到答案。
我假设它的意思是 $variable = returned value or string if function doesn't return a value 但我想澄清一下。
谢谢
这将检查函数 some_function()
返回的值是否等于值 string
(不要与 string 数据类型混淆)或不分配 true
或 false
给 $variable
,根据结果。
将语句分解可以最好地理解该语句:
some_function(arg1, arg2)
先执行函数,returns 一些值。请注意,即使函数不包含 return
语句,它也会 return 一个值,在这种情况下,值为 null
.
== 'string'
将值 returned 与字符串值 'string'
进行比较。请注意,PHP 的比较运算符可能执行 "type juggling",因此其他值(例如整数 0)被认为等于此字符串。这将为您提供 true
或 false
.
的结果
$variable =
比较的结果(不是函数调用)存储在$variable
。
所以在语句之后,如果函数 returned 的东西被认为等于 'string'
,$variable
将包含 true
,否则 false
。
<?php
$a = "Hello ";
$b = "World";
if ($result = some_function($a, $b) == "Hello World") {
echo "Yep it matched" . "<br/>";
// In True or false
if ($result == TRUE) {
echo "True matched";
}
}
else
{
echo "Nope, it doesn't matched" . "<br/>";
if ($result == FALSE) {
echo "False failed";
}
}
function some_function($a, $b)
{
$new = $a . $b;
return $new;
}
?>
输出:
Yep it matched
True matched
解释:
some_function($a, $b)
在if
条件下调用函数some_function($a, $b)
,returned值将保存在some_function($a, $b)
的地方。
- 因此,根据我们的示例
some_function($a, $b)
,if 语句中的 Hello World
是 return 从 some_function($a, $b)
函数编辑的(即 return $新).
现在,它检查语句
if ($result = some_function($a, $b) == "Hello World") {
我们的 some_function($a, $b
) 有 "Hello World" & 它与右侧的字符串 "Hello World" 进行比较。 (即这部分 == "Hello World")
- 如果匹配,
$result
将具有值 TRUE
并执行 if 块中的内容。
- 如果不匹配,
$result
将具有值 FALSE
,然后它转到 else
块并执行 else
块中的内容。
假设,如果
$a = "Good ";
$b = "Night ";
然后 $a & $b 在 some_function($a, $b)
中作为参数传递,这将 return Good Night
.
并且 returned 值(在本例中为 Good Night
)将存储在 if 条件中 some_function($a, $b)
的位置。
我们的 some_function($a, $b
) 有 Good Night
& 它与右侧的字符串 "Hello World" 进行比较。 (即这部分 == "Hello World")
如果匹配,$result 将被设置为 TRUE
如果没有设置为 FALSE
。
在这里,它将失败,因为它不匹配。所以,它将输出为,
Nope, it doesn't matched
False failed
如果匹配,则打印为
Yep it matched
True matched
很抱歉,如果之前有人问过这个问题,但我不知道要搜索什么才能找到答案。
我假设它的意思是 $variable = returned value or string if function doesn't return a value 但我想澄清一下。
谢谢
这将检查函数 some_function()
返回的值是否等于值 string
(不要与 string 数据类型混淆)或不分配 true
或 false
给 $variable
,根据结果。
将语句分解可以最好地理解该语句:
some_function(arg1, arg2)
先执行函数,returns 一些值。请注意,即使函数不包含 return
语句,它也会 return 一个值,在这种情况下,值为 null
.
== 'string'
将值 returned 与字符串值 'string'
进行比较。请注意,PHP 的比较运算符可能执行 "type juggling",因此其他值(例如整数 0)被认为等于此字符串。这将为您提供 true
或 false
.
$variable =
比较的结果(不是函数调用)存储在$variable
。
所以在语句之后,如果函数 returned 的东西被认为等于 'string'
,$variable
将包含 true
,否则 false
。
<?php
$a = "Hello ";
$b = "World";
if ($result = some_function($a, $b) == "Hello World") {
echo "Yep it matched" . "<br/>";
// In True or false
if ($result == TRUE) {
echo "True matched";
}
}
else
{
echo "Nope, it doesn't matched" . "<br/>";
if ($result == FALSE) {
echo "False failed";
}
}
function some_function($a, $b)
{
$new = $a . $b;
return $new;
}
?>
输出:
Yep it matched
True matched
解释:
some_function($a, $b)
在if
条件下调用函数some_function($a, $b)
,returned值将保存在some_function($a, $b)
的地方。- 因此,根据我们的示例
some_function($a, $b)
,if 语句中的Hello World
是 return 从some_function($a, $b)
函数编辑的(即 return $新). 现在,它检查语句
if ($result = some_function($a, $b) == "Hello World") {
我们的
some_function($a, $b
) 有 "Hello World" & 它与右侧的字符串 "Hello World" 进行比较。 (即这部分 == "Hello World")- 如果匹配,
$result
将具有值TRUE
并执行 if 块中的内容。 - 如果不匹配,
$result
将具有值FALSE
,然后它转到else
块并执行else
块中的内容。
假设,如果
$a = "Good ";
$b = "Night ";
然后 $a & $b 在 some_function($a, $b)
中作为参数传递,这将 return Good Night
.
并且 returned 值(在本例中为 Good Night
)将存储在 if 条件中 some_function($a, $b)
的位置。
我们的 some_function($a, $b
) 有 Good Night
& 它与右侧的字符串 "Hello World" 进行比较。 (即这部分 == "Hello World")
如果匹配,$result 将被设置为 TRUE
如果没有设置为 FALSE
。
在这里,它将失败,因为它不匹配。所以,它将输出为,
Nope, it doesn't matched
False failed
如果匹配,则打印为
Yep it matched
True matched