note 7880 added to in_array

From: Date: Fri, 18 Aug 2000 06:53:53 +0000
Subject: note 7880 added to in_array
Groups: php.notes 
Request: Send a blank email to php-notes+get-2454@lists.php.net to get a copy of this message
It's worth noting that PHP4 will generate the warning message "Wrong datatype for second argument in call to in_array()" during execution if the array you pass to in_array() is undefined, instead of just returning false. In other words if there is a potential that your array won't have values inserted into it by the time it's sent to in_array(), be sure to initialize it with an empty array to avoid nasty error messages appearing in your pretty PHP pages: For example, a lurking disaster: -------------------------------------- if ($sometimes == "true") { $array[] = "something"; } in_array("something" $array); // statement above may generate warning The safe way: --------------------------------------- $array = array (); if ($sometimes == "true") { $array[] = "something"; } in_array("something", $array); // won't ever generate a warning http://www.php.net/manual/function.in-array.php

« previous php.notes (#2454) next »