PHP Variable

Variable can be access by dynamic variable name and variable can store another variable

You can make variable in PHP by $ ( dollar) sign 

Example

output

simple php coder

another example of php code for  beginer

Example

output

17

Tips : Variable name  letter , character , _variableName

Tips :  PHP variable are case sensitive $int and $INT  both are different variable

PHP Variable

1 - global variable

2 - local variable

3 - static variable

Global (global)  -Which variable decelare outside the function is global variable

NOTE : Generally  you can not access global variable inside the function

Tips : If you want to access global variable inside the function then you should use global keyword  before variable

Example

PHP global variable

<?php

$x = 5;  // This is global variable

$y = 8; //This is also global variable

echo $x ; // no error 

function myFunction(){

echo $x;

}

myFunction();  // it generate error becuase $x is global variable

?>

Use global variable inside the function

<?php

$x =5;

$y = 8;

function myFunction(){

global $x;

echo $x;

}

myFunction();

?>

J

local variable - Which variable declared inside the function is local variable and the scope of local variable is inside the function

Tips : If you want to access local variable outside the function you need to return keyword

Example

local variable example

<?php

function myFunction(){

$x = 12;

return $x;

}

$out = myFunction();

echo $out;

?>

J

static variable

When you want to not change variable value after increament then you can use static variable

Example

<?php

function myFunction(){

$a = 0;

echo $a;

$++;

}

// after first calling of function the value of $a = 1;  deu to static variable;

myfunction(); 

echo "<br>";

myFunction();

echo "<br>";

myFunction();

?>

Result

0

1

2