PHP - 检查变量是否未定义

时间:2015-05-12 12:54:49

标签: php

考虑这个jquery语句

isTouch = document.createTouch !== undefined

我想知道我们是否在PHP中有类似的声明,而不是isset()但是确切地检查未定义的值,如:

$isTouch != ""

在PHP中是否有类似的内容?

9 个答案:

答案 0 :(得分:106)

您可以使用 -

$isTouch = isset($variable);

如果true定义为$variable,它将存储false。如果需要反转,只需删除!

  

注意:如果var存在并且其值不是NULL,则返回TRUE,否则返回FALSE。

或者如果您想检查false0等,请使用empty() -

$isTouch = empty($variable);

empty()适用于 -

  • "" (空字符串)
  • 0 (0为整数)
  • 0.0 (0作为浮动)
  • " 0" (0作为字符串)
  • NULL
  • FALSE
  • array() (空数组)
  • $ var; (声明的变量,但没有值)

答案 1 :(得分:16)

另一种方式是:

if($test){
    echo "Yes 1";
}
if(!is_null($test)){
    echo "Yes 2";
}

$test = "hello";

if($test){
    echo "Yes 3";
}

将返回:

"Yes 3"

最好的方法是使用isset(),否则你可能会遇到类似" undefined $ test"的错误。

你可以这样做:

if( isset($test) && ($test!==null) )

您没有任何错误,因为第一个条件未被接受。

答案 2 :(得分:6)

要检查是否设置了变量,您需要使用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 />';
}

此代码将打印:

isset true
isset false

https://php.net/manual/en/function.isset.php

中阅读更多内容

答案 3 :(得分:5)

isset() 函数不检查变量是否已定义。

您似乎明确表示您不是在问题中寻找 isset()。我不知道为什么有这么多答案说 isset() 是要走的路,或者为什么接受的答案也说明了这一点。

在编程中认识到 null 是很重要的。我不知道为什么决定 isset() 在值为 null 时返回 false。

要检查变量是否未定义,您必须使用 get_defined_vars() 检查变量是否在已定义变量列表中。没有与 JavaScript 的 undefined 等价的东西(这就是问题中显示的内容,那里没有使用 jQuery)。

在下面的例子中,它的工作方式与 JavaScript 的未定义检查相同。

$isset = isset($variable);
var_dump($isset); // false

但是在这个例子中,它不会像 JavaScript 的 undefined 检查那样工作。

$variable = null;
$isset = isset($variable);
var_dump($isset); // false

$variable 被定义为 null,但 isset() 调用仍然失败。

那么你实际上如何检查变量是否已定义?您检查定义的变量。

使用 get_defined_vars() 将返回一个关联数组,键是变量名,值是变量值。我们这里仍然不能使用isset(get_defined_vars()['variable']),因为键可能存在并且值仍然为空,所以我们必须使用array_key_exists('variable', get_defined_vars())

$variable = null;
$isset = array_key_exists('variable', get_defined_vars());
var_dump($isset); // true


$isset = array_key_exists('otherVariable', get_defined_vars());
var_dump($isset); // false

但是,如果您发现在您的代码中必须检查变量是否已定义,那么您很可能做错了。这是我个人的看法,为什么核心 PHP 开发人员会在某些内容为 null 时让 isset() 返回 false。

答案 4 :(得分:4)

您可以使用 -

Ternary oprator检查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'] : '';

答案 5 :(得分:2)

!==进行比较时,

JavaScript'严格不等于'运算符(undefined导致false null值{。}}。< / p>

var createTouch = null;
isTouch = createTouch !== undefined  // true

要在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();

在大多数情况下,isset($variable)是合适的。这对array_key_exists('variable', get_defined_vars()) && null !== $variable是有利的。如果您只是使用null !== $variable而没有预先检查存在,那么您将使用警告搞乱日志,因为这是尝试读取未定义变量的

但是,您可以在没有任何警告的情况下将未定义的变量应用于引用:

// 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

答案 6 :(得分:1)

您可以使用PHP isset()函数测试是否设置了变量。如果测试已设置为NULL的变量, isset()将返回FALSE。示例:

<?php
    $var1 = '';
    if(isset($var1)){
        echo 'This line is printed, because the $var1 is set.';
    }
?>

此代码将输出“由于设置了$ var1,因此已打印此行。”

进一步了解https://stackhowto.com/how-to-check-if-a-variable-is-undefined-in-php/

答案 7 :(得分:0)

if(isset($variable)){
    $isTouch = $variable;
}

OR

if(!isset($variable)){
    $isTouch = "";// 
}

答案 8 :(得分:0)

最简单的方法检查 variablearray index 是否存在(已定义) && is not null && is not empty && is not false :

#1

if($variable ?? false) echo '$variable is defined';
else echo '$variable is not defined';

// Result: $variable is not defined

#2

$variable = null;

if($variable ?? false) echo '$variable is not null';
else echo '$variable is null';

// Result: $variable is null

#3

$variable = false;

if($variable ?? false) echo '$variable is not false';
else echo '$variable is false';

// Result: $variable is false

#4

$variable = '';

if($variable ?? false) echo '$variable is not empty';
else echo '$variable is empty';

// Result: $variable is empty