Question about variable typing in PHP.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
grill8
Forum Newbie
Posts: 2
Joined: Fri Sep 05, 2008 12:39 am

Question about variable typing in PHP.

Post 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
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: Question about variable typing in PHP.

Post 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
Cut
Forum Commoner
Posts: 39
Joined: Sat Aug 23, 2008 8:01 pm

Re: Question about variable typing in PHP.

Post 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
Post Reply