PHP variables


Variable and PHP variable usage

Variables are the container that are used to store the information . We temporarily uses the variable container to store information. Suppose we have basket. We put some stuff in the basket. Then later we will take the stuff directly from basket , the same process is with variable container. In different languages there are compulsory data type with the variable is required. E.g in C we want to store number in variable my_variable . Then it is compulsory to write the integer datatype with variable my_variable. How ever good advantage in PHP, there is no compulsory check to write the datatype with variable. Any variable can store any type of data .

PHP Variable for storing a sentence

We are going to store a sentence into the PHP variable and then we will display this sentence into the paragraph tags

<html> <head> <title>PHP variable Usage </title> </head> <body> <p> <?php $my_first_varaible = "The quick brown fox jumps over the lazy dog"; echo $my_first_varaible; ?> </p> </body> </html>

PHP Addition using variables

<?php $var_a = 10; $var_b = 10.5; $sum = $var_a $var_b; echo "Sum of two number is : "; echo $sum; ?>Output: Sum of two number is : 20.5

Here we took two variables $var_a and $var_b and insert their sum into the third variable $sum. Then we display the result from the variable $sum.

Chapter Next »