RE: Help with a home-grown function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dan Trainor <mailto:info@xxxxxxxxxxxxxxxx>
    on Thursday, July 21, 2005 2:03 PM said:

> I never see "hi" even if an array is set as such:
> 
> $vars = array("one","two","three","four","five");

That's because your function always returns true. If it finds a missing
value it returns true. If it doesn't find a missing value it returns
true. Because of this the true code block is always executed.

You should do this instead:

function hasMissingVals($input)
{
	// make sure we've been passed an array with values
	if(is_array($input) && (count($input) > 0))
	{
		foreach($input as $v)
		{
			if(empty($v))
			{
				return true;
			}
		}
	}

	return false;
}

This way your function will always return false unless it finds a
missing value in which case it will return true.

As you can see I changed the name slightly from 'findMissingVals()' to
'hasMissingVals()' since your function is not actually finding any
missing values. Finding suggests that your function will return data
based on the location of the missing value which it does not do.

> if (findMissingVals($vars)) {
> 	if (!$var1)	{ ?> hi1 <? };
> 	if (!$var2)	{ ?> hi2 <? };
> 	if (!$var3)	{ ?> hi3 <? };
> 	if (!$var4)	{ ?> hi4 <? };
> 	if (!$var5)	{ ?> hi5 <? };
> 	if (!$var6)	{ ?> hi6 <? };
> } else {
> 	echo "hi";
> }

1. Where are $var1, $var2, $var3, etc. coming from? I think you mean to
write $var[1], $var[2], $var[3], etc.?

2. It will be a good idea to get out of the habit of breaking in and out
of PHP like that. Instead just do: echo 'hi1';

You'll have problems down the road with modifying the headers (cookies,
redirects, content-type, etc.) if you break in and out.


Hope this helps,
Chris.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux