PHP Variables

PHP Variables are working like containers to store the important and useful information in it. So, to store our needed information in a memory space is necessary and we use PHP language variables for this purpose.

There are some rules for PHP language variables which are given below:

  • A variable of PHP language starts with the $ sign and just after that the name of variable comes.
  • The name of a variable cannot be start with a number.
  • The name of variables are also case-sensitive means ( $name and $NAME will consider to be two different variables )
  • A variable name will always be start from a letter or an _Underscore.
  • You can declare the variable but it is not necessary to declare them before assignment.

Here is an example of a variable in PHP language.

<?php
$a = 6;
$b = 12.5;
$name = "My name is Ali";
echo $a;
echo "<br>";
echo $b;
echo "<br>";
echo $name;

?>
php variables output

Types of PHP Variables

There are different data types of a Php variable which are:

Integers Type variables

Integers are actually the whole numbers in which no decimal included. They can be both positive and negative. There should be at least one digit in an integer.

<!DOCTYPE html>
<html>
<body>

<?php  
$x = 6654;
echo $x;
?>  

</body>
</html>

Float/Double Type variables

A float or named as double is a floating point number in exponential point or with a decimal point. These numbers are like 10.345 and 49.5 etc.

<!DOCTYPE html>
<html>
<body>

<?php  
$y = 49.5;
echo $y;
?>  

</body>
</html>

Strings Type variables

Strings are normally a sequence of characters that can be placed in single or double quotes. A proper example to explain strings is given below:

<!DOCTYPE html>
<html>
<body>

<?php 
$city = "This is my city";
$language = 'You are here to learn PHP language';

echo $city;
echo "<br>";
echo $language;
?>

</body>
</html>

Boolean Type variables

A Boolean is actually a condition-based situation in which there are two values true or false. These are the conditions normally used in a Boolean situation.

$x = true
$y = false

Null Value in PHP

Null is actually a datatype in which no value exist except NULL means if you created a variable and doesn’t assign any value to it then it gets NULL value automatically.

$a;
$b = 7007.70;
$b = null;

Here we don’t assign any value to $a that means it gets a NULL value by default. But in $b, we assign two values that means we can assign a NULL value to a variable.

Arrays in PHP

Arrays are the data structures that helps you to store multiple values in one variable.

<!DOCTYPE html>
<html>
<body>

<?php  
$list = array("Ali","Hamza","Sufyan");
echo $list[0];
echo "<br>";
echo $list[1];
echo "<br>";
echo $list[2];
?>  

</body>
</html>

There are two more data types with arrays which we will discussed in advanced PHP.