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 "My name is ";
$FirstNumber = 10;
$SecondNumber = 5;
echo $FirstNumber+$SecondNumber; //Summation Output 15
echo $FirstNumber-$SecondNumber; //Subtraction Output 5
echo $FirstNumber*$SecondNumber; //Multiplication Output 50
echo $FirstNumber/$SecondNumber; //Division Output 2
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 $a.$b; // Output Hello Wordl!
HelloWorld! My name is Sajjad
If you set your variable value empty then PHP detect it default value 0 and count this variable value 0
both are executed 1 as their result.
$number = $number+1;
that means PHP execute result with the default value.