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().
我关闭错误显示的唯一原因是为了避免在使用之前测试每个var,这要归功于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;
?>
7 个解决方案
#1
71
<?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?
如果它是你的脚本,你知道你使用哪些变量,为什么你想花费资源来检查变量是否被声明为befor?
#2
10
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进行检查,但这只是一个偏好,取决于您如何使用这些变量以及用于什么。
#3
7
Your if()
with isset()
attempt is the proper way of doing that!
使用isset()尝试的if()是正确的方法!
But you can write it a little bit shorter/more readable, using the Ternary Operator:
但您可以使用三元运算符将其写得更短/更易读:
$foo = isset($foo) ? $foo : '';
The first $foo
will 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将被设置为之后的值?当=之后的条件为真时,否则它将被设置为:之后的值。条件(在=和?之间)将始终作为布尔值进行转换。
Since PHP 5.3 you can write it even shorter:
从PHP 5.3开始,你可以写得更短:
$foo = isset($foo) ?: '';
This will set $foo
to TRUE
or 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).
这将把$ foo设置为TRUE或FALSE(取决于isset()返回的内容),如评论中@Decent Dabbler所指出的那样。删除isset()会将其设置为'',但它也会抛出一个未定义的变量通知(尽管不在生产中)。
Since PHP 7 you can use a null coalesce operator:
从PHP 7开始,您可以使用null coalesce运算符:
$foo = $foo ?? '';
This won't throw any error, but it will evaluate as TRUE
if $foo
exists and is empty, as opposed to the shorthand ternary operator, that will evaluate as FALSE
if the variable is empty.
这不会抛出任何错误,但如果$ foo存在并且为空,它将评估为TRUE,而不是速记三元运算符,如果变量为空,则将评估为FALSE。
#4
5
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.
请注意更改的三元运算符。
#5
0
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.
足够。
#6
0
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.
它不会修复它但它不会显示nd不会阻止脚本解析。
#7
-3
$a = ['foo', 'bar', 'ping', 'pong']; foreach($a as $v) $$v = '';
$ a = ['foo','bar','ping','pong']; foreach($ a as $ v)$$ v ='';
// ;)
//;)
#1
71
<?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?
如果它是你的脚本,你知道你使用哪些变量,为什么你想花费资源来检查变量是否被声明为befor?
#2
10
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进行检查,但这只是一个偏好,取决于您如何使用这些变量以及用于什么。
#3
7
Your if()
with isset()
attempt is the proper way of doing that!
使用isset()尝试的if()是正确的方法!
But you can write it a little bit shorter/more readable, using the Ternary Operator:
但您可以使用三元运算符将其写得更短/更易读:
$foo = isset($foo) ? $foo : '';
The first $foo
will 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将被设置为之后的值?当=之后的条件为真时,否则它将被设置为:之后的值。条件(在=和?之间)将始终作为布尔值进行转换。
Since PHP 5.3 you can write it even shorter:
从PHP 5.3开始,你可以写得更短:
$foo = isset($foo) ?: '';
This will set $foo
to TRUE
or 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).
这将把$ foo设置为TRUE或FALSE(取决于isset()返回的内容),如评论中@Decent Dabbler所指出的那样。删除isset()会将其设置为'',但它也会抛出一个未定义的变量通知(尽管不在生产中)。
Since PHP 7 you can use a null coalesce operator:
从PHP 7开始,您可以使用null coalesce运算符:
$foo = $foo ?? '';
This won't throw any error, but it will evaluate as TRUE
if $foo
exists and is empty, as opposed to the shorthand ternary operator, that will evaluate as FALSE
if the variable is empty.
这不会抛出任何错误,但如果$ foo存在并且为空,它将评估为TRUE,而不是速记三元运算符,如果变量为空,则将评估为FALSE。
#4
5
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.
请注意更改的三元运算符。
#5
0
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.
足够。
#6
0
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.
它不会修复它但它不会显示nd不会阻止脚本解析。
#7
-3
$a = ['foo', 'bar', 'ping', 'pong']; foreach($a as $v) $$v = '';
$ a = ['foo','bar','ping','pong']; foreach($ a as $ v)$$ v ='';
// ;)
//;)