Home Logo
Show Full Menu
Hide Full Menu
 

If Else Statement

Created: 01/10/10 Views: 1039 myFave Image


In this tutorial we will learn If Else Statements
We have already seen what an If Statement is in the previous tutorial (if you have not read this, then do so before you carry on with this tutorial) and the If Else statements is just an add-on. We can write a statement that prints something if the condition is true, however what happens if the condition is false? This is where the else part comes in, the else allows us to request a task to perform if the statement has been returned false.

To write an If Else Statement is simple
  • if( //Condition to check ) {
  • //Requested task if condition is true
  • }else{
  • //Requested task if condition is false
  • }


Here is a simple example of an If Else Statement
    Script
  • $name = "John";
  • if($name == "Frank"){
  • print "Hey Frank!";
  • }else{
  • print "Hey Guest!";
  • }

    Result
  • Hey Frank!
So from this example, the statement is checking if the variable 'name' is equal to (==) 'Frank', and if it is, then print "Hey Frank!", however if the statement is false, it will print "Hey Guest!" instead.

Copy and try for yourself!
    Script
  • $theNum = 11;
  • if($theNum >11){
  • print "greater";
  • }else{
  • print "not greater";
  • }

    Result
  • not greater

    Script
  • $theNum = 11;
  • if($theNum == 11){
  • print "Same";
  • }else{
  • print "Different";
  • }

    Result
  • Same

This is only a few basic examples, but it should be enough for you to understand the basic method of writing an If Else Statement and why you would use them in your PHP scripting.


next tutorial: Elseif Statments

aliejana - 02/12/11 00:13
This Tutorial will not download for here and you tube. all other downloads OK Thanks john