123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- --TEST--
- Test array_key_exists() function
- --FILE--
- <?php
-
- echo "*** Testing basic functionalities ***\n";
- $search_arrays = array(
- array(1,2,3,4),
- array('a','b','c'),
- array('abc', 'bcd', 'dcf'),
- array("test", "rest", "enjoy"),
- array("Name" => "Jack", "Loc" => "Mars", "Id" => "MS123"),
- array('Red' => 'Rose', 'I' => 'You'),
- array(0 => 'Zero', 1 => 'One', 2 => 'Two', 3 => "Three" ),
- array(0.1 => 'Zero', 1.1 => 'One', 2.2 => 'Two', 3.3 => "Three" )
- );
- $keys = array( 1, 'a', 2, 4, "Name", "Red", 0, 3 );
- $key_counter = 0;
- foreach ($search_arrays as $search_array) {
- $key = $keys[ $key_counter++ ];
- echo "-- Iteration $key_counter --\n";
- var_dump( array_key_exists($key, $search_array) );
- }
- echo "\n*** Testing possible variations ***\n";
- $key_variations = array ("", NULL, null, " ", '', "test", 1);
- $key_counter = 0;
- $key_count = count ( $key_variations );
- echo "\n** Variation loop 1 **\n";
- $out_loop_count = 0;
- foreach ($search_arrays as $search_array) {
- $key_counter = 0;
- $out_loop_count ++; echo "-- Iteration $out_loop_count --\n";
- while ( $key_counter < $key_count ) {
- $key = $key_variations[ $key_counter++ ];
- var_dump( array_key_exists($key, $search_array) );
- }
- }
- $search_arrays_v = array (
- array(),
- array(NULL),
- array(array(), 1, 2),
- array(1,2,3, "" => "value", NULL => "value", true => "value" ),
- array( array(2,4,5), array ("a","b","d") )
- );
- echo "\n** Variation loop 2 **\n";
- $out_loop_count = 0;
- foreach ($search_arrays_v as $search_array) {
- $key_counter = 0;
- $out_loop_count ++; echo "-- Iteration $out_loop_count --\n";
- while ( $key_counter < $key_count ) {
- $key = $key_variations[ $key_counter++ ];
- var_dump( array_key_exists($key, $search_array) );
- }
- }
- echo "\n*** Testing error conditions ***\n";
- var_dump( array_key_exists() );
- var_dump( array_key_exists(array(), array()) );
- var_dump( array_key_exists("", "") );
- var_dump( array_key_exists(1, 1) );
- var_dump( array_key_exists(1, NULL) );
- var_dump( array_key_exists(1, true) );
- var_dump( array_key_exists(false, true) );
- var_dump( array_key_exists(false, 17.5) );
- var_dump( array_key_exists(1, array(), array()) );
- var_dump( array_key_exists(17.5, array(1,23) ) ) ;
- echo "\n*** Testing operation on objects ***\n";
- class key_check
- {
- private $private_var = "Priviate var";
- protected $protected_var = "Protected var";
- public $public_var = "Public var";
- public $arr = array("var" => "value", "1" => "one", ""=>"value");
- public function print_member()
- {
- echo $this->$private_var."\n";
- echo $this->$protected_var."\n";
- echo $this->$public_var."\n";
- }
- }
- $key_check_obj = new key_check;
- var_dump(array_key_exists("private_var", $key_check_obj));
- var_dump(array_key_exists("protected_var", $key_check_obj));
- var_dump(array_key_exists("public_var", $key_check_obj));
- var_dump(array_key_exists("print_member", $key_check_obj));
- var_dump(array_key_exists("arr", $key_check_obj));
- var_dump(array_key_exists("var", $key_check_obj->arr));
- var_dump( array_key_exists($key_check_obj, $key_check_obj) );
- echo "Done\n";
- ?>
- --EXPECTF--
- *** Testing basic functionalities ***
- -- Iteration 1 --
- bool(true)
- -- Iteration 2 --
- bool(false)
- -- Iteration 3 --
- bool(true)
- -- Iteration 4 --
- bool(false)
- -- Iteration 5 --
- bool(true)
- -- Iteration 6 --
- bool(true)
- -- Iteration 7 --
- bool(true)
- -- Iteration 8 --
- bool(true)
- *** Testing possible variations ***
- ** Variation loop 1 **
- -- Iteration 1 --
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(true)
- -- Iteration 2 --
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(true)
- -- Iteration 3 --
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(true)
- -- Iteration 4 --
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(true)
- -- Iteration 5 --
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- -- Iteration 6 --
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- -- Iteration 7 --
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(true)
- -- Iteration 8 --
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(true)
- ** Variation loop 2 **
- -- Iteration 1 --
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- -- Iteration 2 --
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- -- Iteration 3 --
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(true)
- -- Iteration 4 --
- bool(true)
- bool(true)
- bool(true)
- bool(false)
- bool(true)
- bool(false)
- bool(true)
- -- Iteration 5 --
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(false)
- bool(true)
- *** Testing error conditions ***
- Warning: array_key_exists() expects exactly 2 parameters, 0 given in %s on line %d
- NULL
- Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
- bool(false)
- Warning: array_key_exists() expects parameter 2 to be array, string given in %s on line %d
- NULL
- Warning: array_key_exists() expects parameter 2 to be array, integer given in %s on line %d
- NULL
- Warning: array_key_exists() expects parameter 2 to be array, null given in %s on line %d
- NULL
- Warning: array_key_exists() expects parameter 2 to be array, boolean given in %s on line %d
- NULL
- Warning: array_key_exists() expects parameter 2 to be array, boolean given in %s on line %d
- NULL
- Warning: array_key_exists() expects parameter 2 to be array, double given in %s on line %d
- NULL
- Warning: array_key_exists() expects exactly 2 parameters, 3 given in %s on line %d
- NULL
- Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
- bool(false)
- *** Testing operation on objects ***
- bool(false)
- bool(false)
- bool(true)
- bool(false)
- bool(true)
- bool(true)
- Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
- bool(false)
- Done
|