Concept of Variable
Sometimes in your program, you need to store data. During the execution of the program, computer processor stores those data in its memory cells before using those. If you use 100 different data in your program, then processor allocates 100 or more memory cells to store those data. Every memory cell has a number. In your program, you may need to use same data many times, therefore, to use a data for the next time in the program, you need to know the address of memory cell(s) where it is stored. But when you work with lots of data, it is not easy to remember those cell numbers. And, with the increase of data, it becomes harder or about impossible. To remember those cell numbers easily, programmers give each cell an easy name. This name is called variable. For example, if your age is 25 and it is stored in memory cell number 6927, is it not easy to remember the variable “age” instead of 6927 to refer the 25? You don’t need to know where 25 is stored in memory; variable is used to reference that location number.
Before using a variable, you need to declare it.
The syntax for declaring a variable would be-
The syntax for declaring a variable named today is-
<?php $today; ?>
Variable naming rules
A PHP variable’s name can be anything as long as it follows the rules below-
- Variable name must be starts with dollar ($) sign. ex. $today, $height etc.
- First character must be a letter or underscore. Ex. $today, $_today etc.
- Other than first character, there may have any number letters (A-Z,a-z), numbers (0-9), or underscores(_). ex. $_4site etc.
- Variable name is case-sensitive. That means $today and $Today are 2 different variables.
- Any variable that doesn’t fulfill the above rules is an invalid variable.
- Variable name should be meaningful so that it tells what it is for. Ex. $username.
- Predefined variables names in PHP start with a underscore then only capital letters, ex. $_REQUEST, therefore, avoid declaring your variables in this format.
Assigning values to variable
What is the use if a variable doesn’t hold a value? There are 2 ways to assign a value to a variable-
- By value
- By reference
The syntax for assigning a value to a variable is-
$variable_name = value;
Here, ‘variable_name’ is the name of the variable and ‘value’ is any type of data(ex. Integer, string etc.). The above line can be read as “assign ‘value’ to the variable ‘variable_name’”.
To declare a variable named today and assigning a value of Wednesday to it, you could write-
<?php $today=”Wednesday”; ?>It’s a very good practice to assign a value to the variable when declaring it.AS PHP interpreter handles data before it becomes HTML, therefore, it is called PHP:Hypertext Preprocessor.It’s a common tendency for beginners to think the above example as “$day is equal to Wednesday” when they see the = sign. It could be true in algebra, but not in PHP programming.
The syntax for assigning a reference to a variable is-
$variable_name = &$existing_variable_name;
Here, ‘variable_name’ is the name of the variable and ‘existing_variable_name’ is a previously declared variable that will be used as reference to the ‘variable_name’ variable. The above line can be read as “assign ‘existing_variable_name’ as the reference to the variable ‘variable_name’”.
To declare a variable named tomorrow and to assigning a reference of day variable to it, you could write-
<?php $today=”Wednesday”; $tomorrow=&$today; ?>
As the new variable is referencing the existing variable, so their values are same. Therefore, changing value to a variable will also change the value in other one.
The syntax for displaying a variable is-
The syntax for displaying a variable named today is-
<?php $today=”Wednesday”; echo $today; print $today; ?>
Line 3 and 4 both display Wednesday.
Variable scope refers the parts of a script where the variable’s value is available after it is declared within a script. To understand scope properly, function knowledge is required. You‘ll about variable scope in the next lesson after function.
There are two types of variables in PHP-
- User-defined variables
- Predefined variables
As the names implies these variables are defined in the script by the users. In the above examples the variables we defined are all user-defined variables.
<?php $today=”Friday”; ?>
In the above example, the variable $today that I created is a user-defined variable.
- PHP comes with a large number of predefined variables to any script that it runs.
- Predefined variables play an important role in PHP programming. They provide lots of useful information such as server information, information that comes after submitting a form etc.
- As these variables are structures as associative arrays and you’re not introduced array yet, we’ll discuss about predefined variables after array.
|‹‹ PHP Basic Syntax : Previous Lesson||Next Lesson: PHP Data Type››|