PHP – isset, in_array 用法

isset, in_array 用法

PHP basic function,isset, in_array,檢查變數是否已設置及不可為NULL,和檢查陣列中的值是否存在,也用簡單的舉例來介紹吧!

  • isset
  • in_array

isset

isset – 檢查變數是否已設置且不為NULL
bool isset ( mixed $var [, mixed $… ] )

$a = "omg";
$b = "charlie";
echo isset($a); // TRUE
echo isset($a, $b); // TRUE
unset ($a);
echo isset($a); // FALSE
echo isset($a, $b); // FALSE
$c = NULL;
echo isset($c); // FALSE

in_array

in_array – 檢查陣列中的值是否存存在
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

$array = ['aaa', 'bbb', 'ccc'];
if (in_array('aaa', $array)){
  echo 'aaa is in array.';
}

aaa is in array.

if (!in_array('123', $array)){
  echo '123 is not in array.';
}

123 is nor in array.

一起討論吧