| Data types |
| There are 8 primitive data types in PHP.They are |
| 1.Boolean |
| 2. Integer |
| 3. Float |
| 4. String |
| 5. Array |
| 6. Object |
| 7. Resource |
| 8. NULL |
|
|
|
| Boolean |
A Boolean is a truth value. It can either be true or false. Both are case_insensitive. |
|
| Syntax: |
<?php
$bol= True; //assign the value true to $bol
?> |
|
Typically, some kind of operator which returns a boolean value, and the value is passed on to a control structure. |
<?php
// == is an operator which test
// equality and returns a boolean
if ($action == "show_version"){
echo "The version is 1.23";}
// this is not necessary...
if ($show_separators == TRUE){
echo "<hr>\n";}
// ...because instead, this can be used:
if ($show_separators){
echo "<hr>\n";}
?> |
|
| Convert a boolean |
| When converting to boolean the following values are considered FALSE: |
| the boolean FALSE itself |
| the integer 0 (zero) |
| the empty string, and the string "0" |
| the float0.0 (zero) |
| an array with zero elements |
| an object with zero member variables |
| the special type Null (including unset variables) |
| SimpleXML objects created from empty tags |
| Every other value is considered TRUE (including any resource). (-1 is considered as true. |
| |
<?php
var_dump((bool) ""); //bool(true)
var_dump((bool) 1); // bool(true)
var_dump((bool) -2); // bool(true)
var_dump((bool) "foo"); // bool(true)
var_dump((bool) 2.3e5); // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array()); // bool(false)
var_dump((bool) "false"); // bool(true)
?> |
|
|
|
| Integer |
An integer is a set of number. For eg:- {-2, -1,0,1,2,3,4,5...}
|
| Syntax: |
| Integers can be specified in decimals, hexadecimals, octal notation etc. |
<?php
$dec = 144; // decimal number
$neg = -125; // a negative number
$octal = 0123; // octal number (equivalent to 83 decimal)
$hex = 0x1A; // hexadecimal number (equivalent to 26 decimal)
?> |
|
| Converting to integer |
Convert a value to integer, use either the (int) or (integer) casts. However, 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. |
| Floating point numbers |
Floating point numbers (also known as "floats", "doubles", or "real numbers") can be specified using any of the following syntaxes: |
<?php
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?> |
|
|
|
| Converting to float |
Converting strings to float, see String conversion to numbers. For values of other types, the conversion is performed by converting the value to integer first and then to float. See Converting to integer for more information. As of PHP 5, a notice is thrown if an object is converted to float. |
| String |
A string is a series of characters. A character is same as that of a byte. ie., there are 256 different characters are possible. PHP does not support unicode. There are 2 basic unicode functionalities. They are utf8 encode() and utf8 decode(). |
PHP imposes no boundary on the size of a string; the only limit is the available memory of the computer. |
| Syntax: |
| A string literal can be specified in four different ways: |
|
|
|
|
To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash before a single quote, or at the end of the string, double it (\\). Note that attempting to escape any other character will print the backslash too. |
<?php
echo 'this is a simple string';
echo 'You can also have embedded newlines in
strings this way as it is
okay to do';
// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';
?> |
|
|
|
| Array |
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional array are also possible.
Explanation of those data structures is beyond the scope of this manual, but at least one example is provided for each of them. For more information, look towards the considerable literature that exists about this broad topic. |
| Syntax: |
An array can be created by 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 and string
//value may be any value of any type. |
|
|
|
<?php
$arr = array("foo" => "bar", 12 => true);
echo $arr["foo"]; // bar
echo $arr[12]; // 1
?> |
|
| Objects |
| To create a new object, use the new statement to instantiate a class: |
<?php
class foo{
function do_foo()
{
echo "Doing foo.";
} }
$bar = new foo;
$bar->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. |
<?php
$obj = (object) 'ciao';
echo $obj->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. See the appendix for a listing of all these functions and the correspondingresource types. |
| Converting to resources |
As resource variables hold special handlers to opened files, database connections, image canvas areas and the like, converting to a resource makes no sense. |
| NULL |
The special NULL value represents a variable with no value. NULL is the only possible value of type NULL. |
| Syntax: |
There is only one value of type null, and that is the case-insensitive keyword NULL. |
|
|
|