FFD - Visit the site
ICMS

12 January 06

Beginner PHP Tutorial 4: Conditions and Operators

The last tutorial was nice and short, but I have quite a bit more to cover in this tutorial.

What are conditions and operators?

Conditions are pretty much 'If [this] is true then'. So something has to be true. So if you said 'If I have three pounds then buy 4 sweets' and you had four pounds. Then the condition would be false and 4 sweets wouldn't be bought (unless you had another condition). If you had three pounds then 4 sweets would indeed be bought because the condition was true.

Operators are more maths and programming related, but tie in with conditions. They are part of conditions. So in maths you could say 'If my name is equal to Ted then ...'. The 'is equal to' is the operator. You could also say 'If the length of your name is greater than 7 characters then ...'. In this the operator is 'is greater than'.

Simple PHP Conditions and Operators

The operator in php for 'is equal to' is two equal signs ('=='). So a condition could simply be '$variable == "value"'. The operator for 'is greater than' is '>' and for 'is less than' is '<'. But our condition does nothing except return true or false depending on whether the variable 'variable' is equal to 'value' or not. We need a function to work with our condition, this function is 'If()'.

If() function

This function does the most simple thing ever, it performs a task IF something is true. So:

<?php
if($variable == 'value'){
echo 'wow';
}
?>

...places 'wow' on the screen if the variable 'variable' is equal to 'value'. Notice that the task performed if the condition is true is placed inside { and }. Also notice that a semi-colon is only placed after the 'tasks' and not after the { or the }.

Else and Else If

So we have covered simple if but we may want to do something if the condition returns false. Well we can just add 'else{ echo 'boo';}'...magic! So our if function is now:

<?php
if($variable == 'value'){
  echo 'wow';
}else{
  echo 'boo';
}
?>

...which is pretty good but there is still something to add: else if. This is both 'else' and 'if' put together (sorry if I'm stating the obvious) and basically means 'otherwise if [this] is true then ...' and can be used like else:

<?php
if($variable == 'value'){
  echo 'wow';
}else if($variable == 'value2'){
  echo 'ok';
}else{
  echo 'boo';
}
?>

Switches

Well the IF function is all very nice but too many else if 's can be very tiresome to type in:

<?php
if($name == 'jake'){
  echo 1;
}else if($name == 'jack'){
  echo 2;
}else if($name == 'james'){
  echo 3;
}else if($name == 'john'){
  echo 4;
}else if($name == 'joseph'){
  echo 5;
}
?>

...so instead use switch() like this:

<?php
switch($name){
  case 'jake': echo 1; break;
  case 'jack': echo 2; break;
  case 'james': echo 3; break;
  case 'john': echo 4; break;
  case 'joseph': echo 5; break;
  default: echo 0;
}
?>

Note that 'break' is used to break out of the switch statement, to stop php executing the code for james, john and joseph. The default condition is the same as ELSE. For a good tutorial on switches visit the php.net tutorial on switches.

Well done, you have learnt a lot in this tutorial. I recommend that you re-read this tutorial and create your own IF conditions before you continue to the next tutorial.