Get complete CodeIgniter blog project for free!

Downloaded more than 161 times, this is full project must-study for serious developers!

By submitting this form, you agree to our Terms of Service and Privacy Policy.

PHP Variable

A variable like a pot in PHP. We can hold many data using a variable. Like a text string "Hello World" or Integer value like 500. We can use a variable in the whole script if we set variable value top in the script. We don't need to use value repeatedly if we use variable. Here are some rules to declare a variable.

1. In PHP variable must start with "$", otherwise, it'll not work.
2. Variables are case sensitive. $the_variable & $The_Variable is not same.
3. You must start a variable with letter or "_" (underscore). Any number isn't accepted starting in a variable.
4. PHP accepts alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ in a variable.
5. PHP doesn't accept space in a variable. example- '$helloworld', '$HelloWorld', '$Hello_World' is correct variable but '$hello world' isn't a correct variable.
Here are some example and discussion about PHP variable. Let's see how we write or declare a variable in the script.


$name = "Sajjad";

echo "Hello World!";

echo "<br/>";

echo "My name is ";

echo $name;


Hello World!
My name is Sajjad

PHP Varible Basic
Here is a simple math with variable,

$FirstNumber = 10;
$SecondNumber = 5;

echo $FirstNumber+$SecondNumber; //Summation Output 15
echo "<br/>";
echo $FirstNumber-$SecondNumber; //Subtraction Output 5
echo "<br/>";
echo $FirstNumber*$SecondNumber; //Multiplication Output 50
echo "<br/>";
echo $FirstNumber/$SecondNumber; //Division Output 2

PHP Varible Math

You can join to many variables using (.) dot. Here is an example

$a = "Hello";
$b = "World! ";
$c = "My name is ";
$d = "Sajjad";

echo $a.$b.$c.$d; // Output Hello Wordl! My name is Sajjad
echo '<br/>';
echo $a.$b; // Output Hello Wordl!
HelloWorld! My name is Sajjad

PHP Variable Join
If you set your variable value empty then PHP detect it default value 0 and count this variable value 0
example -

echo $number;

$number = $number+1;
echo $number;
both are executed 1 as their result.
that means PHP execute result with the default value.

Search on PHPAns
Subscribe to PHPAns
Join with our 32000+ subscribers and get our latest articles in your email inbox for free.
Delivered by FeedBurner
We always respect your privacy and take protecting it seriously.
Connect Social With PHPAns