codescracker
php

PHP Variables



« 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 Variables Example

Below is an example of variables in PHP language.

<html>
<head>
   <title>PHP Variables - CodesCracker</title>
</head>
<body>
<?php 
   # PHP Variables Example - CodesCracker
   $variable1 = 5;
   $variable2 = 10;
   echo $variable1;
   echo "<br/>";
   echo $variable2;
   echo "<br/>";
   echo $variable1+$variable2;
   echo "<br/>";
   echo $variable2/$variable1;
   echo "<br/>";
   echo $variable2-$variable1;
   echo "<br/>";
   echo $variable1-$variable2;
   echo "<br/>";
   $sum = $variable1+$variable2;
   echo $sum;
   echo "<br/>";
   $variable1 = "Codes";
   $variable2 = "Cracker";
   echo $variable1;
   echo "<br/>";
   echo $variable2;
   echo "<br/>";
   echo $variable1.$variable2;
   echo "<br/>";
   $fullname = $variable1.$variable2;
   echo $fullname;
   echo "<br/>";
   $my_name = $fullname;
   $my_1_name = $fullname;
   echo $my_name;
   echo "<br/>";
   echo $my_1_name;
   echo "<br/>";
?>
</body>
</html>

Here is the sample output produced by the above PHP variable example code:

php variables

PHP Online Test


« Previous Tutorial Next Tutorial »



© Copyright 2021. All Rights Reserved.

CodesCracker