Saturday, August 20, 2016

What is Variables? Types of PHP Variables.

PHP Variables


Php language have some variables. which is used in scripting. The variable start wih "$" sign here. The variable is container. If you use text variables than double quotes the value. e.g. "Hello Rahman". Example of variables "$Mashiur" and "$mashiur" are two different variable. Now I am discussing different different types of variable here -

PHP Output Variables:

The echo statement is called output variables. Because it show output data on screen.

Example of PHP Output Variables:
<html>
<body>

<?php

$variables_faruk = "HI Faruk, How are you?";
echo "Hello Faruk.  " .$variables_faruk. "  Yes I am fine";

// Or another way we will get same output

$variables_faruk = "Hi Faruk, How are you?";
echo " Hello FAruk $variables_faruk Yes I am fine";



?>

</body>

</html>


// Result is : Hello Faruk. HI Faruk, How are you? Yes I am fine

Sum of Variables


<?php

// Sum of variables

$variable1 = 100;
$variable2 = 200;
$varable3 = 300;
$variable4 = 400;
$varable5 = 500;

echo $variable1 + $variable2 + $varable3 + $variable4 + $varable5;

?>

// Result of above code is : 1500

PHP Variables Scope

The variable of PHP can be assigned any where of the script. It has different type of variable scope. E.g. 
  • local variable 
  • global variable
  • static variable
Local variable: A variable is declared within or inside of the function is called local variables 


Global variable: A variable declared outside of the functions is called global variables.  


Globas Varaible Code:

<html>
<body>

<?php

$variable_global = 500;  //global scope

function globalvariable () {

//This will show an error. That means it does not sohw any output.
echo "<p>By using variable inside will show an error : $variable_global</p>";
}

globalvariable ();

echo "<p>By using variable inside will dont show an error: $variable_global</p>";

?>

</body>
</html>


Result is :
Notice: Undefined variable: variable_global in D:\xampp\htdocs\scripthour\index.php on line 11
By using variable inside will show an error :
By using variable inside will dont show an error: 500



Local Variables

<html>
<body>
<?php
function function_local () {

$local_variables = 600; //local scope
echo "<p>This is local variable inside of function and dont show any error $local_variables</p>";

}

function_local() ;

echo "<p>This is local variable outside of function and show any error $local_variables</p>";
?>
</body>
</html>


/*Result is

This is local variable inside of function and dont show any error 600
Notice: Undefined variable: local_variables in D:\xampp\htdocs\scripthour\index.php on line 13
This is local variable outside of function and show any error */



Global Keyword

<html>
<body>

<?php
$global_variable1 = 500;
$global_variable2 = 600;
function global_variable_inside_function () {
global $global_variable1, $global_variable2;
$global_variable1 = $global_variable1 + $global_variable2;
}
global_variable_inside_function ();
echo "$global_variable1";

?>
</body>
</html>


//Result : 1100

PHP also store the global variable in array is called $GLOBALS[index]. The index is also hold the name only of a variable. The above code can be written as 

<html>
<body>

<?php

$global_variable1 = 500;
$global_variable2 = 600;
function global_variable_inside_function () {
$GLOBALS['global_variable1'] = $GLOBALS['global_variable1'] +$GLOBALS['global_variable2'] ;
}

global_variable_inside_function ();

echo "$global_variable1";

?>

</body>
</html>

//Result : 1100


PHP The Static Keyword

Generally when a function is implemented than all the value of its is deleted. Sometimes we do not want to do this. We wont delete this. To do it we need static keywords  



<html>
<body>

<?php

$static_variable = 500;
function staticvariable() {
static $static_variable = 0;
echo $static_variable;
$static_variable ++ ;
}

staticvariable();
staticvariable();
staticvariable();
staticvariable();
staticvariable();
staticvariable();
staticvariable();
staticvariable();
staticvariable();
?>

</body>
</html>


Result : 012345678

By AutogarmentAutogarment Sales all types of ERP related to garments industries. 

No comments:

Post a Comment