 |
|
Oracle Database Tips by Donald Burleson
|
The commands in PHP language are of the usual
variety found in the most modern languages since the advent of C.
The principal commands of interest in this chapter are if, for,
while, for each and switch. These commands are the
same in almost all modern programming languages such as C/C++, Java,
Perl and PHP.
Conditional Statements
If Statement
The if statement has very simple syntax
noted as follows:
if
(<expression 1>) { statement block 1; }
elseif (<expression 2>) { statement block 2; }
else { statement block 3; }
If expression 1 is TRUE, statement block 1 is
executed. If it is not TRUE, expression 2 is evaluated, and if TRUE,
statement block 2 is executed. If neither is TRUE, statement block 3
is executed. Only the if part is mandatory, the elseif
and else parts can be skipped. The following are examples of
perfectly valid if statements:
if ($a == $b)
{ print "Variable a is equal to variable b"; }
if ($a != 3 }
{ print "Value of a != 3"; }
else { print "Value of a is 3"; }
if ($a<0) {
$ABS = -$a; }
elseif ($a>0) { $ABS= $a; }
else { $ABS=0; }
Enveloped in a UNIX style script, the last example
looks like the following:
#!/usr/local/bin/php
<?php
$a=-1;
if ($a<0) { $ABS = -$a; }
elseif ($a>0) { $ABS= $a; }
else { $ABS=0; }
print "ABS=$ABS\n";
?>
It can be executed like any other shell or Perl
script:
$
./example2.php
ABS=1
On UNIX and Linux, the "#!" characters at the very
beginning of the file means the full path of the interpreter is used
to execute the script. The rest of the script is passed to the
interpreter as the command input. Of course, in order to be able to
execute the script, script must be given the right to execute like
this:
chmod 755
./example2.php
In the example below, the interpreter is /usr/local/bin/php,
and it processes the rest of the script and correctly prints the
message ABS=1 at the command prompt. If the given if statement
is embedded in a HTML file instead of a script, the following syntax
can be used:
<?php if
($a > 0): ?>
The value of "a" is greater then zero.
<?php endif; ?>
PHP can be embedded in a HTML document and can be
used for conditional displaying of HTML elements. The form above is
actually quite useful and is definitely preferred over this one:
if ($a>0) {
print "<pre>The value of \"a\" is greater then zero.</pre>";
}
Printing HTML tags from PHP using print or echo is
considered a bad programming style and should be avoided.
Switch Statement
The switch statement is otherwise known as
the multiple choice statement and is logically equivalent, but shorter
and more elegant to write, to the if/elseif/else statement with
multiple elseif parts. The following is the syntax for the
switch statement:
switch ($i) {
case 1:
echo "Thumb";
break;
case 2:
echo "Index finger";
break;
case 3:
echo "Middle finger";
break;
case 4:
echo "Ring finger";
break;
case 5:
echo "Little finger";
break;
default:
echo "Greetings, Earthlings!";
echo "I am from the planet Aldebaran."
echo "Take me to your leader.";
}
Without the break
statement, the switch
statement does not finish after reaching one case, but proceeds to the
next one. Breaks are necessary and will be explained later.
The following is an alternative example of a
switch statement that does not have breaks after each case:
switch ($i) {
case 1:
case 2:
case 3:
case 4:
case 5:
echo "I'm human";
break;
default:
echo "Greetings, Earthlings!";
echo "I am from the planet Aldebaran."
echo "Take me to your leader.";
}
The second switch statement's meaning is,
if the number $i is equal to 1,2,3,4 or 5, the statement outputs "I'm
human" and outputs the greeting to the Earthlings otherwise.
See
code depot for complete scripts
This is an excerpt from the book
Easy Oracle PHP. You can get it
for more than 30% by buying it directly from the publisher and get
instant HTML-DB scripts from the code depot:
 |
Easy Oracle PHP
Create Dynamic Web Pages with Oracle Data
Includes online HTML-DB code depot
Buy it now for 30% off
- Only $19.95
|
HTML-DB support:
 |
For HTML-DB development support just call to gat an
Oracle Certified professional for all HTML-DB development
projects. |
|