在 PHP 中声明多个变量的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11025250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Proper way to declare multiple vars in PHP
提问by Tom Biakryek
I've been coding personal scripts for years in PHP and get used to turn off Error display. I'm about to release some of these scripts and would like to do it the proper way.
我多年来一直在用 PHP 编写个人脚本,并习惯于关闭错误显示。我即将发布其中一些脚本,并希望以正确的方式进行。
The only reason why I turn off error display is to avoid having to test every single var, prior using it, thanks to isset().
我关闭错误显示的唯一原因是为了避免在使用之前测试每个变量,这要归功于 isset()。
So, here is my question:
所以,这是我的问题:
Is there a better way to declare multiple vars than this ?
有没有比这更好的方法来声明多个变量?
<?php
// at the begining of my main file
if (!isset($foo)) ($foo = '');
if (!isset($bar)) ($bar = '');
if (!isset($ping)) ($ping = '');
if (!isset($pong)) ($pong = '');
// etc. for every single var
?>
Something like this for instance :
例如这样的事情:
<?php
var $foo, $bar, $ping, $pong;
?>
回答by DaneSoul
<?php
$foo = $bar = $ping = $pong = '';
?>
If it's your script and you know which variables where you use, why you want spend resourses to check if the variable was declared befor?
如果它是您的脚本并且您知道您使用了哪些变量,那么您为什么要花费资源来检查该变量是否是在之前声明的?
回答by pocketfullofcheese
I posted this in a comment earlier, but someone suggested I submit it as an answer.
我之前在评论中发布了这个,但有人建议我将其作为答案提交。
The shortest and simplest way I can think of, is to do:
我能想到的最短和最简单的方法是:
$foo = $bar = $ping = $pong = '';
I often prefer to set things to false, instead of an empty string, so that you can always do checks in the future with === false, but that is just a preference and depends on how you are using these variables and for what.
我通常更喜欢将事物设置为 false,而不是空字符串,以便您将来始终可以使用 === false 进行检查,但这只是一种偏好,取决于您如何使用这些变量以及用于什么。
回答by powtac
Your if()with isset()attempt is the proper way of doing that!
你if()的isset()尝试是这样做的正确方法!
But you can write it a little bit shorter/more readable, using the Ternary Operator:
但是您可以使用三元运算符将其编写得更短/更具可读性:
$foo = isset($foo) ? $foo : '';
The first $foowill be set to the value after the ?when the condition after the =is true, else it will be set to the value after the :. The condition (between =and ?) will always be casted as boolean.
第一个$foo将设置为?when 之后的条件=为真时之后的值,否则将设置为 之后的值:。条件(在=和之间?)将始终被转换为布尔值。
Since PHP 5.3 you can write it even shorter:
从 PHP 5.3 开始,你可以写得更短:
$foo = isset($foo) ?: '';
This will set $footo TRUEor FALSE(depending on what isset()returns), as pointed by @Decent Dabbler in the comments. Removing isset()will set it to ''but it will also throw an undefined variable notice (not in production though).
正如@Decent Dabbler 在评论中指出的那样,这将设置$foo为TRUE或FALSE(取决于isset()返回的内容)。删除isset()会将其设置为''但它也会抛出未定义的变量通知(尽管不在生产中)。
Since PHP 7 you can use a null coalesce operator:
从 PHP 7 开始,您可以使用空合并运算符:
$foo = $foo ?? '';
This won't throw any error, but it will evaluate as TRUEif $fooexists and is empty, as opposed to the shorthand ternary operator, that will evaluate as FALSEif the variable is empty.
这不会抛出任何错误,但它将评估为TRUE好像$foo存在并且为空,而不是速记三元运算符,它将评估为FALSE好像变量为空。
回答by Connor Deckers
A somewhat round-about way of doing this is if you put the name of your variables in an array, and then loop them with a Ternary Operator, similar to powtac's answer.
这样做的一种有点迂回的方法是,如果您将变量的名称放在一个数组中,然后使用三元运算符循环它们,类似于powtac的答案。
$vars = array('foo', 'bar', 'ping', 'pong');
$defaultVar = '';
foreach($vars as $var)
{
$$var = isset($$var) ? $$var : $defaultVar;
}
As mentioned in other answers, since version 5.3, PHP allows you to write the above code as follows:
正如其他答案中提到的,从 5.3 版开始,PHP 允许您按如下方式编写上述代码:
$vars = array('foo', 'bar', 'ping', 'pong');
$defaultVar = '';
foreach($vars as $var)
{
$$var = isset($$var) ?: $defaultVar;
}
Note the changed Ternary Operator.
请注意更改后的三元运算符。
回答by mbougarne
In OOP you can use this approach:
在 OOP 中,您可以使用这种方法:
protected $password, $full_name, $email;
For non-OOP you declare them just in code they will be Undefined if you didn't assign any value to them:
对于非 OOP,您只需在代码中声明它们,如果您没有为它们分配任何值,它们将是未定义的:
$foo; $bar; $baz;
$set_foo = (isset($foo)) ? $foo : $foo = 'Foo';
echo $set_foo;
回答by jli
Why not just set them?
为什么不设置它们?
<?php
$foo = '';
$bar = '';
//etc
?>
If you're trying to preserve the value in them, then yes that's the correct way in general. Note that you don't need the second pair of brackets in your statements:
如果您试图保留其中的价值,那么是的,这通常是正确的方法。请注意,您的语句中不需要第二对括号:
if (!isset($foo)) $foo = '';
is enough.
足够。
回答by c0d3x1337
To fix the issue of
为了解决问题
<?php
$foo = $bar = $ping = $pong = '';
?>
throwing
投掷
Notice: Undefined variable: ...
注意:未定义变量:...
<?php
@$foo = $bar = $ping = $pong = '';
?>
It will not fix it but it will not be shown nd will not stop the script from parsing.
它不会修复它,但不会显示它并且不会停止解析脚本。

