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.

<?php

$name = "Sajjad";

echo "Hello World!";

echo "<br/>";

echo "My name is ";

echo $name;

?>

output,
Hello World!
My name is Sajjad

PHP Varible Basic
Here is a simple math with variable,

<?php
$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
?>
Output,
15
5
50
2

PHP Varible Math

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

<?php
$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!
?>
Output,
HelloWorld! My name is Sajjad
HelloWorld!

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

<?php
$number++;
echo $number;
?>
And,

<?php
$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
Top