Page 1 of 1

Question about variable typing in PHP.

Posted: Sat Sep 06, 2008 11:20 pm
by grill8
Hello all,

I am an experienced C++ programmer but am new to PHP and have some questions about variable typing.

In C++ you type all of your variables (int, short, long, double etc.) but in PHP it is my understanding that all variables are simply preceded by a $.

I am using PHP 4 so unfortunately exceptions are not allowed which means (like in C) you usually return success or failure rather than being allowed the luxury of using exceptions and returning what you will.

In short, my questions are ...

Can I return ... say FALSE from a PHP function if something fails, but in another place in the same function return ... say ... an array?
Also, how does this affect the variable that contains the return value (see below) since they are of different types (FALSE and array are different type)?


For example, consider this:
Foo is returning either A) FALSE, or B) an array.

Code: Select all

 
$Array = array();
if(FALSE == $Array = Foo())
{
     return FALSE;   
}
 

So ... in short ... I need to know if you can return values of different types from PHP functions, and how (like in the snippet above) one tests for equalities between values if they are of different types (in the above example $Array could then be assigned either FALSE or an array).

Thank you for your help.
Jeremy

Re: Question about variable typing in PHP.

Posted: Sun Sep 07, 2008 12:18 am
by zplits
I have found it in my book. I hope this will help you.
PHP also supports a number of specialized functions to check if a variable or
value belongs to a specific type.
Function - What It Does
is_bool() - Checks if a variable or value is Boolean
is_string() - Checks if a variable or value is a string
is_numeric() - Checks if a variable or value is a numeric string
is_float() - Checks if a variable or value is a floating point number
is_int() - Checks if a variable or value is an integer
is_null() - Checks if a variable or value is NULL
is_array() - Checks if a variable is an array
is_object() - Checks if a variable is an object

Re: Question about variable typing in PHP.

Posted: Sun Sep 07, 2008 12:48 am
by Cut
grill8 wrote:Can I return ... say FALSE from a PHP function if something fails, but in another place in the same function return ... say ... an array?
Uh huh.
For example, consider this:
Foo is returning either A) FALSE, or B) an array.

Code: Select all

 
$Array = array();
if(FALSE == $Array = Foo())
{
     return FALSE;   
}
 
That snippet makes no sense. If FALSE is equal to $array equals foo() then return false ???

I don't understand why $Array would be false, either...
So ... in short ... I need to know if you can return values of different types from PHP functions, and how (like in the snippet above) one tests for equalities between values if they are of different types (in the above example $Array could then be assigned either FALSE or an array).
You can use the === operator: http://ca.php.net/manual/en/language.op ... arison.php