PHP Tutorials for beginners – Variables

php variableHi Friends, Hope you are doing well. I believe that you have gone through our previous PHP tutorials and get some ideas. That is fine. Today we are going to talk about PHP Variables.

PHP variables as containers for storing data.

Rules for PHP variables:

  • A variable starts with the $ sign, followed by the name of the variable.

  • A variable name must begin with a letter or the underscore character.

  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).

  • A variable name should not contain spaces.

  • Variable names are case sensitive ($y and $Y are two different variables).


OK. Do not get sleepy, If you go for example you can easily understand what is variable.

For eg 1:
<?php
$a = "Hello, ";
echo $a;
?>

Check this output in your localhost, it will show Hello. 

This is it, Here $a is a variable, which contains hello value into that. Like that way we have to use php variables.

Eg 2:
<?php
       $v = 2500;
       $x = 7500;
       $y = 5000;
       $z = $v+$x+$y;
       echo $z;
?>

Do this practice in your localhost. Now you can get clear view about these variables.

Notes:

We do not initiate data type in PHP variables. It automatically converts the variable to the correct data type, depending on its value.

If you use alphabets, make double codes (in between that). for example ( $name = "hello word, how are you"; ) and should end with semi colon.

PHP has four different variable scopes. We will see about those in next tuto's.

Thanks for reading. Post your comments.

Post a Comment

Post a Comment (0)

Previous Post Next Post