How to Declare Variables in a PHP Program
In world of programming,Variables are just containers that are used to store information.When the program runs,these variables are used during the execution and results can be stored in the same variables or new ones that are defined as the program runs.
In my Previous post,I shared about PHP Syntax and wrote my first PHP program to display “Hello world”,Today am going to dig dip into PHP Variables,How to create them and store information in them.
1 2 3 4 5
<?php $text = "Hello world!"; $t = 5; $y = 10.5; ?>
After the above code is execute,$text will hold “Hello world ” string and $t will hold 5 and $y will hold 10.5
if we wish to reassign these variables some new values in the code,this could be done easily
1 2 3 4 5 6 7 8 9
<?php $text = "Hello world!"; $t = 5; $y = 10.5; $text="PHP programming"; echo $text; ?>
The above code will Display “PHP programming” and not “Hello World“,this is because we have reassigned variable $text .
Echo() is a php function that is used to output data to the screen.The same function can be replaced with Print() function which does the same work.
Facts about PHP Variables
1.They can have short names like x ,y or a more descriptive names like surname,years etc
2.They aways start with the Dollar sign then name of the variable follows
3.Always start V name with a letter or underscore eg:$age,$_years
4.No variable name should start with a number eg:$9years
5.Variable name can only have these xcters (A-z, 0-9, and _ )
6.They are case-sensitive eg($YEARS and $years ) are two different names
See my Next Post on