Share on Google+Share on Google+

PHP Numbers Variables

Advertisement
We already define that variables are used to store the values or information in the form text strings, numbers or array. A variable in PHP always recognized with $sign.

PHP Variables Numbers

We already define that variables are used to store the values or information in the form text strings, numbers or array. A variable in PHP always recognized with $sign. After declaring a variable once in your script you can used again and again. 

You can also assign numbers in PHP variable or you can also perform mathematics by assigning values in the variable.

Let's see the example :

PHP Number Variables Example 1 :

<?php

$num = 10;

echo "The number is : ".$num;

?>

The output of the above example is : 6

In the above example, I assign the value of  variable $num as 6 and print on the browser by using echo command.

Let's see more example to understand how to perform mathematics using PHP variable :

Example 2 :

<?php

$num1 = 10;

$num2 = 20;

$sum = $num1 + $num2;

echo "The sum of two number is : ".$sum";

?>

Example 3 :

<?php

$num1 = 20;

$num2 = 10;

$subtract = $num1 - $num2;

echo "The sum of two number is : ".$subtract ;

?>

Example 4 :

<?php

$num1 = 20;

$num2 = 10;

$multiply = $num1 * $num2;

echo "The sum of two number is : ".$multiply ;

?>

Example 5 :

<?php

$num1 = 20;

$num2 = 10;

$division = $num1/ $num2;

echo "The sum of two number is : ".$division ;

?>

By using appropriate mathematical operator you can do any mathematical queries in php.

Advertisements

Posted on: January 14, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.