
For any programming language variables are the bread and butter of every. PHP also have variable features. variables help you store and retrive data. PHP supports a number of different variable types like integers, floating point numbers, strings and arrays.
PHP variables rule:
- All variables start with a dollar sign $
- PHP variables name must start with a letter or underscore.
- PHP variables may only be contain alpha-numeric characters and underscores.
- PHP variable are case-sensitive.
Below examples will help you understand PHP variables:
$name = 'Ravi Shekhar' // valid variable $3name = 'Saurav Kumar'; // invalid variable as it starts with a number $_3name = 'Vikash Singh'; // valid variable as it starts with an underscore
More PHP variables example:
$add = 2 + 2; // Assigns a value of 2 $subtract = 2 - 1; // Assigns a value of 1 $multiply = 5 * 2; // Assigns a value of 10 $divide = 10 / 2; // Assigns a value of 5.
Now let’s use PHP variables to add two numbers.
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP variable to add two numbers</title>
<style type="text/css">
body{
margin: 0 auto;
width: 980px;
}
</style>
</head>
<body>
<h3>Use of variables to add two number</h3>
<?php
$a = 10; // this is first valiable holding value of 10
$b = 5; # this is second variable holding value 5
$c = $a + $b; // This is third variable holding addition of a and b
echo $c; // this will print value of variable c
?>
</body>
</html>