Variables in PHP


Variables are used to store data like strings or numbers for example min_age=18. The value of the variable can be changed numerous times throughout the code. It is so easy to define a variable in PHP just write $sign and then a variable name like this $min_age=18. You do not even need to mention the data type of variable. Because PHP will automatically choose the correct data type of variable according to the value stored in a variable.

Basic Things to keep in mind while creating Variables in PHP: -

  • All variables in PHP start with a $ sign, followed by the name of the variable.
  • A variable name must start with a letter or the underscore character _.
  • The variable name cannot start with a number.
  • Variable names in PHP can only contain alpha-numeric characters and underscores (A-Z, 0-9, and _).
  • The variable name cannot contain spaces.

To initialize a variable you just need the assignment operator (=) then write the value of the variable for example $min_age=18 or $name= ”Mohit”.

Now we will check this basic example to understand Variables in PHP.

<?php 
$age=22;
$name="Mohit";
$city="Delhi";
$height=5.9;
 
echo "Hello ".$name." Your age is ".$age." You height is ".$height.", and You are from ".$city;

?>

Note! Please note I am using dot (.) symbol in the above example in echo to concatenate strings.



Spread the love:

Please share this page on social media with your friends..

Facebook share Twitter Linkedin share




Great News! Now you can read all the tutorials on our Android App..

Download Owlbuddy: Learn Programming Tutorials App Now.

Download Owlbuddy: Learn Programming Tutorials