简体   繁体   English

我可以在 PHP if 条件中定义一个变量吗?

[英]Can I define a variable in a PHP if condition?

For example, can I do:例如,我可以这样做:

if ($my_array = wp_get_category($id)) {
    echo "asdf";
} else {
    echo "1234";
}

If nothing is returned by the function, I want to go into the else statement.如果function什么都没有返回,我想把go放到else语句中。

Yes, that will work, and the pattern is used quite often.是的,这行得通,而且这种模式经常被使用。

If $my_array is assigned a truthy value, then the condition will be met.如果$my_array被分配了一个值,那么条件将被满足。

CodePad .键盘

<?php

function wp_get_category($id) {
   return 'I am truthy!';
}

if ($my_array = wp_get_category($id)) {
    echo $my_array;
} else {
    echo "1234";
}

The inverse is also true...反之亦然……

If nothing is returned by the function, I want to go into the else statement.如果 function 没有返回任何内容,我想将 go 放入 else 语句中。

A function that doesn't return anything will return NULL , which is falsey .不返回任何内容的 function 将返回NULL ,这是错误的。

CodePad .键盘

<?php

function wp_get_category($id) {
}

if ($my_array = wp_get_category($id)) {
    echo $my_array;
} else {
    echo "1234";
}

This is in fact a common pattern and will work.这实际上是一种常见的模式,并且会起作用。 However, you may want to think twice about using it for more complex cases, or at all.但是,您可能需要三思而后行,将其用于更复杂的情况,或者根本不使用。 Imagine if someone maintaining your code comes along and sees想象一下,如果有人维护您的代码并看到

if ($x = one() || $y = two() && $z = three() or four()) {

}

It might be better to declare the variables before using them in the conditional.在条件中使用它们之前声明变量可能会更好。

you might want something like this:你可能想要这样的东西:

if (!is_null($my_array = wp_get_category($id)) {
    echo "asdf";
else
    echo "1234";

Assuming the function returns null upon failure.假设 function 在失败时返回 null。 You may have to adjust it a bit.你可能需要稍微调整一下。

I found this wondering about the rules of declaring a variable then using it immediately in subsequent conditions in the same statement.我发现这对声明变量然后在同一语句的后续条件中立即使用它的规则感到疑惑。

Thanks to previous answer for the codepad link, I made my own to test the theory.感谢先前对键盘链接的回答,我自己做了一个测试理论。 Spoiler alert: It works.剧透警报:它有效。

http://codepad.org/xTwzTwGR http://codepad.org/xTwzTwGR

Following is one more alternative to define any variable (with safety):以下是定义任何变量(安全)的另一种选择:

$my_array = ($my_array = $wp_get_category($id)) ?: /* else statement here */;

I always found this principle confusing as it never seemed to work for me: Take the following code:我总是发现这个原则令人困惑,因为它似乎对我来说从来没有用过:采用以下代码:

if ($pid = $arr['Key']){

This may throw an error Undefined index: Key .这可能会引发错误Undefined index: Key Equally I get the same result with this:同样,我得到了相同的结果:

if (!empty($pid = $arr['Key']))

The solution now with PHP7+ is as follows:现在用PHP7+的解决方法如下:

if ($pid = $arr['Key']?? false)

Which will allow for an array with an empty value setting $pid = false;这将允许具有空值设置的数组$pid = false; and not triggering the IF statement.并且不触发 IF 语句。

I hope that helps someone as arrays threw me but the double coalesc is super helpful and can be used in the if concept.我希望这能帮助别人,因为 arrays 把我扔了,但是双合并非常有用,可以在if概念中使用。

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

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