Hi all,

I have 2 multidimensional arrays which i have to print simultaneously and one of them will not have same number of elements at some times. So i check them using empty() function. As it is not possible to check every single element, i wrote a function which has to check the elements index (i pass these as arguments)and do the remaining process. But here the problem is that the function is not passing if the element is empty and this effects the other array to be printed properly.

I would be thankful if some one could tell me how to pass empty/NULL values to functions or how to make function accept the empty/NULL values to pass as arguments.

Thanking you,

Kind Regards

Recommended Answers

All 4 Replies


function foo( $bar = null, $baz = null, $qux = null ){
     //Check if variables are not null
     //Do something with the variables


foo( $bar );
foo( $bar, $baz );
foo( $bar, $baz, $qux );
foo( null, $baz );
foo( null, null, $qux );

//etc etc...

The call to the function looks like this:

$toCount = toCount($human["gene"]["genenames"], $mouse["gene"]["genenames"]);

and here the second array "genenames" is not created in


and the function looks like this:

function toCount($man = null, $mice = null)
			if((!(empty($man))) && (!(empty($mice))))
				$toCount = max(count($man), count($mice)); # take the one which has maximum entries as the counter
			else if (!(empty($man)))
				$toCount = count($man);
			else if(!(empty($mice)))
				$toCount = count($mice);
			return $toCount;

when i do this it shows me an error that undefined index "genenames" (as the array is not created) and this array creation depends on the user query. So what i want is if then function takes these arguments then i can check inside the function with empty() or array_exists() functions.

Thanks a lot in advance.


You can't use an array index that is not defined. Period.
You would first need to do a check of the variable to see if it exists and is set before passing it to the function.

if( empty( $mouse["gene"]["genenames"] ) ){
    $mouse["gene"]["genenames"] = array();

That code will check if the index is empty (http://php.net/manual/en/function.empty.php), and if it doesn't exist it just creates it as an empty array.

if( empty( $mouse["gene"]["genenames"] ) ){
	$mouse["gene"]["genenames"] = array();

$toCount = toCount( $human["gene"]["genenames"], $mouse["gene"]["genenames"] );
Member Avatar

I tried following this thread, but got confused. Apologies if I got the wrong end of the stick:

//Initialize to prevent error on passing parameters
$man = array();
$mouse = array();
//I assume that these arrays are always created, so the above may not be needed

function toCount($man,$mouse){
  $manno = (isset($man['gene']['genenames']))? count($man['gene']['genenames']) : 0;
  $mouseno = (isset($mouse['gene']['genenames']))? count($mouse['gene']['genenames']) : 0;
  return $manno + $mouseno;

//dummy data - could be anything
$man['gene']['genenames'] = array('t','s');
$mouse['gene']['genenames'] = array(0,1,2,3,4,5,6,7,8);

echo toCount($man,$mouse);

@ms, you'll be able to put me straight - isset() OK in this instance?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.