NYCPHP Meetup

NYPHP.org

[nycphp-talk] Question Using If's and Possible Arrays

Dan Cech dcech at phpwerx.net
Tue Jan 17 20:40:08 EST 2006


As you may have found out, the value of $variable['alias'] will be NULL.

I'm not sure the internal reason for this, but it should never be an 
issue because you should have a line checking the return of callfunction 
and acting appropriately if it's not what's expected.

Something like:

function callfunction()
{
   return false;
}

$variable = callfunction();

if (!is_array($variable)) {
   echo 'there was an error';
}

if ($variable['alias']) {
   do_something();
} else {
   do_something_else();
}

Of course if you were using pear errors you would be able to change it to:

function callfunction()
{
   return PEAR::raiseError('error message',1);
}

$variable = callfunction();

if (PEAR::isError($variable)) {
   echo 'there was an error: '.$variable->getMessage();
}

if ($variable['alias']) {
   do_something();
} else {
   do_something_else();
}

Hope this helps,

Dan

IAlsoAgree at stny.rr.com wrote:
> I have a question about using if statements with variables that are
> sometimes arrays and sometimes not arrays.
> 
> I have a function that returns either the contents of a database row or
> false.
> For example:
> $variable = callfunction()
> 
> In this case, callfunction would either return the contents of a row in
> a database or simply false.
> 
> Lets assume that, for instances of this question, callfunction returns
> false and $variable is now equal to false.
> function callfunction()
>  {
>   return false;
>  }
> 
> If I then use:
> if ($variable['alias'])
> 
> Will the if statement result in false because that is the only value of
> $variable (noting that $variable has NOT been initiated as an array,
> this is the first time it has been refrenced as an array)?
> 
> -Joe




More information about the talk mailing list