note 16026 added to function.array-merge

From: Date: Sat, 13 Oct 2001 00:26:56 +0000
Subject: note 16026 added to function.array-merge
Groups: php.notes 
Request: Send a blank email to php-notes+get-18870@lists.php.net to get a copy of this message
Here's an easy way to write functions that accept either a scalar argument or an array. This relies on an undocumented behavior of array_merge(), such that if a non-array arguement is supplied, it is automatically converted into an array. I'm using PHP 4.0.3pl1 from Debian Potato. function scalar_or_array($mixed) { foreach (array_merge(array(), $mixed) as $item) { echo "$item"; } } scalar_or_array('a'); scalar_or_array(array('b', 'c')); This code should print "abc". -- http://www.php.net/manual/en/function.array-merge.php http://master.php.net/manage/user-notes.php?action=edit+16026 http://master.php.net/manage/user-notes.php?action=delete+16026 http://master.php.net/manage/user-notes.php?action=reject+16026

« previous php.notes (#18870) next »