PHP - 检查变量是否未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30191521/
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
PHP - check if variable is undefined
提问by Timothy Coetzee
Consider this jquery statment
考虑这个 jquery 语句
isTouch = document.createTouch !== undefined
I would like to know if we have a similar statement in PHP, not being isset() but literally checking for an undefined value, something like:
我想知道我们在 PHP 中是否有类似的语句,不是 isset() 而是字面上检查未定义的值,例如:
$isTouch != ""
$isTouch != ""
Is there something similar as the above in PHP?
PHP中是否有与上述类似的东西?
回答by Sougata Bose
You can use -
您可以使用 -
$isTouch = isset($variable);
It will store true
if $variable
is defined else false
. If need the invers the simply remove the !
.
它将存储true
if$variable
定义为 else false
。如果需要反转,只需删除!
.
Note : Returns TRUE if var exists and has value other than NULL, FALSE otherwise.
注意:如果 var 存在且具有非 NULL 值,则返回 TRUE,否则返回 FALSE。
Or if you want to check for false
, 0
etc also then use empty()
-
或者,如果你想检查false
,0
等等也然后使用empty()
-
$isTouch = empty($variable);
empty()
works for -
empty()
效劳于 -
- ""(an empty string)
- 0(0 as an integer)
- 0.0(0 as a float)
- "0"(0 as a string)
- NULL
- FALSE
- array()(an empty array)
- $var;(a variable declared, but without a value)
- "" (空字符串)
- 0 (0 作为整数)
- 0.0 (0 作为浮点数)
- “0” (0 作为字符串)
- 空值
- 错误的
- array() (空数组)
- $var; (声明的变量,但没有值)
回答by TiDJ
An another way is simply :
另一种方法很简单:
if($test){
echo "Yes 1";
}
if(!is_null($test)){
echo "Yes 2";
}
$test = "hello";
if($test){
echo "Yes 3";
}
Will return :
将返回 :
"Yes 3"
The best way is to use isset(), otherwise you can have an error like "undefined $test".
最好的方法是使用isset(),否则会出现“undefined $test”之类的错误。
You can do it like this :
你可以这样做:
if( isset($test) && ($test!==null) )
You'll not have any error because the first condition isn't accepted.
你不会有任何错误,因为第一个条件不被接受。
回答by ErasmoOliveira
To check is variable is set you need to use isset function.
要检查是否设置了变量,您需要使用 isset 函数。
$lorem = 'potato';
if(isset($lorem)){
echo 'isset true' . '<br />';
}else{
echo 'isset false' . '<br />';
}
if(isset($ipsum)){
echo 'isset true' . '<br />';
}else{
echo 'isset false' . '<br />';
}
this code will print:
此代码将打印:
isset true
isset false
read more in https://php.net/manual/en/function.isset.php
回答by Sumit Thakur
You can use -
您可以使用 -
Ternary oprator to check wheather value set by POST/GET or not somthing like this
三元运算符检查 POST/GET 设置的值是否是这样的
$value1 = $_POST['value1'] = isset($_POST['value1']) ? $_POST['value1'] : '';
$value2 = $_POST['value2'] = isset($_POST['value2']) ? $_POST['value2'] : '';
$value3 = $_POST['value3'] = isset($_POST['value3']) ? $_POST['value3'] : '';
$value4 = $_POST['value4'] = isset($_POST['value4']) ? $_POST['value4'] : '';
回答by Quasimodo's clone
JavaScript's 'strict not equal' operator (!==
) on comparison with undefined
does notresult in false
on null
values.
JavaScript的“严格不等于”运算符(!==
)与比较undefined
并不会导致false
对null
值。
var createTouch = null;
isTouch = createTouch !== undefined // true
To achieve an equivalent behaviour in PHP, you can check whether the variable name exists in the keys of the result of get_defined_vars()
.
为了在 PHP 中实现等价的行为,您可以检查变量名是否存在于结果的键中get_defined_vars()
。
// just to simplify output format
const BR = '<br>' . PHP_EOL;
// set a global variable to test independence in local scope
$test = 1;
// test in local scope (what is working in global scope as well)
function test()
{
// is global variable found?
echo '$test ' . ( array_key_exists('test', get_defined_vars())
? 'exists.' : 'does not exist.' ) . BR;
// $test does not exist.
// is local variable found?
$test = null;
echo '$test ' . ( array_key_exists('test', get_defined_vars())
? 'exists.' : 'does not exist.' ) . BR;
// $test exists.
// try same non-null variable value as globally defined as well
$test = 1;
echo '$test ' . ( array_key_exists('test', get_defined_vars())
? 'exists.' : 'does not exist.' ) . BR;
// $test exists.
// repeat test after variable is unset
unset($test);
echo '$test ' . ( array_key_exists('test', get_defined_vars())
? 'exists.' : 'does not exist.') . BR;
// $test does not exist.
}
test();
In most cases, isset($variable)
is appropriate. That is aquivalent to array_key_exists('variable', get_defined_vars()) && null !== $variable
. If you just use null !== $variable
without prechecking for existence, you will mess up your logs with warnings because that is an attempt to read the valueof an undefined variable.
大多数情况下,isset($variable)
是合适的。这相当于array_key_exists('variable', get_defined_vars()) && null !== $variable
. 如果你只是使用null !== $variable
而不预先检查是否存在,你会用警告弄乱你的日志,因为这是尝试读取未定义变量的值。
However, you can apply an undefined variable to a reference without any warning:
但是,您可以在没有任何警告的情况下将未定义的变量应用于引用:
// write our own isset() function
function my_isset(&$var)
{
// here $var is defined
// and initialized to null if the given argument was not defined
return null === $var;
}
// passing an undefined variable by reference does not log any warning
$is_set = my_isset($undefined_variable); // $is_set is false
回答by Waruna Manjula
if(isset($variable)){
$isTouch = $variable;
}
OR
或者
if(!isset($variable)){
$isTouch = "";//
}