i want use array constant in php 5.6. question is: how check whether key 'a' exists in array , "test2" return true well?
my code this:
const arr = array( 'a' => 'first', 'b' => 'second' ); $test1 = defined("arr"); $test2 = defined("arr['a']"); echo '<br>test1: '; var_dump($test1); echo '<br>test2: '; var_dump($test2);
result:
test1: bool(true) test2: bool(false)
you need use array_key_exists function
var_dump(array_key_exists('a', arr));
defined() checks if constant defined , is, can additionaly check if constant array is_array(arr);
example:
<?php const arr = array( 'a' => 'first', 'b' => 'second' ); $test1 = array_key_exists('a', arr); $test2 = array_key_exists('c', arr); echo 'test1: '; var_dump($test1); echo 'test2: '; var_dump($test2);
output:
test1: bool(true) test2: bool(false)
notice:
it work php version >= 5.6 working fiddle
Comments
Post a Comment