PHP Variables and Expressions for Complete Beginners
Written by Ian Elliot   
Thursday, 09 January 2020
Article Index
PHP Variables and Expressions for Complete Beginners
Expressions

morephpRound

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.

Related Articles

Ten minutes to PHP objects

Object-oriented HTML Generation In PHP

Ten minutes to PHP

 

Introduction to PHP

phpcover

Contents

  1. Getting started
    Getting Started With NetBeans PHP - Local Projects
  2. What PHP Does
  3. PHP Variables and Expressions for Complete Beginners
  4. PHP Control Structures 1 - if and else
  5. PHP Control Structures 2 - switch and elseif
  6. PHP loops
  7. Advanced loops
  8. Functions
  9. Ten minutes to PHP objects
  10. PHP Inner Functions And Closure
  11. NetBeans Remote Projects and debugging

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


PHP Control Structures 1 - if and else

Getting to grips with programming or a new language is a matter of mastering the flow of control. This is the key idea in programming and understanding it makes the difference between a programmer and [ ... ]



PHP Inner Functions And Closure

PHP inner functions and anonymous functions are a little strange to say the least. However, just because something is strange doesn't mean that it isn't useful. We take a close look at the way PHP fun [ ... ]


Other Articles

espbook

 

Comments




or email your comment to: comments@i-programmer.info

<ASIN:1449392776>

<ASIN:0994346980>

<ASIN:0596006306>

<ASIN:1785883445>

<ASIN:1890774790>

<ASIN:1484219953>

 



Last Updated ( Thursday, 09 January 2020 )