Types

Boolean
It is the simplest type. A boolean expresses a truth value. It can be either TRUE or FALSE.

Syntax

To specify a boolean literal, assign it the keywords TRUE or FALSE. Both words are case-insensitive.  Typically, the result of an operator which returns a boolean value is passed on to a control structure.  Converting to boolean

To explicitly convert a value to boolean, use the (bool) or (boolean) casts. Although, in most cases the cast is unnecessary, since a value will be automatically converted if an operator, function or control structure requires a boolean argument.

When converting to boolean, the following values are considered FALSE: the boolean FALSE itself the integer 0 (zero) the float 0.0 (zero) the empty string, and the string "0" an array with zero elements an object with zero member variables (PHP 4 only) the special type NULL (including unset variables) SimpleXML objects created from empty tags

Every other value is considered TRUE (including any resource). Warning: -1 is considered TRUE, like any other non-zero (whether negative or positive) number!  Example: 

is_bool
is_bool — checks out whether a variable is a boolean It is mostly applied in control structures and conditional statements. is_bool ($var)

Finds whether the given variable is a boolean. var - is the variable being evaluated. Returns TRUE if var is a boolean, and FALSE if otherwise.

Example: 

Integers
When you declare a variable and assign it a whole number, then php assigns it an integer datatype (you do not have to tell php this it does it automatically), php does not support unsigned integers, the size of an integer is platform dependent although the size of about 2 billon is the usual value for 32 bits signed.

The only problem is that php does not alert you when you go outside the bounds of what an integer can hold. An integer is any positive or negative whole number within the accepted limit.

They can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2)notation, optionally preceded by a sign (- or +).

The Binary integer literals are available since PHP 5.4.0.

To use the octal notation, then you precede the number with a 0 (zero). To use hexadecimal notation precede the number with 0x. To use binary notation precede the number with 0b.

Example: 

Formally, the structure for integer literals is: decimal    : [1-9][0-9]* | 0

hexadecimal : 0[xX][0-9a-fA-F]+

octal      : 0[0-7]+

binary     : 0b[01]+

integer    : [+-]?decimal | [+-]?hexadecimal | [+-]?octal | [+-]?binary

Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.

Warning : If an invalid digit is given in an octal integer (i.e. 8 or 9), the rest of the number is ignored.

Example  php Integer overflow:

When PHP encounters a number beyond the bounds of the integer type, then the number will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will consequently return a float.

Example (32-bit system): 

Example: (64-bit system) 

There is no integer division operator in PHP. 1/2 yields the float 0.5. The value can be casted to an integer to round it downwards, or the round function provides finer control over rounding.  Converting to integer

In order to explicitly convert a value to integer, you can use either the (int) or (integer) casts. Although, in most cases the cast is not needed, since a value will be automatically converted if an operator, function or control structure requires an integer argument. A value can also be converted to integer with the intval function.

Converting From booleans FALSE will yield 0 (zero), and TRUE will yield 1 (one). Converting From floating point numbers When converting from float to integer, the number will be rounded towards zero. If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31 on 32-bit platforms and +/- 9.22e+18 = 2^63 on 64-bit platforms), the result is undefined, since the float doesn't have enough precision to give an exact integer result. No warning, not even a notice will be issued when this happens! Warning Never cast an unknown fraction to integer, as this can sometimes lead to unexpected results. 

Converting From strings See php Strings

The behaviour of converting to integer is undefined for other types. Do not rely on any observed behaviour, as it can change without notice.

is_int
is_int — Checks to find whether the type of a given variable is integer is_int ( mixed $var )

Note:

To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric.

var - Is the variable being evaluated. Returns TRUE if var is an integer, FALSE otherwise.

Example: 

Floating point numbers
Floating point numbers (also known as "floats", "doubles", or "real numbers") can be specified using any of the following syntaxes: 

Formally: LNUM         [0-9]+ DNUM         ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*) EXPONENT_DNUM [+-]?(({LNUM} | {DNUM}) [eE][+-]? {LNUM})

The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (the 64 bit IEEE format). Warning: Floating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and, of course, error progragation must be considered when several operations are compounded. Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....

NB:never trust floating number results to the last digit, and never compare floating point numbers for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available.

Arrays
An array can be created by using the array language construct. It takes as parameters any number of comma-separated key => value pairs. array( key =>  value, ... ) // key may only be an integer or string // value may be any value of any type  "bar", 12 => true); echo $arr["foo"]; // bar echo $arr[12];   // 1 ?> For more see php Arrays

Objects
Object Initialization

In order to create a new object, you use the new statement to instantiate a class: do_foo; ?>

Converting to object

If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was NULL, the new instance will be empty. Arrays convert to an object with properties named by keys, and corresponding values. For any other value, a member variable named scalar will contain the value. scalar; // outputs 'ciao' ?>

Resources
A resource is a special variable, holding a reference to an external resource. Resources are created and used by special functions.

Note: The resource type was introduced in PHP 4

Converting to resource:

As resource variables hold special handlers to opened files, database connections, image canvas areas and the like, converting to a resource makes no sense.

Freeing resources:

Due to the reference-counting system introduced with PHP 4's Zend Engine, a resource with no more references to it is detected automatically, and it is freed by the garbage collector. For this reason, it is rarely necessary to free the memory manually.

Note: Persistent database links are an exception to this rule. They are not destroyed by the garbage collector.

get_resource_type
get_resource_type — Returns the resource type string get_resource_type ( resource $handle )

The above function gets the type of the given resource.

handle - This is the evaluated resource handle.

Return Values:

If the given handle is a resource, this function will return a string representing its type. If the type is not identified by this function, the return value will be the string Unknown.

This function will return FALSE and generate an error if handle is not a resource. Example:

<?php // prints: mysql link $c = mysql_connect; echo get_resource_type($c). "\n"; // prints: file $fp = fopen("foo", "w"); echo get_resource_type($fp). "\n"; // prints: domxml document $doc = new_xmldoc("1.0"); echo get_resource_type($doc->doc). "\n"; ?>

NULL
The special NULL value represents a variable with no value(empty but not zero). NULL is the only possible value of type NULL (nothing).

A variable is considered to be null if:


 * 1) it has been assigned the constant NULL.


 * 1) it has not been set to any value yet.

Syntax
 * 1) it has been unset.

There is only one value of type null, and that is the case-insensitive keyword NULL. <?php $var = NULL; ?>

Casting a variable to null will remove the variable and unset its value.

is_null
(PHP 4 >= 4.0.4, PHP 5)

is_null — Is a function that checks to determine whether a variable is NULL

Description bool is_null ( mixed $var )

var - The variable being evaluated.

Returns TRUE if var is null, FALSE otherwise.

Example: <?php error_reporting(E_ALL); $fofo = NULL; var_dump(is_null($inexistent), is_null($fofo)); ?>

unset
unset — Unsets a given variable. The behavior of unset inside of a function can vary depending on what type of variable you are attempting to destroy.

If a globalized variable is unset inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset was called.

void unset ( mixed $var [, mixed $... ] )

unset destroys the specified variables.

<?php function destroy_fofo {   global $fofo; unset($fofo); }

$fofo = 'bare'; destroy_fofo; echo $fofo; ?>

The above example will output:

bare

To unset a global variable inside of a function, then use the $GLOBALS array to do so: <?php function foo {   unset($GLOBALS['bar']); } $bar = "something"; foo; ?>

If a variable that is PASSED BY REFERENCE is unset inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset was called. <?php function foo(&$bar) {   unset($bar); $bar = "blah"; } $bar = 'something'; echo "$bar\n"; foo($bar); echo "$bar\n"; ?>

If a static variable is unset inside of a function, unset destroys the variable only in the context of the rest of a function. Following calls will restore the previous value of a variable. <?php function foo {   static $bar; $bar++; echo "Before unset: $bar, "; unset($bar); $bar = 23; echo "after unset: $bar\n"; }

foo; foo; foo; ?>

var - is the variable to be unset.

No value is returned.

Example unset: <?php // destroy a single variable unset($foo); // destroy a single element of an array unset($bar['quux']); // destroy more than one variable unset($foo1, $foo2, $foo3); ?>

Example - Using (unset) casting: <?php $name = 'Felipe'; var_dump((unset) $name); ?>

Note: Because this is a language construct and not a function, it cannot be called using variable functions It is possible to unset even object properties visible in current context. It is not possible to unset $this inside an object method since PHP 5. When using unset on inaccessible object properties, the __unset overloading method will be called, if declared.

empty
empty — Determine whether a variable is considered to be empty

bool empty ( mixed $var )

var - is the variable to be checked

Note:

empty only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).

empty is the opposite of (boolean) var, except that no warning is generated when the variable is not set.

Returns FALSE if var has a non-empty and non-zero value.

The following things are considered to be empty: "" (an empty string) 0 (0 as an integer) 0.0 (0 as a float) "0" (0 as a string) NULL FALSE array (an empty array) var $var; (a variable declared, but without a value in a class)

In php version 5.00 and higher, Objects with no properties are no longer considered empty.

Example isset comparison: <?php $var = 0; // Evaluates to true because $var is empty if (empty($var)) { echo '$var is either 0, empty, or not set at all'; } // Evaluates as true because $var is set if (isset($var)) { echo '$var is set even though it is empty'; } ?> Note: Because this is a language construct and not a function, it cannot be called using variable functions When using empty on inaccessible object properties, the __isset overloading method will be called, if declared.

Type Juggling
PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which the variable is used. That is to say, if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, it becomes an integer.

An example of PHP's automatic type conversion is the addition operator '+'. If either operand is a float, then both operands are evaluated as floats, and the result will be a float. Otherwise, the operands will be interpreted as integers, and the result will also be an integer. Note that this does not change the types of the operands themselves; the only change is in how the operands are evaluated and what the type of the expression itself is. <?php $foo = "0"; // $foo is string (ASCII 48) $foo += 2;  // $foo is now an integer (2) $foo = $foo + 1.3; // $foo is now a float (3.3) $foo = 5 + "10 Little Piggies"; // $foo is integer (15) $foo = 5 + "10 Small Pigs";    // $foo is integer (15) ?>

To force a variable to be evaluated as a certain type, see the section on Type casting. To change the type of a variable, see the settype function section.

Note:

The behaviour of an automatic conversion to array is currently undefined.

Also, because PHP supports indexing into strings via offsets using the same syntax as array indexing, the following example holds true for all PHP versions: <?php $a   = 'car'; // $a is a string $a[0] = 'b';  // $a is still a string echo $a;      // bar ?>

Type Casting
Type casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast. <?php $foo = 10;  // $foo is an integer $bar = (boolean) $foo;  // $bar is a boolean ?>

The casts allowed are: (int), (integer) - cast to integer (bool), (boolean) - cast to boolean (float), (double), (real) - cast to float (string) - cast to string (array) - cast to array (object) - cast to object (unset) - cast to NULL (PHP 5) (binary) casting and b prefix forward support was added in PHP 5.2.1

Note: tabs and spaces are allowed inside the parentheses, so the following are functionally equivalent: <?php $foo = (int) $bar; $foo = ( int ) $bar; ?>

Casting literal strings and variables to binary strings: <?php $binary = (binary) $string; $binary = b"binary string"; ?>

Note:

Instead of casting a variable to a string, it is also possible to enclose the variable in double quotes. <?php $foo = 10;           // $foo is an integer $str = "$foo";       // $str is a string $fst = (string) $foo; // $fst is also a string // This prints out that "they are the same" if ($fst === $str) { echo "they are the same"; } ?>

settype
settype — Sets the type of a variable

bool settype ( mixed &$var, string $type )

Set the type of variable var to the specified type.

var - The variable being converted. type Possibles values of type are: "boolean" (or, since PHP 4.2.0, "bool") "integer" (or, since PHP 4.2.0, "int") "float" (only possible since PHP 4.2.0, for older versions use the deprecated variant "double") "string" "array" "object" "null" (since PHP 4.2.0)

Returns TRUE on success or FALSE on failure.

Example settype: <?php $foo = "5bar"; // string $bar = true;  // boolean settype($foo, "integer"); // $foo is now 5  (integer) settype($bar, "string"); // $bar is now "1" (string) ?>

Note: Maximum value for "int" is PHP_INT_MAX.