Web Design Articles

Part 3: Conditional Programming

Web Design Articles

Submit a Web Design Article


Web Design Articles    |    Code    |    PHP

Here you will find articles about Web Design, Web Site Accessibility, Web Standards and Internet Marketing. If you would like to have an article you have written published on this site please use the form above to submit your article for publication. If your article is published, you will receive a credit with a byline linked to your website URL.


   Article

Part 3: Conditional Programming

by David Gowans


The Basics of Conditional Statements (if this is true then... else...)

Conditional statements are used to compare two values and carry out different actions based on the results of the test. Conditional statements take the form IF, THEN, ELSE. Basically the IF part checks for a condition. If it is true, the then statement is executed. If not, the else statement is executed.


Structure of a Conditional Statement

The structure of an IF statement is as follows:

if (variable A == variable B) {
      // Do this
}

else {
      // Do something else
}


Standard Comparison Variables

You can use the standard comparison symbols to compare values and control the flow of the user´s experience:

==

!=

<

<=

>

>=

equal to

not equal to

less than

less than or equal to

greater than

greater than or equal to


The IF Statement

The most common use of an IF statement is to compare a variable to another piece of text, a number, or another variable. For example:

if ($username == "webmaster") {

      // Login the user

}

else {

      echo "Invalid Username";

}


This would compare the contents of the variable ($username) to the text string ("webmaster").  Therefore, IF the username is exactly the same as the text string "webmaster" the code would only allow the user to be logged in.  Otherwise, the ELSE statement would print the "Invalid User" response. Of course, you are not limited to just one line of code. You can add any PHP commands in between the curly brackets. You can even include other IF statements (nested statements).


Multiple Comparisons within a Conditional Statement

There are other ways you can use your IF statement to compare values. You can compare multiple variables within the same IF statement by simply including the && or || symbol between each comparison. 


The && symbol means that ALL comparisons must return a value of TRUE before it will allow the code within the conditional to be processed.  In this case, the user will not be logged in unless the username and password are correct.

if ($username == "webmaster" && $password == "checkers") {

      // Login the user

}

else {

      echo "Username and Password combination are not valid";

}


The || symbol means that ONLY ONE comparison must return a value of TRUE before it will allow the code within the conditional to be processed.  In this case, the email will not be sent unless all variables have been entered correctly.

if ($name == "" || $email == "" || $phone == "") {

      echo "Your email was not sent. Please enter all fields before submitting.";

}

else {

      echo "Email submission successful";

}


posted on Jun 4, 2007

   Topics



   Google


Web Design Articles    |    Code    |    PHP