VARIABLES IN PHP:

 VARIABLES IN PHP:

PHP VARIABLES:

A variable in PHP is a name of memory location which holds data. Variable has a unique address. the variable is declared by $ sign followed by the variable name.

Characteristics

  • All variables are denoted with the dollar sign ($) before the variable name.
  • The value is stored in the variable by the assignment operator.
  • Variables are assigned with the = operator, with the left-hand side and the expression to be evaluated on the right.
  • Do not need, to be declared variable before assignment.
  • Variables hold any types of values it may be int, char, float, etc;
  • Automatically Variables can convert one data type to another.

Rules for PHP variables:

  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive ($age and $AGE are two different variables)

Syntax of declaring a variable:

$variablename=value;

 <?php

$x = 5;

$y = 4;

echo $x + $y;

?>

 Output:9

 

Assigning Values to Variables

Assigning a value to a variable in PHP by equality(=) symbol, assignment operators.

This assigns the value on the right side of the equation to the variable on the left.

<?php

   $myCar = "Honda";

   echo $myCar;

?>

 Output Honda


Destroying PHP Variables

To destroy a variable, pass the variable to PHP's unset( ) function. as in the following

Syntax :- unset ($variable_name);

example:

<?php

 $name="abc";

 echo $name;

 //unset( ) function destroy the variable reference.

 unset($name);

 ?>

Output abc

 PHP variable name is case-sensitive

Php make difference in between capital and small letter of same alphabets (A and a are different .

example:

<?php

$a = 'Welcome SJKPGM';

echo "Value of a: $a";

echo "Value of A: $A";

?>

Output:-

Value of a: Welcome SJKPGM

Value of A:

 

PHP is a loose type language

In another language such as C, C++, and Java the programmer must declare the name and data type of the variable before using it. 

In PHP the data type of the variable does not essential to be declared before use it  because types are associated with values rather than variables so As a result, a variable can change the type of its value.

<?php

$height = 3.5;

$width = 4;

$area=$height*$width;

echo "Area of the rectangle is : $area";

?>

Output:- Area of the rectangle is 7.5

 

PHP variables: Assigning by Reference

PHP4 assigns values to variables by reference. This means that the new variable simply points to the original variable. Changes to the new variable affect the original, and vice a versa.

Consider the following example:

<?php

$a=101;

$b= & $a;

$b="my $b";

echo $b;

echo '<br />';

echo $a;

?>

Output:

my 101

  my 101

 

Variable Scope

The scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types −

·        Local variables

·        Function parameters

·        Global variables

·        Static variables

The scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types −

 

Local Variables

A variable declared in a function is considered local; that is, it can be referenced only in that function. Any assignment of this variable outside of a function will be considered as a different variable.
<?php
   $a = 5;
   
   function abc () { 
      $a = 10;
      print "\$a inside function is $a. <br />";
   }
   abc ();
      print "\$a outside of function is $a. <br />";
   
?>
This will produce the following result −
$a inside function is 10. 
$a outside of function is 5. 
 
Function Parameters
Function parameters are declared after the function name and inside parentheses. They are declared much like a typical variable would be −
<?php
   // multiply a value by 10 and return it to the caller
   function multiply ($value) {
      $value = $value * 10;
      return $value;
   }
   
   $retval = multiply (10);
   Print "Return value is $retval\n";
?>
This will produce the following result −
Return value is 100

Global Variables

a global variable can be accessed in any part of the program. It is used by placing the keyword GLOBAL in front of the variable that should be recognized as global. Placing this keyword in front of an already existing variable tells PHP to use the variable having that name. Consider an example −
<?php
   $somevar = 15;
   
   function addit() {
      GLOBAL $somevar;
      $somevar++;
      print "Somevar is $somevar";
   }
   
   addit();
?>
This will produce the following result −
Somevar is 16

Static Variables

a static variable will not lose its value when the function exits and will still hold that value should the function be called again.
You can declare a variable to be static simply by placing the keyword STATIC in front of the variable name.
<?php
   function keep_track() {
      STATIC $count = 0;
      $count++;
      print $count;
      print "<br />";
   }
   
   keep_track();
   keep_track();
   keep_track();
?>
This will produce the following result −
1
2
3
==================================================================
 

Post a Comment

0 Comments