Explain variable declaration in php?
Answer:
Starting with $ symbol and then variable_name, ex: $x the followed by an assignment operator '=' and then the value of the variable. That's how we declare a variable
$variable_name = "SomeValue";
More:
​Its good to know that you've started learning PHP and wanted to know more about it. Before talking about how to declare a variable we must have an idea what a variable is, what it does and why we use it.
Variable in any programming language is used to store value (The value can be anything like a number, a String, or a JSON response you've got from your web server). The importance of a variable is that once you store a value in it you can access the variable to use the value.
Ex: Suppose you have to make a million dollar program which will perform addition of number in PHP, which is coming from the beautiful HTML form you've placed on your website. The peace of code you would use is $_GET['number1'] and then $_GET['number2'] after this addition will be $_GET['number1'] + $_GET['number2'].
<?php
echo $_GET['number1'] + $_GET['number2'];
?>
Suppose now you want to check that the user has only entered a number and not a String and also you've to perform multiplication, division, subtraction then there will be more code and the readability of code will become worse.
<?php
echo $_GET['number1'] + $_GET['number2'];
echo $_GET['number1'] + $_GET['number2'];
echo $_GET['number1'] + $_GET['number2'];
echo $_GET['number1'] + $_GET['number2'];
?>
Instead of this you can store the value in a variable and proceed further.
<?php
$x = $_GET['number1'] ;
$y = $_GET['number2'];
echo $x + $y;
echo $x - $y;
?>
Note that it's a good programming practice to use variable name as an indication of what it does. For example, in the above code, $x represents the first number you know it and I know it. But when it will be used by someone else how he will come to know that it represents the first number so you should use $num1 instead of $x.
Thank you, please correct me if I am wrong.