Introduction To PHP – 3

More About Variables

<?php

$x=5;  #this is variable x

function gajjar(){

$y=10;
echo “y is “.$y;

/*here you cant print variable x as

echo “x is “.$x; it will show undefined variable x because of its not global*/

}

gajjar();      #this is function call.
echo “<br>x is “.$x;

/*same here you cant use $y it was only limited to that function gajjar() only*/

?>

To overcome these problems see examples below you will get automatically cleared.

ex.1

<?php

$x = 5;

function gajjar() {
global $x;
$x = $x + 5;
}

GAJJAR();
echo “x is “.$x;
?>


ex 2.

<?php
$x = 5;
function gajjar() {

global $y;
$y=50;
$y = $y + 5;
}

GAJJAR();
echo “x is “.$x;
echo “<br> y is “.$y;
?>

Static variable:

<?php
function gajjar() {
static $k = 0;
echo “<br>”.$k;
$k++;
}

Gajjar();
gajjaR();   #this all are same!
GaJjAr();
?>

in short variable k stores the value of previous calls.

NOTEvar_dump($x); this inbuilt function of php returns the data type of variable.


Defining Boolean

<?php

$x = true;
$y = false;
var_dump($x);

?>

variables of different data types.

<?php

$a=5;
echo $a.”<br>”;

$a=5.5;
echo $a;

$a=”karan”;
echo $a;

?>

In php you don’t need to define variables type. PHP will automatically define weather it is integer or float or string.

Advertisements

Author: kanu_lp

Teck Geek guy... Bands Obsessed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s