3 PHP Variables
3 PHP Variables
Eg
<?php
$myCar = "Honda";
echo $myCar;
?>
Output- Honda
PHP Concatenation
Eg ii (concatenate variable with string)
<?php
$myCar = "Honda City";
echo $myCar . " is riding"; // dot(.) operator is used to concatenate mycar variable
and is riding
?>
<?php
$first = 100;
$second = 200;
$third = $first + $second;
echo "Sum = ".$third;
?>
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.
<?php
$first = 1000;
$second = 500;
$third = $first - $second;
echo "Subtraction = ".$third;
?>
In the above example We perform subtraction using variables( $first, $second) with
vale=1000,500. Subtract second variable from first, result is hold by third
variable($third) . Print this third variable passing with echo statement.
<?php
$name="steve";
echo $name;
//unset( ) function destroy the variable reference.
unset($name);
?>
Output steve
In the above example declare variable $name hold value="steve". In this program we
used unset() function to delete a particular variable. first it show the output: "steve",
because we pass unset function after echo statement. Now pass variable name inside
unset($name) function output will show an Notice error(Variable is undefined).
Eg vi
<?php
$first = 100;
$second = 200;
$third = $first + $second;
echo "Sum = ".$third;
unset($third);
//after delete the variable call it again to test
echo "Sum = ".$third;
?>
Note : Trying to access or use a variable that's been unset( ), as in the preceding script,
will result in a PHP "undefined variable" error message. This message may or may not
be visible in the output page, depending on how your PHP error reporting level is
configured.
<?php
$name="rexx";
$NAME="rahul";
echo $name."<br/>";
echo $NAME;
?>
<?php
//define variables
$name = "Fiona";
$age=25;
//display variable contents
var_dump ($name);
var_dump($age);
?>
<?php
$first = 100;
$second = 200;
$third = $first + $second;
var_dump ($third);
?>
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
<?php
$bool = true;
var_dump ($bool);
?>