 |
|
Oracle Database Tips by Donald Burleson
|
Strings
Strings are the most frequently used type to
describe free flowing text. The most basic notation is to place the
desired string between double quotes, shown like this: $a="Using PHP
with Oracle";. The variable $a is set to the same value as if
an expression is written like this: $a='Using PHP with Oracle';. PHP
recognizes both single and double quotes, but there is a significant
difference. Text within the single quotes is not interpreted any
further, while text within the double quotes is interpreted. In
particular, special characters and variables are interpreted. The
following script brings to light this meaning:
<?php
$a="Oracle";
$b="Using PHP with $a";
$c='Using PHP with $a';
print "$b<BR>$c";
?>
The result from the script above looks like the
following:
Using PHP
with Oracle
Using PHP with $a
The result would look like this because variable
$bis assigned its value
using the double quotes, while the variable $c is defined by
using the single quotes. Because of the double quotes in variable
$b, the value of variable $a is interpolated in value $b.
Single quotes disable such behavior. Interpolationcan be selectively inhibited even within the double quotes
by using the backslash character (\).
If the assignment is written to variable $b
as in the example below, the values of $b are the same as the
variable of $c because the interpolation of the value of $a
are inhibited by the backslash character.
$b="Using
PHP with \$a";
From the script above, <BR> is used within the
print command to separate the values of the variables $b and
$c. Why should one do this? The results of the PHP scripts are
displayed within the browser, which speaks HTML and in principle
ignores new lines except within the preformatted strings.
So, in order to display the two values on two
different lines, HTML should be used, not the old ASCII terminal
logic. The example illustrates a bad programming practice. If it can
be avoided, HTML tags should not be placed in the print command. The
proper way of performing these responsibilities is illustrated in the
next example.
There is another way of describing strings, called
"in string". Before explaining what this means, view the much
anticipated example:
<html>
<pre>
<?php
$a="Oracle";
$b= <<< EOF
This is a book about using the PHP
programming language with databases,
in particular with the $a database
EOF;
print $b;
?>
</pre>
</html>
The script is embedded in an HTML file. Instead of
placing a <BR> tag at the end of the each line, the script itself is
placed between <pre> and </pre> tags, which mean that the text between
is an externally formatted file and the new lines are displayed as
such. This is the correct programming practice and should be followed
whenever possible. In addition, look to the assignment of the variable
$b. Variable $b contains everything within the <<< EOF
and EOF; markers. The blank between the <<< and EOF is required by
the syntax. Variable $a is interpreted. The result as expected
is the following:
This is a
book about using the PHP
programming language with databases,
in particular with the Oracle database
This is so called in-string. Such notation is used
when multi-line string is needed within the script. Using double
quotes for multi-line string also works, but is deprecated.
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. |
|