Thursday, February 28, 2013

Variables in PHP

Variables in PHP

  •                 All variable names should start with alphabets or underscore(_).
  •                 Variable names are case-sensitive
  •                 Variables can only have alphabets and numbers and special character like  underscore(_).
Variables starts with dollar($) sign.

     valid variable names:  $name
                                         $Address1,
                                          $Address2,
                                          $_myCity
                                           $Full_Name
                                           $my_City

PHP is a loosely coupled language.   So an variable can hold any type of data during its lifetime.
i.e same variable can be used for string, int,float etc., At runtime PHP engine determines type of the variable.

Variable  examples


$name="Peter England";  var_dump($name);  // Output: string(13) "peter england"
$_myCity="New Jersy"; var_dump($_myCity); // Output: string(9) "New Jersy"
$Full_Name="sam uncle"; var_dump($_myCity); // Output: string(9) "sam uncle"
 $2day ="saturday";var_dump($2day);// Output: Parse error: syntax error, unexpected '2' (T_LNUMBER), expecting variable (T_VARIABLE) or '$' in 

Variable shouldn't start with numbers.

Variable can hold any type of data during its lifetime.

for ex:


$mynum ="10"; // Output:string(2) "10"
$mynum = 1+1; // Output:int(2)   , in this case $mynum is an integer variable.


Tags: variables in PHP 5.4.8,PHP Version 5.4.8, variable usage in PHP, Declaring variables in PHP,
variables syntax in PHP 5.4.8, variables in PHP.

No comments:

Post a Comment