Now that you know about variables and how to use them we need to expand our horizons to include expressions.
An expression is something that can be "worked out".
For example 1+2 is an expression because you can work it out, i.e. add 1 and 2 together to produce the result 3.
An expression is a mixture of values and operators like "add" that can be combined together to produce another single value as a result. In short an expression is anything that reduces down to a value.
An arithmetic expression is the most familiar example and in PHP you can use the usual operators of arithmetic with the only difference being that the symbol for multiplication is replaced by * and division is always indicated by /.
Consider a more complicated example:
this means 2 times 3 plus 1.
Why is the multiply done first?
The answer is that all operators have priorities and in this case the multiply * has a higher priority than the plus + and so is performed first. If operators have the same priority then they are applied left to right in the order you wrote them.
If you are worried about operator priorities or want to change the priorities then you need to use brackets. Brackets are always worked out first. For example:
evaluates to 9 because the bracket is worked out first to give 3 and then 3*3 gives 9.
You can store the result of an expression in a variable. So:
is a perfectly valid PHP instruction and it stores the value 7 in $a.
Of course you can use variables within expressions. So for example:
results in 5 being stored in $b – whenever a variable is used it's value is retrieved and used in the expression.
That's more or less all there is to expressions but PHP provides lots of different operators for different types of data and you can spend a lot of time mastering them.
Self reference – adding one
Expressions are fairly easy and you might well think you understand them perfectly but there are some expressions that are troubling if you are a beginner.
For example, consider the following:
Read as a mathematical equation this looks like nonsense but programming isn't mathematics.
If you read the instruction as suggested earlier in the article then what it means should be clear - store in $a the result of adding one to what is already stored in $a. That is, first work out $a+1 and then store the result back in $a.
So if $a initially contained 2 after the instruction it would contain 3. So it is an instruction that adds one, or increments, the contents of a variable. Once seen and understood – its easy.
takes what is stored in $a, multiplies it by 2 and stores the result back in $a. That is, it doubles the contents of $a. You can probably work out other examples for yourself.
It has already been pointed out that PHP identifies four different types of data. In fact PHP doesn't make much out of the distinction between data types and does its best to cover them up. Other languages make more of data typing and even elevate it to their central organising principle. In PHP the only real importance of data type is the operators you can use with the type - and even in this respect PHP tries to smooth things over.
For example, you can only use arithmetic operators with numeric data – you can't multiply two strings together.
That is "cat"* "dog" doesn't make any sense as a piece of arithmetic. This might seem obvious but there are times when data types aren't quite as clear cut.
Sometimes there is a possible simple conversion that allows the operator to be used and PHP attempts to make life easy by always performing these conversions for you.
For example, if you write
echo 1+ "1" ;
then strictly speaking it shouldn't work because "1" is a text string, even if it is a digit, and you can't do arithmetic with text but PHP automatically converts it to a number for you.
It even works if the string is not a pure number:
echo 1+ "1 apple";
There are, however, limits to how far PHP will go and it doesn't work in the case of:
echo 1+ "one apple";
The reason it doesn't work is that converting "one" into numeric 1 is more than a data type conversion - it involves interpretation.
You can write some PHP to convert words like "one" into numeric values like 1 so that you can do arithmetic with them – but this is a slightly more complicated story and well off topic.
To find out more about how types are converted you need to look the details in the documentation on each type. In particular because user input is often in the form of a string the whole subject of string handling is wrapped up in data type conversion. The most important thing is to realise that PHP does convert data types for you but sometimes this can produce an error or a result that you never really intended.
PHP is a language that creates web pages, but exactly how does it do this? If you know some HTML, you might be wondering what PHP have to do with it and how it integrates into a web page. In this arti [ ... ]
PHP is a programming language specifically designed to generate HTML pages but one of the topics that is rarely discussed is exactly how to do this. In this article the idea that HTML generation shoul [ ... ]