Thursday 10 December 2015

PHP Variables

Variables in PHP
     Variable is nothing it is just name of the memory location.
A Variable is simply a container i.e used to store both numeric and non-numeric information.
Rules for declaring Variable:
Variables in PHP starts with a dollar($) sign, followed by the name of the variable.
The variable name must begin with a letter or the underscore character.
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
A variable name should not contain spaces


Creating (Declaring) PHP Variables
A variable is created the moment you assign a value to it:
Example:
<?php
  $myCar = "Honda";
  echo $myCar;
?>
Output : Honda
In the above example:
Create a variable ($mycar)containing a string with value=”Honda”.
To print the carname pass $mycar inside echo statement.
 
Concatenation variable with string
<?php
  $myCar = "Honda City";
  echo $myCar." is riding";
 
       ?>
     Output: Honda City is riding
 
In the above example
V
ariable($mycar) hold value=”honda city”. Now we wants to concatenate variable with string.
pass this variable($
mycar) inside echo statement.
To concatenate this with a string(“is riding”) use dot(.) between variable name and string.
The output will be displayed : Honda City is riding
Sum of two numbers
<?php
  $first = 100;
                $second = 200;
                $third = $first + $second; 
  echo "Sum = ".$third;
  ?>
    Output: Sum = 300 
In the above example
Declare $first , $second variable with value=100, 200 respectively.
Now add these two numbers using arithmetic operator (“+”).
sum of these two variable result stored in a third variable($sum).
Now print the sum passing ($third) with echo statement with a string.
Check Variable Contents(what data type PHP variables contain)

For String value
<?php
  $myCar = "Honda City";
  var_dump ($myCar);
  ?>
  Output : string 'Honda City' (length=10)
In the above example
We use
var_dump( ) function to check the Contents(property) of variable, $mycar hold value =”Honda City”.
Now pass this variable inside
var_dump($mycar) function .
It show all information related this(data type of variable, length(only string), value)
 For Integer values
<?php
  $first = 100;
          $second = 200;
                $third = $first + $second;
  var_dump ($third);
  ?>
     Output : int 300
For Boolean value
<?php 
  $bool = true;         
  var_dump ($bool);
?> 
Output : Boolean true 
In the above variable
$
bool with value=”true”. var_dump($bool) function is used to display the result
so output will display Boolean true, because variable holds a Boolean value

For Floating value
<?php
  $first = 100.5;
          $second = 200.2;
                $third = $first + $second;
  var_dump ($third);
  ?>
  Output :  float 300.7 
In the above example
variables first hold $first=100.5, second hold $second=200.2.
Now add these two values, result is stored in third variable($third).
Pass this variable inside
var_dump($third) to check the content.
output will float 300.7