Jump to content

  •  

Photo
- - - - -

P6: Ifs and Elses

PHP4Beginners

  • Please log in to reply
No replies to this topic

#1 DraxxTV

DraxxTV

    Member

  • SuperGeek
  • PipPip
  • 14 posts

Posted 06 July 2014 - 06:19 PM

In programming any languages, the use of ifs and else statements are a vital part of a programmer's vocabulary. This is used to see if a certain condition is met and is very useful. In this lesson we will be going over some very basic ways of using If Statements and Else Statements. This will be expanded on in later lessons.

The first thing we will be using is the If statement and we will be using it to see if a persons name is fred.

<?
$name = 'james';

if ($name == 'fred'){
    echo "Hello Fred!';
}
?>

The next thing we are going to want to do is to echo something out to the user if their name isn't fred. We can do this by adding an else statement to the end.

<?
$age = 17;
if($age >= 18){
    echo "you are old enough";
}else{
    echo "I'm Sorry, you are not old enough. Come back later. ";
}
?>

This should then display Hello Friend. The best way of thinking about this and its uses is with the following:

if($Something== true){
    //run code here!
}else{
    //do something else
}

This should be pretty simple to understand and may seem slightly useless at this point but as we continue through the series it will become very important and will be harder for you to get your head around.

 







Also tagged with one or more of these keywords: PHP4Beginners