The Most Active and Friendliest
Affiliate Marketing Community Online!

“Propeller”/  Direct Affiliate

Tip : all about php data types

D

dman_2007

Guest
PHP supports eight data types :

Four scalar types :
  1. boolean
  2. integer
  3. float
  4. string

two compund types :
  1. array
  2. object

and finally two special types :
  1. resource
  2. null

You can find out a variable or expressions value and type bu using var_dump function, gettype function can also be used for finding out the type of a variable.

If you want to converta variable to a specific data type you can use settype function. Alternatively you can aslo cast it to a different type. You can use following casts to convert a variable to your desired data type in php :

  1. (bool), (boolean)
  2. (int), (integer)
  3. (float) (real), (double)
  4. (string)
  5. (array)
  6. (object)
 
few things on the post above.

if you do an comparison between values, in php this is done via '==' and not '=' (the assign operator). More available here:

PHP: Comparison Operators - Manual

so if ($var = true) will always return true (successfully assigned the value true to the variable $var), even if it was false before. always be careful of such typos, easy to make and hard to spot!

second, PHP evaluates the existence of a variable as true, for example

PHP:
$foo = "bar";
unset($foo);
if ($foo) 
    echo "exists, value is $foo";
else
    echo "not set or false";

$foo = true;
if ($foo) {
// do something here. it will work.
}

what does it mean, 1 or 0 are faster? performance wise or? anyway, this just has no relevance over casting php data types, it would be more useful to cover crappy problems like, WHY YOU SHOULD NOT TRUST A COMPARISON OF FLOATS or similar.
 
MI
Back