PHP-変数が未定義かどうかを確認します


92

このjqueryステートメントを検討してください

isTouch = document.createTouch !== undefined

PHPに同様のステートメントがあり、isset()ではなく、文字通り未定義の値をチェックしているかどうかを知りたいのですが、次のようになります。

$isTouch != ""

PHPに上記のようなものはありますか?


回答:


171

使用できます-

$isTouch = isset($variable);

が定義されているtrue場合に返さ$variableれます。変数が定義されていない場合は、を返しfalseます。

注:varが存在し、NULL以外の値を持つ場合はTRUEを返し、それ以外の場合はFALSEを返します。

あなたがチェックしたい場合はfalse0などあなたは、その後、使用することができますempty()-

$isTouch = empty($variable);

empty() のために働く-

  • "" (空の文字列)
  • 0 (整数として0)
  • 0.0 (フロートとして0)
  • 「0」 (文字列として0)
  • ヌル
  • FALSE
  • array() (空の配列)
  • $ var; (宣言された変数ですが、値はありません)

2
isset()常にboolを返します。
VeeeneX 2015年

ブールキャストは必要ですか?isset(...)はすでにブール値を返しますか?
Cr3aHal0 2015年

1
いいえ。戻るtruefalse。したがって、そのキャストは必要ありません。
Sougata Bose 2015

1
empty()空の文字列の場合はfalseを返すを使用してこれを行うこともできます。isset()空の文字列の場合はtrueを返し、空の場合は内部でissetチェックを実行します。
マイク2015年

1
$isTouch = (bool) $variable;これを行うこともできます。これはと同じisset()ように機能し、のように機能するため、おそらく少し優れていますempty()
マイク2015年

20

別の方法は単純です:

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

最初の条件が受け入れられないため、エラーは発生しません。


$test!==null角かっこなしで使用すると、どうなりますか?エラーが発生しますか?
Virの

いいえ、それも大丈夫です。
TiDJ

7

変数が設定されているかどうかを確認するには、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で詳細を読む


6

使用できます-

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'] : '';

3

JavaScriptの「厳密ではない」演算子(!==)との比較でundefinedは、値は生成されませfalsenull

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

1

PHPのisset()関数を使用して、変数が設定されているかどうかをテストできます。ISSET()はNULLに設定されている変数をテストした場合に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/をご覧ください。


弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.