PHP Variables
A variable is a container to store information like numbers, characters, strings, etc. In PHP variable is declared using the $ sign followed by the variable name. PHP is a loosely typed language so we don’t need to declare the data type of the variable. It automatically assigns the data type to a variable based on the assigned value.
Naming Convention.
- Variable names must begin with a dollar ($) sign, followed by the variable name.
- It can only contain alpha-numeric characters and underscore (A-z, 0-9, _).
- A variable name cannot contain spaces.
- A variable name cannot start with a number or special symbols.
- Names are case-sensitive, so $name and $NAME both are treated as a different variables.
Creating Variables.
<?php $lang = "This is PHP"; $number = 100; $pie = 3.14; echo "Language is:". $lang. "<br>"; echo "Number is: $number <br/>"; echo "Pie = $pie"; ?>
Output.
Language is:This is PHP Number is: 100 Pie = 3.14