What is PHP Variable and How to use?

A PHP variable is like a container where we can store a lot of information. For example, a text string “Hello ICT Layer” or an integer value 100. Once you put information in a variable, you can use it again and again throughout the code, instead of repeating the original information (value). In PHP, the variable “$” must start with this symbol or it will not work. The value of a variable may change at any time in the script.

  • Variable name case sensitive. For example, $a_number and $A_number are two different variables, in the eyes of PHP.
  • Variables are written in PHP in the following way
<?php
$hello = "This is a string";
$a_number = 4;
$anotherNumber = 8;
echo $hello ."<br/>";
$total = $a_number+$anotherNumber;
echo $total;
?>
PHP and ASP
Output of PHP variable

Explanation: In the above code, I put the string inside the quotation and put it in the $ hello variable, then I output it to the browser with echo. Again I put numbers in the variables $ a_number and $ anotherNumber and then did math with it.

  • PHP is a “Loosely Typed” language so when declaring a variable without mentioning the type of the variable, PHP will automatically convert the variable to the correct data type.

Variable Naming Method:

  1. Must start with a letter or “_” (underscore).
  2. Names include alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _. May have.
  3. There should be no space in the variable name. If the name is more than one then write “_” in underscore ($ my_string) or uppercase letter ($ myString).
<?php
$feedback = "ictlayer";
$domain = "@google.com";
$feedback = $feedback.$domain;
echo $feedback;
?>

A dot (.) Joins two strings together, this is the general rule. This is called concatenate. The same work can be done as follows. You will see that in different time codes.

<?php
$feedback = "ictlayer";
$domain = "@google.com";
$feedback .= $domain;
echo $feedback;
?>

Its output will be the same as above. Meaning [email protected]

The output of the two code blocks below will be the same: PHP variable

<?php
$test = 0;
$test++;
echo $test;
?>

Output 1 will come, note that if you do not set the value of that test variable then PHP will take its value by default NULL.

<?php
$test = 0;
$test = $test+1;
echo $test;
?>

To know more about PHP variable visit our youtube channel.