Website Templates Search

Variables basic in Php

  • Click to view live demo By Admin
  • Click to view live demo Aug 26, 2017

Variable in PHP is used to store a value in memory.
We can store integer, strings, characters, decimals in memory by using variables in PHP.

There are some rules to create variables.

RULES:

1. Variable in php is declared with a $ sign followed by variable name.
(e.g $a=10 where a is a variable which holds the value 10)
2. Variable names in php are case-sensitive(e.g. $A and $a both are different).
3. Variable name can not start with a number.
4. Variable name must be start with a letter or underscore ( e.g. A-z, 0-9, and _).
5. After first character, for other characters we can use letters, numbers and underscores.
6. Variable name can not contain spaces.

Example:

<?php  
  
    $variable    = 1;            // valid name
	$variablePhp = 1;            // valid name
	
	//$12php       = "variables";   // not valid name
	// Parse error: syntax error, unexpected '12' (T_LNUMBER), 
	//expecting variable (T_VARIABLE) or '$' 

	$_variable   = 1;            // valid name
	$php_variable= 2             // valid name
	
?>

Declaring, assigning values in variables.

Example:

<?php 

    $integer_Variable = 0;                          // Integer
    $string_Variable = "Year to Date"     // String
    $boolean_Variable = true;                       // Boolean
    $decimal_Variable = 100.25;                     // Double
    $array_Variable = array(250, 300, 325, 475);    // Array

?>

Example:

<?php
 
    $a = 6;
    $b = 4;
    $result = $a - $b;

?>

Display Variables Value :

Use print or echo to display variable on screen.

Example:

<?php

  $variableName = "PhpOnWeb";
  print $variableName;
  
  // Display with double quote
  print "$variableName Example"
  
  // Display with single quote 
  print $variableName .' Example'
  
?>

Variable Scope In Php :

We can declared variable anywhere in php script.
There are three type of varible scope in php.

1.   Local Variables
2.   Global Variables
3.   Static Variables

Now we are describing these variable scopes.

1.  Local Variables

Variable defined inside function are called Local Variables in function scope.
Local Variables accessible only in that function, outside the function not accessible.

Example:

<?php
 
    // This variable is global scope for this script but not accessible inside function
    $variable = "Scope";   
	
	function foo()
	{
	    // local variable scope inside function 
		$variable = "Example";    
		print "Variable Inside Function: ".$variable;
	}
	
	print "Variable Outside Function: ".$variable;
	
	foo();  // function called
	
	print "Variable Outside Function: ".$variable;
	
?>


------------ ****** ------------
OUTPUT:
        Variable Outside Function: Scope
        Variable Inside Function: Example
		Variable Outside Function: Scope
		

2. Global Variable

Variables defined in script is called global variable, we can access variable value inside anywhere in script but not inside function.
If we want to access global variable inside function then declared variable name with keyword global.

Example:

 <?php 

		$var1 = 2;
		$var2 = 3;

		function Add()
		{
			global $var1;  // Access global variable
			global $var2;  // Access global variable

			$var2 = $var1 + $var2;
		}

		Add();

		print "Global Variable value : " . $var2;

?>


------------ ****** ------------
OUTPUT:
         Global Variable value : 5

Example:

<?php 

	$var1 = 2;

	function AddGlobalLocal()
	{
		global $var1;  // Access global variable
		$var2 = 3;     // local variable inside function scope

		$var2 = $var1 + $var2;
	}

	AddGlobalLocal();

	print "Global Variable value : " . $var1;
	print "Local Variable value : " . $var2;

?>

------------ ****** ------------
OUTPUT:
        Global Variable value : 2
        Local Variable value : 0

3.  Static Variable

Static Variables create separate memory space  for defined scope.
If static variable is defined in function then it will preserve value in function scope.
If static variable is defined in global scope then it will preserve value in script ( php file ) scope.

Example:

<?php 

	function StaticAdd()
	{
	     // Static variable inside function scope
		 static $variable = 1;  
		 print "<br>Static Variable value inside function : " . $variable;
		
		// Increment value of static variable
		$variable = $variable + 1;
		
	}

	StaticAdd();
	StaticAdd();
	StaticAdd();
	print "<br>Static Variable value : " . $variable;

?>

------------ ****** ------------
OUTPUT:
        Static Variable value inside function : 1
		Static Variable value inside function : 2
		Static Variable value inside function : 3
		Static Variable value : 0

Example:

 <?php 
    
	// Static variable inside current script scope (global scope)
	static $var1 = 1; 

	function StaticGlobalAdd()
	{
		 global $var1;
		 print "<br>Static Variable value inside function : " . $var1;
		// Increment value of static variable
		$var1 = $var1 + 1;
		
	}

	StaticGlobalAdd();
	StaticGlobalAdd();
	StaticGlobalAdd();

	print "<br>Static Variable value : " . $var1;

?>

------------ ****** ------------
OUTPUT:
        Static Variable value inside function : 1
		Static Variable value inside function : 2
		Static Variable value inside function : 3
		Static Variable value inside function : 4
		Static Variable value : 4