Variables illustration


The current date and time is February 4th, 2012 2:17:PM

Companion Web Site

Larry Ullman has a web site devoted to PHP for the World Wide Web: Second Edition at this location.

It is intended as a supplement /complement to the book and has many helpful resources.

The Book

Variable image

PHP for the World Wide Web: Second Edition written by Larry Ullman

Chapter 6
Control Structures

"Writing conditionals in PHP comes down to identifying True or False situations." p132

Creating the HTML Form

As with the work in the preceding chapters, the ideas we will be learning and applying will need a form to work on/from. So an HTML form will be constructed to gather certain information from users which will then be sent to a PHP page.

The if Conditional

The syntax is quite simple:
if (condition) {
statement(s);
}
It depends on the True or False nature of the condition as to whether the statement is executed or not. The Validation Functions listed on page 120 are what decide whether to return a True or False concerning the data entered. There are many, but these were used in the chapter:

Using Else

if (condition) {
statement(s)
}else {
other _statement(s);
}

When using the if-else, the else statement(s) will be executed when the if condition is not met clearly and cleanly!

More Operators

Comparison:

Here is link to the PHP Manual page with the Comparison Operators

Logical:

Here is a link to the PHP Manual page with the Logical Operators

Besides using Logical Operators to make more complex conditionals, Nesting Conditionals can be used.

When constructing Conditionals:

Using elseif

You can use as many elseifs as you wish as part of one if statement as long as else is the last part of the conditional.

The Switch Conditional

This can save time and clarify an if-elseif-else conditional which has become too big, bulky, elaborate. The switch has only one condition which is the value of its variable. It uses cases and works through each case looking for the one that satifies its condition. It needs a 'break' (placed between each case in the conditional)to stop it once it has found its condition. Without the break, it will go right to the closing curly brace even after it finds what it is looking for.

The For Loop

The use of the forloop is to execute sections of code repeatedly according to a number you designate.

for (initial expression; condition; closing expression){
statement(s);
}

Here is an example for causing numbers from 1 to 10 to be printed to the page:
for ($v = 1; $v <=10; $v++){
echo "$v";
}

The Form

Complete this form to register

First Name:
Last name:
Email Address:

Password:


Confirm Password:

Birthday

Favorite Color:




Warning: show_source() has been disabled for security reasons in /home/rjoannej/public_html/ullmanBook/chapter6.php on line 243

Warning: show_source() has been disabled for security reasons in /home/rjoannej/public_html/ullmanBook/chapter6.php on line 247