Variables are used for storing values, such as numbers or strings , so that they can be used many times in a script. |
A large part of writing scripts, and programming, involves the handling and manipulation of data. Data have forms, ranging from single characters, words and sentences to integers, floating point numbers and true and false values. |
In object oriented environments such as PHP, data can also take the form of an object, a self contained module which is capable of encapsulating any number of data values of numerous different types. |
When working with data values in PHP, we need some convenient way to store these values so that we can easily access them and make reference to them whenever required. This is where PHP variables come in to the use. |
• Variables are used for storing a values, like text strings, numbers or arrays. • When a variable is define can be used over and over again in your script • All variables in PHP start with a $(Doller) sign symbol. |
Variable type |
As you’ve seen, variables begin with a dollar sign ($) and are followed by a meaningful name. The variable name cannot begin with a numeric character, but it can contain numbers and the underscore character (_). Additionally, variable names are case-sensitive, meaning that $EBIZ_emp and $ebiz_emp are two different variables. |
PHP is a Loosely Typed Language |
In PHP a variable does not need to be declared before being set. |
$var=10; |
In the example above, you see that you do not have to tell PHP which data type the variable is. |
• PHP automatically converts the variable to the correct data type, depending on how they are set.
• In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it. • In PHP the variable is declared automatically when you use it. |
Variable Naming |
Variable Naming rules in PHP |
• A variable name must start with a letter or an underscore “_” • A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ ) • A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString) |
Variable Scope |
The scope is the area in which the variable is declared. In PHP, you set your scope with curly braces (the { and } characters). There are two particular types of scope, the global scope which refers to everything outside functions, and the local scope which refers to everything inside functions.Variables you declare in the global scope cannot be used in the local scope, and variables declared in the local scope cannot be used in the global scope. |
Creating and Using Variables |
Assigning a Value to a PHP Variable |
Values are assigned to variables using the PHP assignment operator. The assigment operator is represented by the = sign. To assign a value to a variable therefore, the variable name is placed on the left of the expression, followed by the assignment operator. The value to be assigned is then placed to the right of the assigment operator. Finally the line, as with all PHP code statements, is termininated with a semi-colon (;). |
Let’s begin by assigning the word “Car” to a variable named Vehicle: |
$Vehicle = “Car”; |
We have now declared a variable with the name Vehicle and assigned a string value to it of “Car“. We can similarly declare a variable to contain an integer value: |
$rainbow_colour = 7; |
The above assignment creates a variable named rainbow_colour and assigns it a numeric value of 7. Once a variable has been created, the value assigned to that variable can be changed at any time using the same assignment operator approach: |
<?php $rainbow_colour = 7; // Set initial values $Vehicle = "Car"; $rainbow_colour = 8; // Change the initial values to new values $Vehicle = "Bus"; ?> |
Accessing PHP Variable Values |
Now that we have learned how to create a variable and assign an initial value to it we now need to look at how to access the value currently assigned to a variable. In practice, accessing a variable is as simple as referencing the name it was given when it was created. |
For example, if we want to display the value which we assigned to our rainbow_colour variable we can simply reference it in our echo command: |
<?php echo “The colours of rainbow is $rainbow_colour.”; ?> |
This will cause the following output to appear in the browser: |
The colours of rainbow is 8. |
The examples we have used for accessing variable values are straightforward because we have always had a space character after the variable name. The question arises as to what should be done if we need to put other characters immediately after the variable name. For example: |
<?php echo "The color of the rainbow is the $rainbow_colourth"; ?> |
What we are looking for in this scenario is output as follows: |
The color of rainbow is the 8th. |
Unfortunately PHP will see the th on the end of the $rainbow_colour variable name as being part of the name. It will then try to output the value of a variable called $rainbow_colourth, which does not exist. This result in nothing being displayed for this variable: |
The colour of the rainbow |
Fortunately we can get around this issue by placing braces ({ and }) around the variable name to distinguish the name from any other trailing characters: |
<?php echo "The color of the rainbow is the ${rainbow_colour}th"; ?> |
To give us the desired output: |
The color of the rainbow is 8th. |
Changing the Type of a PHP Variable |
Loosely typed languages such as PHP (and JavaScript), allow the variable type to be changed at any point in the life of the variable simply by assigning a value of a different type to it. For example, we can create a variable, assign it an integer value and later change it to a string type by assigning a string to it: |
<?php $myNumber = 6; // variable is of integer type $myNumber = six; // variable has now changed to string type ?> |
The process of dynamically changing the type of a variable is known as automatic type conversion. |