PHP Variables

In PHP all the variables begin with a dollar sign $ and the value can be assigned using the = operator. But dollar sign is not technically part of the variable name!

Php variables must

  • start with a letter or underscore "_".
  • be only comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
  • be separated with underscores or differentiated with capitalization if variables consist of more than one word.

<?php

   $variable = 7;

   // assign 5 to the variable


   $_variable = 10;

   // assign 5 to the _variable


   variable = 5;

   // If you forget that dollar sign at the beginning, it will not work.


   $my variable = 5;

   // it will not work  (sperated with spaces)


   $1variable = 15;

   // it will not work.  (start with number)



?>

In Php variable names are case-sensitive, so use the exact same capitalization when using a variable. The variables $a and $A are different variables.

<?php

   $a = 5;

   $A = 5;

   echo $variable;

   // output 5


   echo $Variable;

   // output 15


?>

Data types in Php which we use to construct our variables:

  • Integers - numbers, without a decimal point
  • Doubles - floating-point numbers
  • Booleans - have only two possible values , true or false
  • NULL - this is a special type that only has one value
  • Strings - sequences of characters
  • Arrays - collections of other values
  • Objects - instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.
Note:

In PHP we don't need to specify the variable type, as it takes the data type of the assigned value.

<?php

   $int  =  5;

   // This variable is integer 


   $double = 5.34;

   // This variable is double


   $string = 'Hello W3docs';

   // This variable is string


   $array = array($int, $double, $string);

   // This variable is array



?>

PHP automatically converts types from one to another when necessary.

The value of a variable is the value of its most recent assignment.

<?php

   $int  =  5;

   // This variable is integer 


   $double = 5.34;

   // This variable is double


  $double = 5;

  // Now This variable is integer, and value of $double is 5



?>

Variable Scope

Scope of variable can be defined as the scope where variable keep its value.

PHP variables can be one of those four scope types:

  • Local variables
  • Function parameters
  • Global variables
  • Static variables

Local variables

The variable that defined in function, (we will discuss the functions in the next lessons), considers local, that is to say: variable keep its value only in that function!

<?php

   $x = 4;

   function myFunction () 
   {

      $x = 0;

      echo  "variable x inside of function is $x" ;

   }

   myFunction();

   echo  "variable x outside of function is $x" ;
?>

As a result we get:

   $x inside function is 0.
   $x outside of function is 4.

Function parameters

Function parameters are defined after the function name and inside brackets.

<?php

   function myFunction ($variable)
   {

      $variable = 10;

      return $variable;

   }

   echo $variable ;

   // it will not work  ($variable is visible only in function)


   $newVariable =  myFunction();

   echo  $newVariable ;

   // output 10

?>

Global Variables

In contrast to local variables, a global variable can be accessed in any part of the program. To be modified a global variable, it must be declared to be global in the function in which it is modified. For it we place the keyword GLOBAL in front of the variable that should be recognized as global.

<?php

   $variable = 5;


   function myFunction () 
   {

      GLOBAL $variable = 10;

      echo $variable ;

      // output 10


   }

   myFunction();

?>

Static variables

In contrast to the variables declared as function parameters, which are destroyed on the function's exit, a static variable will keep its value when the function exits .

<?php


   function myFunction () 
   {

      STATIC $variable = 0;

      $variable ++ ;

      echo $variable ;

   }

  myFunction();

  // output  1


  myFunction();

  // output  2


  myFunction();

  // output  3


?>

Beyond the variables that we declare in our code, PHP has a collection of variables, which are system defined variables and are accessible from anywhere inside the PHP code.

All of these variables are stored by PHP as arrays. Some we can get by the name of the index position as a variable name. Other can only be accessed through their arrays.

Some of that variables :

  • $_SERVER - Contains information about the server and the HTTP connection.
  • $_COOKIE - Contains any cookie data sent back to the server from the client.
  • $_COOKIE - Contains any cookie data sent back to the server from the client.
  • $_GET - Contains any information sent to the server as a search string as part of the URL.
  • $_POST - Contains any information sent to the server as a POST style posting from a client form.
  • $_FILE - Contains information about any uploaded files.
  • $_FILE - Contains information about environmental variables on the server.

<?php

   $cookie   =   $_COOKIE[" myCookie "];

   // return cookie with name 'myCookie'


?>

More details, you will learn in the following lessons.