 |
|
Oracle Database Tips by Donald Burleson
|
While Statement
The syntax of the while statement is
extremely simple:
while (
expression ) { statement block; }
Note the following example:
while ( $i>0 ) {
$FACT *= $i;
$i--;
}
Here is a PHP script that computes 5! The
"factorial" of the number N is equal to product of all numbers from 1
to N. Written mathematically, N!=1*2*3*...*N. For the number 5, the
formula reads: 5!=1*2*3*4*5=120.
The following example shows this in script form:
$ cat
example3.php
#!/usr/local/bin/php
<?php
$i=5;
$FACT=1;
while ( $i>0 ) {
$FACT *= $i;
$i--;
}
print "Factorial:$FACT\n";
?>
$ ./example3.php
Factorial:120
Like the if statement, the while
statement has a form that allows for easy embedding into HTML:
<?php while
(expression): ?>
HTML
...
<?php endwhile; ?>
For Statement
The syntax of the for statement is
completely analogous to the syntax of the same statement found in
other programming languages:
for(begin
expression; end expression; step expression)
{ statement block; }
or, alternatively, for easier embedding into HTML:
<?php
for(begin expr; end expr; step expr): ?>
HTML
<?php endfor; ?>
The begin expression is evaluated at the beginning
of the loop and is usually used for assigning values to the variables
used in loop. The end expression is evaluated at the beginning of each
loop iteration. When the end expression evaluates to FALSE, the loop
stops. The step expression is evaluated once per iteration and is
typically used to increase or decrease the value of the loop iterator.
Sounds very complicated, doesn't it?
The following example makes it appear much
simpler:
$ cat
example4.php
#!/usr/local/bin/php
<?php
$i=1;
$FACT=1;
for( $i=1; $i<= 5; $i++) {
$FACT *= $i;
}
print "Factorial:$FACT\n";
?>
$ ./example4.php
Factorial:120
This example is a bit more complicated,
illustrating the alternative syntax for the for statement:
<!DOCTYPE
html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 1st July 2004), see www.w3.org">
<title>Example 5</title>
</head>
<body>
<h3>Loop:</h3>
<hr>
<?php
for($i=1,$FACT=1;$i<=5;$i++):
$FACT *=$i; ?>Factorial=<?=$FACT?>
<br>
<?php endfor; ?>
<hr>
<br>
Final factorial=<?=$FACT?>
<br>
</body>
</html>
The output produced by a web browser looks like
the following:
This script contains several elements worth
discussing in further detail. These elements are as follows:
-
The way it is produced (using the "tidy"
program).
-
<?=$FACT?> syntax for displaying the value
of the $FACT variable.
-
How PHP is embedded in a HTML file.
First, how is the script produced? The script is
produced by using the program for pretty-printing and checkup of HTML
documents called "tidy". More information about the tidy program can
found on
http://tidy.sourceforge.net.
The following information is helpful for the tidy
program:
$ tidy -help
tidy [option...] [file...] [option...] [file...]
Utility to clean up and pretty print HTML/XHTML/XML
see http://tidy.sourceforge.net
Options for HTML Tidy for Linux/x86 released on
1st July 2004:
File
manipulation
-----------------
-out or -o
<file> specify the output markup file
-config <file> set configuration options from the specified
<file>
-f <file> write errors to the specified <file>
-modify or -m modify the original input files
Processing directives
---------------------
-indent or
-i indent element content
-wrap <column> wrap text at the specified <column> (default is
68)
-upper or -u force tags to upper case (default is lower case)
-clean or -c replace FONT, NOBR and CENTER tags by CSS
-bare or -b strip out smart quotes and em dashes, etc.
-numeric or -n output numeric rather than named entities
-errors or -e only show errors
-quiet or -q suppress nonessential output
-omit omit optional end tags
-xml specify the input is well formed XML
-asxml convert HTML to well formed XHTML
-asxhtml convert HTML to well formed XHTML
-ashtml force XHTML to well formed HTML
-access <level> do additional accessibility checks (<level> =
1, 2, 3)
Character
encodings
-------------------
-raw output values above 127 without conversion to
entities
-ascii use US-ASCII for output, ISO-8859-1 for input
-latin0 use US-ASCII for output, ISO-8859-1 for input
-latin1 use ISO-8859-1 for both input and output
-iso2022 use ISO-2022 for both input and output
-utf8 use UTF-8 for both input and output
-mac use MacRoman for input, US-ASCII for output
-win1252 use Windows-1252 for input, US-ASCII for
output
-ibm858 use IBM-858 (CP850+Euro) for input, US-ASCII for
output
-utf16le use UTF-16LE for both input and output
-utf16be use UTF-16BE for both input and output
-utf16 use UTF-16 for both input and output
-big5 use Big5 for both input and output
-shiftjis use Shift_JIS for both input and output
-language <lang> set the two-letter language code <lang> (for
future use)
Miscellaneous
-------------
-version
or -v show the version of Tidy
-help, -h or -? list the command line options
-help-config list all configuration options
-show-config list the current configuration settings
Use --blah blarg for any configuration option
"blah" with argument "blarg"
Input/Output default to stdin/stdout
respectively. Single letter options apart from -f may be combined as
in:
tidy -f
errs.txt -imu foo.html
For further info on HTML see
http://www.w3.org/MarkUp.
The script is produced by executing the following
command from the script called example.php:
tidy -i
example.php>example5.php
The example.php script resembles the
following:
$ cat
example.php
<html>
<title>Example 5</title>
<H3>Loop:</H3><HR>
<?php
for($i=1,$FACT=1;$i<=5;$i++):
$FACT *=$i; ?>
Factorial=<?=$FACT?><BR>
<?php endfor; ?>
<HR><BR>Final factorial=<?=$FACT?><BR>
</html>
The "-i" option is used for indentation. This
option is sufficient for the vast majority of scripts. The style of
the HTML documents produced is a matter of personal expression and the
reader is encouraged to experiment until the aesthetically
satisfactory result is reached.
The <?=$FACT?> syntax used in the script should
also be noted. This syntax is an abbreviation for the following: <?php
echo $FACT ?> and works only if the PHP interpreter recognizes the so
called "short open tags". There is a parameter in the php.ini file
called the "short_open_tag". The following snippet is from the
php.ini file which defines the parameter, together with the comments
from the PHP creators advising that the short tags be turned off:
; Allow the <? tag. Otherwise, only
<?php and <script> tags
; are recognized.
; NOTE: Using short tags should be avoided when developing
; applications or
; libraries that are meant for redistribution, or deployment
; on PHP servers which are not under your control, because
; short tags may not be supported on the target server. For
; portable, redistributable code, be sure not to use short
; tags.
short_open_tag = On
As an Oracle DBA I don't have very many chances to
write scripts for the servers that aren't under my control. In
addition to that, I usually favor brevity, so I do use short tags
wherever I can.
This book aims to present ways to use Oracle RDBMSin conjunction with PHP. Coming from a long term Oracle
DBA, recommendations and advices in this book are somewhat slanted
toward the DBA-centric view of the world. Developers may possess a
somewhat different perspective. Thus, every piece of advice that
should be taken as a discussion point rather than true advice will be
noted. This is one such situation.
The last point in this section is about embedding
PHP elements into HTML files. There is no excuse for using "print"
and "echo" commands to print HTML tags. PHP scripts are meant to be
embedded into HTML documents.
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. |
|