Pages

Saturday, September 1, 2012

PHP DATA TYPES

PHP has been 8 different data types

Four scalar types:-

(Boolean, integer, float, string)

Two compound types:

(array, object)

And finally two special types:

(Resource, null)

Note (to check the variable type and value use php function var_dump )

Top of Form

Bottom of FormBooleans

The Boolean data type variable has only two state either it cab a true or false. Both values are case
insensitive.

Example

$var = True;

$x = false;

When
become the 1.



Integers:-

An integer is a number which belongs to set Z = {..., -2, -1, 0, 1, 2…}

Integer can be a decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) and
optionally by sign (- or +)

Example:-

$a
$a = -123; // a negative number

Hexadecimal number

To

Example:-

$var = 0x1A; // hexadecimal number (equivalent to 26 decimal)

Octal number

To

Example:-

$var = 0123; // octal number (equivalent to 83 decimal)



Float:-

A number who has a decimal point in the value is called float number

$a = 4.45656

$f = 0.03;

String:-

String
character.

Example

$a = 'this is a string';

$a = 'my house number is E/131 St. 45';

We can specify the string in 4 ways:-

(single

Single quoted:-

In single quoted we enclose the string in single quote “’”. To specify the
single

Example:-

$a = 'this is a string';

$a = 'Jawad once said: "I\'ll be back"';

Double quoted:-

In double quoted we enclose the string in double quote ‘”’. The beauty of
Double quotes is it show the php variable value in the string.

Example:-

$a = “double quotes string”;

$b = “the variable a = $a”; //output: the variable a = double quotes string

Heredoc syntax:-

In Heredoc you write your own identifier. After ‘<<<’ put your own identifier
then in the next line put your string data which you have. In the end of the
string put same your identifier to close/end the string.

Example:-

$str = <<<ABC
Example of heredoc
vffkg lglkkg okpopo
fd
ABC;

It behaves like a double quote string. It also interpreted the variable value
in the string as double quote string. Its good when you write the html and
php

Example

echo $str = <<<ABC

<b style="color:red">Its red color string</b>

ABC;

Nowdoc sytax:-

Nowdoc behave like a single quotes. Its syntax almost like a heredoc except
you

Example

$str = <<<'XYZ'
Example of nowdoc
anning muiple les
uig nowdoc snta.
XYZ;

Array:-

Array has a collection of different value and every value has its own key. In
other

Example:-

array(

    key  => value,

    key2 => value2,

    key3 => value3,

    ...

)

The key can either be an integer or a string.  The value can be of any type

Note

Object:-

Object is a instance of class who has the all properties and function of the
class.

Example

class test
{
    function do_test()
    {
        echo "Doing test.";
    }
}

$b = new test;
$b->do_test();

Resources:-

Resource is a special type of variable those holding the external resource
information like file handling, database connections, image canvas areas etc

Null:-

Null is special type of variable. Null mean the variable has initialize but
has no value.

Example

$a = NULL;

0 comments:

Post a Comment