PHP variables and expressions for complete beginners
Thursday, 28 January 2010
Article Index
PHP variables and expressions for complete beginners
Expressions

Banner

Expressions

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:

  1+2*3

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:

  (1+2)*3

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:

   $a=1+2*3;

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:

  $a=3;
  $b=2+$a;

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:

  $a=$a+1:

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.

Similarly:

  $a=$a*2;

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.

Type juggling

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.

More PHP

Banner


Ten minutes to PHP objects

PHP is a fully object-oriented language with lots of powerful features. This introductory guide looks at how PHP handles objects.



Developing with PHP and Eclipse (Indigo)

Find out how to make PHP development easy with the latest version of Eclipse, Indigo, and how to set up local and remote debugging.


Other Articles

    <ASIN:0470395095>

    <ASIN:1932394753>

    <ASIN:0470527587>

    <ASIN:1430210117>

    <ASIN:0596804377>



    Last Updated ( Monday, 15 February 2010 )
     
     

       
    RSS feed of all content
    I Programmer - full contents
    Copyright © 2014 i-programmer.info. All Rights Reserved.
    Joomla! is Free Software released under the GNU/GPL License.