Related Pages
Web page templates
Interactive web page colours CSS tutorial
Interactive fonts using CSS tutorial
Setting web page fonts using CSS

PHP Tutorial

PHP: Displaying Different Content

Look at the lefthand side of the Index page. You will see three links under the SQL heading of the Manuals section. Try these links.

Each of these pages describes procedures for setting up a database, and using Structured Query Language with that database.

However, each page that you see is different. One describes procedures using MySQL databases, the second deals with Microsoft Access databases, while the third uses a SQL Server database. But all pages are from the same document file. I used PHP to decide which part of the document to display.

How?

Constraints:

  • The web page is displayed using HTML, so the web page must have some HTML in it.
  • PHP must provide some means to making choices about what to display.

PHP language constructs - using IF

If statements in PHP work the same way as they do in most modern programming languages. They provide the facility to execute the program code in different ways depending on the value or existence of a particular variable.

Using the above example, there is a line in the MySQL version that reads as
spacerHowever, I have generally tried to avoid the commands specific to MySQL in this course"
for the MySQL version, I could have constructed my code to look like:

spacer<HTML>
spacer ...
spacerHowever, I have generally tried to avoid the commands specific to
spacer<?PHP
spacerspacer if ($link=="mysql") {
spacerspacer print "MySQL";
spacerspacer }
spacer?>
spacer in this course.
spacer ...
spacer</HTML>

More explanation

You can embed PHP code within HTML documents. All that you need are the opening (<?PHP) and closing (?>) tags surrounding your PHP code, as shown above.

This code is part of an HTML document, in which the variable "link" identifies the version that the user has selected. When the brower receives a request for the page containing this code, it is also told that the variable "link" has the value "mysql" ( We'll deal with that in some other tutorial).

The condition to be tested is enclosed in parentheses, ()

PHP uses the double equal symbol to denote equality: $link=="mysql" means that the value of the variable named link must be the string "mysql" for the condition to be true.

The braces, {} in the PHP code enclose what the code must do if the condition is true. If the condition is not met, then no code is executed until the end of the braces.

The print statement includes the word (string) "MySQL" in the sentence outside the PHP code. The browser then displays the complete sentence as below:

However, I have generally tried to avoid the commands specific to MySQL in this course.

If the variable "link" is not equal to "mysql", the browser ignores the code within the braces and will print the following:

However, I have generally tried to avoid the commands specific to in this course.

PHP language constructs - using ELSEIF

If you have several different words to insert, each depending on what the viewer wants to see, then you can use as many if statements as you have different words. The if statements would be inserted into the code immediately following one another. However a more compact, and probably more readable, way to do this is to use elseif statements following the original if statement, as follows:

spacer<HTML>
spacer ...
spacerHowever, I have generally tried to avoid the commands specific to
spacer<?PHP
spacerspacer if ($link=="mysql") {print "MySQL"; }
spacerspacer elseif ($link=="Access") {print "Microsoft Access"; }
spacerspacer elseif ($link=="SQLserver") {print "SQL Server"; }
spacer?>
spacer in this course.
spacer ...
spacer</HTML>

Then, if a user clicked a link with variable equal to "Access", the user's browser would display:

However, I have generally tried to avoid the commands specific to Microsoft Access in this course.

The line would still print with a blank if the variable is not equal to "mysql", "Access" or "SQLserver".

PHP language constructs - using ELSE

If you use else statements at the end of a set of conditions in your code, you can define a default behaviour that will determine what the user's browser will display if none of the condtions are met. It is strongly recommended that you do this: it is very easy for users to insert their own values for a link that includes variables sent to PHP.

The complete decision-making code could now be:

spacer<HTML>
spacer ...
spacerHowever, I have generally tried to avoid the commands specific to
spacer<?PHP
spacerspacer if ($link=="mysql") {print "MySQL"; }
spacerspacer elseif ($link=="Access") {print "Microsoft Access"; }
spacerspacer elseif ($link=="SQLserver") {print "SQL Server"; }
spacerspacer else {print "<b>JUST CLICK THE LINK, PLEASE!</b>"; }
spacer?>
spacer in this course.
spacer ...
spacer</HTML>

If the link variable is not equal to any of "mysql", "Access" or "SQLserver", the browser would then show:

However, I have generally tried to avoid the commands specific to JUST CLICK THE LINK, PLEASE! in this course.

Since there is no condition included in the else statement, any value of the link variable that is not equal to one of "mysql", "Access" or "SQLserver" will be dealt with without causing security issues.

PHP language constructs - using SWITCH and CASE

Switch statements are used instead of if - elseif - else statements. They also must be enclosed within PHP tags in your HTML document. They are also defined using parentheses () for the condition to be tested, and braces {} for the entire switch statement. You may also need to enclose code in braces within each condition if more than one instruction is to be executed when the condition is met.

Switch statements test the value of a PHP variable. Possible values for the variable must be discrete and finite in number. Switch statements are therefore useful for variables that can contain an integer, or string. The variable to be tested is specified at the beginning of the statement, and the possible values are listed within the body of the statement. Code to be executed in each case is included immediately after the value that will trigger the code. Once this code has executed, the break command is used to skip the possibilities that have not yet been considered.

The switch statement that is equivalent to the above code is reproduced below:

spacer<HTML>
spacer ...
spacerHowever, I have generally tried to avoid the commands specific to
spacer<?PHP
spacerspacer switch($link) {
spacerspacer spacercase "mysql" : print "MySQL "; break;
spacerspacer spacercase "axs" : print "Microsoft Access "; break;
spacerspacer spacercase "ss" : print "SQL Server "; break;
spacerspacer spacerdefault : print ""; break;
spacerspacer }
spacer?>
spacerin this course.
spacer ...
spacer</HTML>

Note that a superglobal array should be used instead of the variable that I have called "$link", again for security reasons. This code is merely an example.

Tip: I find the default case extremely useful for debugging. A message like "Oops, the link variable should be one of mysql, access or sqlserver - it's not, at whichever line of code this message is inserted" speaks volumes.

  • Home
  • |
  • Sitemap
  • |
  • Databases
  • |
  • Web development
  • |
  • Delphi development
  • |
  • Image manipulation
  • |
  • KaSaB
  • |
  • Contact