PHP Lesson 4 - Conditional Statements

Now that we have discussed variables, we can begin to add in conditional statements to our php script. Conditional statements are used in a variety of different ways, from password prompts, securing pages, displaying select information and more.

Each conditional statement will begin with { and end with } everything in between defines this statement. For example:

This script will recognize the user of the page and display appropriate information. This is commonly used in password protected pages, or in discussion forum scripts.

Now, we can enter in more conditional statements, that supply the browser with what it should do if the user isn’t the owner.

This will display two conditional statements, one for the owner and one for the unrecognized user. The owner will see Log-In here while the other users will see the message that they donÕt have permission to access the area.

Conditional statements can also be used to display data that may vary according to time, date, month, etc… for example:

If the day is Monday, that date will display on the page. You will need to assign variables for each of these dates to supply the rest of the days of the week.

Conditional statements can also be used to put several variables together. For example:

This will display a log-in form if all of the variables on a user’s browser match the predefined variables in the script. This is an easy way to use multiple if statements while saving work by not retyping them all in. The || tells the browser that there is another conditional statement that needs to be recognized.

These are just a few examples of how conditional statements can be used in your scripts. You can preset variables and use them in a number of ways. Try experimenting with different conditional statements, such as dates to display simple information on you website. Once you have these down, you can move to more complex conditional statements that can help you moderate discussion forums, restrict access to various pages of your site and in general, make your life easier.

Our next article will cover the myriad of functions that PHP can perform. There are over 700 different preset functions designed to simply your coding. Find out if there are functions that can improve your website.

Related Posts:
Java Script Lesson 10 ‘ Conditional Statements
DHTML Lesson 4 - Cross Browser DHTML
DHTML Lesson 2 - Continuing the Basics
PHP Lesson 2 - Syntax
Java Script Lesson 6 - Functions With Two Arguments

Posted on 12/27/05 2:22 AM

Be the first to comment!

Leave a Reply


SYNDICATION

    NEW! Blog Feed:
    Reviews Feed:
    News Feed:
    Resources Feed: