|PHP Variables and Expressions for Complete Beginners|
|Written by Ian Elliot|
|Thursday, 09 January 2020|
Page 1 of 2
Variables and expressions are the core of programming in general and no less so in PHP. If you have never encountered the ideas here is a gentle introduction.
Introduction to PHP
A key idea in programming is the "variable".
A variable is just a named area of storage or somewhere you can store a value.
In PHP a variable is indicated by a leading $ sign before its name.
For example, $a is a variable and you can store a value in a variable using the equals sign.
The temptation is to read this as
"dollar a equals one"
however you will find it much more helpful to understand what is going on if you always read this sort of instruction as
"store one in $a".
You can also think of the variable $a as a name for a sort of bucket that you can put something into for later use.
The whole expression is called an assignment statement - because you assign a value to $a.
Notice also that a variable can only store one value at a time and storing a new value erases any trace of what was already stored.
stores 2 in $a and overwrites anything that was already stored.
This raises the question of how you get something out of a variable?
The answer is you simply use its name where you would have used the value. For example, you can write:
which doesn't send the text "$a" to the output but the value stored in $a, i.e. the number 1.
If you have used other programming languages you might know that they often use specific types of variable to store specific types of data. PHP doesn't work this way and it allows you to store almost any type of data you care to store in a variable with minimum fuss. This is usually good but it can occasionally cause problems.
PHP is said to be weakly typed or untyped.
Similarly PHP doesn't demand that you make a list or declare all of the variables you are using at the start of your program – simply using a variable is enough for PHP to create it and keep track of it.
In the main PHP tries to be as easy to use as possible.
Working with variables
There are some rules about what you can call a variable but most things will work. There are also some special and predefined variable names that you can't use. You can have any number letters or numbers but you can't include any space. You can use the underscore character to make it look as if there is a space as in:
You can also use upper and lower case characters to emphasise the meaning but notice that variable names are case sensitive. That is:
are two different variables.
This is something that often causes errors so be very careful how you mix upper and lowercase letters.
If you are using NetBeans or Eclipse as your PHP development system then you can make use of automatic prompting which shows you a list of possible variable names as you type – you can select one of the possibilities as an easy way to enter a variable name and a way of minimising the likelihood of entering a variable name incorrectly.
Selecting variables from a list is far less error prone than typing them in.
In Eclipse names of all of the variables that you have used are listed in the Outline window. If you click on one of the variable names then every use of that variable name is highlighted throughout the program.
The Outline window helps you keep track of your variables and find them in your PHP code.
PHP lets you store almost any type of data in a variable. At the moment you might not have noticed that there are different types of data – but there are.
For example, "abcd" is a string of characters and 3.14 is a number.
The difference is made very clear when you consider the fact that it makes sense to do arithmetic with 3.14 but not with "abcd".
In fact this is a good definition in that data types differ in the operations you can perform on them.
PHP recognises four basic types of data:
stores the Boolean value true in $a
stores the integer 1 in $a
stores the float 2.1 in $a and
stores the string Hello in $a.
Strings are incredibly useful and there is a great deal to say about them and they deserve an article devoted to them.
Sometimes the differences between these types might seem arbitrary.
For example is 2.0 a float or an integer?
After all it's a whole number but it has a decimal point.
The answer is that it's a float because it has a decimal point. More subtle is the difference between "2.1" and 2.1 – the first is a string of characters (that just happen to be digits and hence look like a number) and the second is a float, i.e a number with a decimal point.
In most cases you can simply ignore the differences between different types of data because PHP will attempt to automatically convert between types as necessary – there is more to say on this topic but after the introduction of expressions.
Also notice, especially if you are familiar with other programming languages that in PHP variables do not have a data type - you can store anything in any variable. However at any given time a variable will have something stored in it that does have a data type and what operations are applicable to that variable will change according to what is stored in it.
|Last Updated ( Thursday, 09 January 2020 )|