php 在PHP中取消设置之前检查var是否存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1374705/
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
Check if var exist before unsetting in PHP?
提问by JasonDavis
With error reporting on, or even for best practice, when unsetting a variable in PHP, should you check to see if it exist first (in this case it does not always exist) and the unset it, or just unset it?
对于错误报告,甚至是最佳实践,当在 PHP 中取消设置变量时,您应该先检查它是否存在(在这种情况下它并不总是存在)然后取消设置,还是只是取消设置?
<?PHP
if (isset($_SESSION['signup_errors'])){
unset($_SESSION['signup_errors']);
}
// OR
unset($_SESSION['signup_errors']);
?>
回答by Jo?o Silva
Just unset it, if it doesn't exist, nothing will be done.
只需取消设置它,如果它不存在,则不会执行任何操作。
回答by Mr. Smith
In regard to some confusion earlier in these notes about what causes unset() to trigger notices when unsetting variables that don't exist....
Unsetting variables that don't exist, as in
<?php unset($undefinedVariable); ?>does not trigger an "Undefined variable" notice. But
<?php unset($undefinedArray[$undefinedKey]); ?>triggers two notices, because this code is for unsetting an element of an array; neither $undefinedArray nor $undefinedKey are themselves being unset, they're merely being used to locate what should be unset. After all, if they did exist, you'd still expect them to both be around afterwards. You would NOT want your entire array to disappear just because you unset() one of its elements!
关于在这些注释中早些时候关于什么导致 unset() 在取消设置不存在的变量时触发通知的一些混淆......
取消设置不存在的变量,如
<?php unset($undefinedVariable); ?>不会触发“未定义变量”通知。但
<?php unset($undefinedArray[$undefinedKey]); ?>触发两个通知,因为此代码用于取消设置数组的元素;$undefinedArray 和 $undefinedKey 本身都没有被取消设置,它们只是用来定位应该取消设置的内容。毕竟,如果它们确实存在,您仍然希望它们之后都存在。你不会希望你的整个数组因为你 unset() 它的一个元素而消失!
回答by Dan Bray
Using unseton an undefined variable will not cause any errors (unless the variable is the index of an array (or object) that doesn't exist).
unset在未定义的变量上使用不会导致任何错误(除非该变量是不存在的数组(或对象)的索引)。
Therefore the only thing you need to consider, is what is most efficient. It is more efficient to not test with 'isset', as my test will show.
因此,您唯一需要考虑的是什么是最有效的。正如我的测试所示,不使用 'isset' 进行测试会更有效。
Test:
测试:
function A()
{
for ($i = 0; $i < 10000000; $i++)
{
$defined = 1;
unset($defined);
}
}
function B()
{
for ($i = 0; $i < 10000000; $i++)
{
$defined = 1;
unset($undefined);
}
}
function C()
{
for ($i = 0; $i < 10000000; $i++)
{
$defined = 1;
if (isset($defined))
unset($defined);
}
}
function D()
{
for ($i = 0; $i < 10000000; $i++)
{
$defined = 1;
if (isset($undefined))
unset($undefined);
}
}
$time_pre = microtime(true);
A();
$time_post = microtime(true);
$exec_time = $time_post - $time_pre;
echo "Function A time = $exec_time ";
$time_pre = microtime(true);
B();
$time_post = microtime(true);
$exec_time = $time_post - $time_pre;
echo "Function B time = $exec_time ";
$time_pre = microtime(true);
C();
$time_post = microtime(true);
$exec_time = $time_post - $time_pre;
echo "Function C time = $exec_time ";
$time_pre = microtime(true);
D();
$time_post = microtime(true);
$exec_time = $time_post - $time_pre;
echo "Function D time = $exec_time";
exit();
Results:
结果:
Function A time = 1.0307259559631- Defined without isset
Function B time = 0.72514510154724- Undefined without isset
Function C time = 1.3804969787598- Defined using isset
Function D time = 0.86475610733032- Undefined using isset
Function A time = 1.0307259559631- 无 isset 定义
Function B time = 0.72514510154724- 没有 isset 的未定义
Function C time = 1.3804969787598- 使用 isset 定义
Function D time = 0.86475610733032- 使用 isset 未定义
Conclusion:
结论:
It is always less efficient to use isset, not to mention the small amount of extra time it takes to write. It's quicker to attempt to unsetan undefined variable than to check if it can be unset.
使用 总是效率较低isset,更不用说编写所需的少量额外时间。尝试unset使用未定义的变量比检查它是否可以更快unset。
回答by TarranJones
If you would like to unset a variable then you can just use unset
如果你想取消设置一个变量,那么你可以使用 unset
unset($any_variable); // bool, object, int, string etc
Checking for its existence has no benefit when trying to unset a variable.
尝试取消设置变量时,检查它的存在没有任何好处。
If the variable is an array and you wish to unset an element you must make sure the parentexists first, this goes for object properties too.
如果变量是一个数组并且您希望取消设置一个元素,您必须首先确保父元素存在,这也适用于对象属性。
unset($undefined_array['undefined_element_key']); // error - Undefined variable: undefined_array
unset($undefined_object->undefined_prop_name); // error - Undefined variable: undefined_object
This is easily solved by wrapping the unsetin an if(isset($var)){ ... }block.
这可以通过将 包裹unset在一个if(isset($var)){ ... }块中轻松解决。
if(isset($undefined_array)){
unset($undefined_array['undefined_element_key']);
}
if(isset($undefined_object)){
unset($undefined_object->undefined_prop_name);
}
The reason we only check the variable(parent) is simply because we don't need to check the property/element and doing so would be a lot slower to write and compute as it would add an extra check.
我们只检查变量(parent)的原因仅仅是因为我们不需要检查属性/元素,这样做会导致编写和计算慢很多,因为它会添加额外的检查。
if(isset($array)){
...
}
if(isset($object)){
...
}
.vs
.vs
$object->prop_name = null;
$array['element_key'] = null;
// This way elements/properties with the value of `null` can still be unset.
if(isset($array) && array_key_exists('element_key', $array)){
...
}
if(isset($object) && property_exists($object, 'prop_name')){
...
}
// or
// This way elements/properties with `null` values wont be unset.
if(isset($array) && $array['element_key'])){
...
}
if(isset($object) && $object->prop_name)){
...
}
This goes without saying but it is also crucial that you know the typeof the variable when getting, setting and unsetting an element or property; using the wrong syntax will throw an error.
这不言而喻,但type在获取、设置和取消设置元素或属性时了解变量的含义也很重要;使用错误的语法会引发错误。
Its the same when trying to unset the value of a multidimensional array or object. You must make sure the parent key/name exists.
尝试取消设置多维数组或对象的值时也是如此。您必须确保父键/名称存在。
if(isset($variable['undefined_key'])){
unset($variable['undefined_key']['another_undefined_key']);
}
if(isset($variable->undefined_prop)){
unset($variable->undefined_prop->another_undefined_prop);
}
When dealing with objects there is another thing to think about, and thats visibility.
在处理对象时,还有另一件事要考虑,那就是可见性。
Just because it exists doesn't mean you have permission to modify it.
仅仅因为它存在并不意味着您有权修改它。
回答by evgpisarchik
Check this link https://3v4l.org/hPAto
检查此链接 https://3v4l.org/hPAto
The online tool shows code compatibility for different versions of PHP
在线工具显示不同版本PHP的代码兼容性
According to this tool the code
根据这个工具的代码
unset($_SESSION['signup_errors']);
would work for PHP >=5.4.0 without giving you any notices/warnings/errors.
将适用于 PHP >=5.4.0 而不会给您任何通知/警告/错误。

