codescracker


php

PHP Variable Types



« Previous Tutorial Next Tutorial »


Variables in PHP are used to store information inside it.

PHP 5 Variables

You can declare or create variable in PHP with a short name like a, b, c, x, y, or long name (descriptive) name such as total_money, age, student_age, like this.

PHP Variable Declaration Rules

Here are the rules to be followed before declaring variables in PHP:

Create/Declare PHP Variables

To know, how to create variables in PHP, then here is an example, uses some PHP variables:

<!DOCTYPE html>
<html>
<body>

<?php
$txt = "Hello Browser!";
$x = 50;
$y = 100.5;

echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>

</body>
</html>

Here is the output produced by the above PHP scripts:

PHP Variables

PHP Output Variables

You can use PHP echo statement to output variable's value on the screen. Here is an example:

<!DOCTYPE html>
<html>
<body>

<?php
$txt = "codescracker.com";
echo "All loves $txt";
?>

</body>
</html>

It will display the following in the browser:

PHP Output Variables

The following example will produce the same output as the example above:

<!DOCTYPE html>
<html>
<body>

<?php
$txt = "codescracker.com";
echo "All loves " . $txt;
?>

</body>
</html>

It will display the same output as above:

PHP Output Variables

Look at the following example which will output the sum of two variables:

<!DOCTYPE html>
<html>
<body>

<?php
$x = 50;
$y = 40;
echo $x + $y;
?>

</body>
</html>

It will display the following:

PHP Variables Summation

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test