Pages

Saturday, September 1, 2012

PHP VARIABLES

In this tutorial we are going to discuss about variable. We can distribute the variable in the following
categories:-

Basics

Predefined Variables

Variable scope

Variable variables

Basic:-

In PHP we define the variable with the ‘$’ sign. The variable name is case sensitive. A variable name
always starts with the letter and underscore.

Example

$a = 'Mike';
$a = 'John';

Predefined Variables:-

PHP provide the predefined variable. Mostly these variables are globally accessible.

Example:-

Superglobals — Superglobals are built-in variables that are always available in all scopes

$GLOBALS — References all variables available in global scope

$_SERVER — Server and execution environment information

$_GET — HTTP GET variables

$_POST — HTTP POST variables

$_FILES — HTTP File Upload variables

$_REQUEST — HTTP Request variables

$_SESSION — Session variables

$_ENV — Environment variables

$_COOKIE — HTTP Cookies

Variable scope:-

Variable scope means the context in which variable accessible. We are trying to explain the scope with
some example:-

Example:-

$a = 1;
include 'b.inc';

In the above example ‘$a’ is global or accessible for include file.

$a = 1; /* global scope */

function test()
{

$a=5;

echo $a; /* reference to local scope variable */

}

test();

echo $a;

Above
the above variable ($a = 1) scope outside the function. It’s not effective
from function variable.

If you want to use the global variable with in function then you need to use
the

$a = 1;
$b = 2;

function Sum()
{
    global $a, $b;

    $b = $a + $b;
}

Sum();
echo $b;

The above result is ‘3’.

Variable variables:-

Some time we need variable variables for dynamic uses. First we take a normal variable. After assign
the value we again assign the value but this time we use ‘$$’ with the variable.

Example:-

$a = 'hello';

$$a = 'world';

echo

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;

OUTPUT THE DATA IN PHP

In PHP for output the string, variable, objects, array we use different function like echo, print, print_r,
var_dump. In this tutorial I am trying to elaborate the each function with suitable example.

Echo

The beauty o f Echo it not only uses as a function we can also use it as a constructor without
parenthesis.

Function example:-

echo ("echo function call");

In Echo when we pass the argument in parenthesis then it become function as show above in example.

The return type of Echo function is Void. It is a slightly faster than print function.

Constructor Example:-

If you want to pass more than one parameter then we can also use Echo but for this we should pass the
parameters without parenthesis.

Constructor Example:-

echo "construct 1 param","construct 2 param","construct 3 param";

In constructor we concatenate the parameter with semi colon “,”.

Echo has one more form from which we can out put the string. We can say it’s a shortcut to output the
data. For this it’s necessary to “short_open_tag” configuration option enabled. In short form you can
immediately follow the opening with equal sign.

Shortcut Example

<?=”it is a shortcut example ”?>

Print:-

Print can be also used as a function as well as a constructor (without parenthesis). But we can pass only
one parameter. Beauty of Print function/constructor is it returns the 1 on success.

Function Example:-

print("print function call");

Constructor Example:-

print "print constructor call without parenthesis ";

It can also be used as an expression

if ((print "show value1") && (print "show value2"))

$var = print "Hello World"; //on successful print $var =1

$x ? print "true" : print "false";

Print_r:-

It uses to show the data/result of string, array and object as a human readable form. It’s best for
debugging the code.

Example:-

$a = array ('a' => 'ant', 'b' => 'bike', 'c' => array ('i', 'j', 'k'));
print_r ($a);

Result:-
<pre>
Array
(
[a] => ant
[b] => bike
[c] => Array
(
[0] => i
[1] => j
[2] => k
)
)
</pre>

If you want to capture the output of print_r rather than output of the data
then pass the second parameter with ‘True’ value

$var = array ('m' => 'mango', 'helo' => 'world', 'x' => array ('i', 'j', 'k')
);
$results = print_r($var , true); // $results now contains output from print_r

Var_dump:-

This function show all the information related to variable like value and type etc. The variable
may be a string, int, array, objects. This function has Void return type

$a = array(1, 2, array("a", "b", "c"));
var_dump($a);

Result:-

array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
array(3) {

[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"

}

}

Note:-

Please provide your feedback regarding this tutorial. If you have any confusion or feel some thin wrong
then please let us know. We are here for you !!!!