CVS update: php3/doc/chapters
| From: | eschmid | Date: | Fri, 25 Dec 1998 17:41:43 +0000 |
| Subject: | CVS update: php3/doc/chapters | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-3035@lists.php.net to get a copy of this message | ||
Date: Friday December 25, 1998 @ 12:41
Author: eschmid
Update of /repository/php3/doc/chapters
In directory asf:/u/temp/cvs-serv15615
Modified Files:
lang-const.sgml
Log Message:
cleaned up some identation and made the examples more readable
Index: php3/doc/chapters/lang-const.sgml
diff -c php3/doc/chapters/lang-const.sgml:1.30 php3/doc/chapters/lang-const.sgml:1.31
*** php3/doc/chapters/lang-const.sgml:1.30 Thu Dec 24 10:04:38 1998
--- php3/doc/chapters/lang-const.sgml Fri Dec 25 12:41:43 1998
***************
*** 14,78 ****
<sect1 id="constants">
<title>Constants</title>
<simpara>
PHP defines a few constants and provides a mechanism for defining
more at run-time. Constants are much like variables, but they have
a slightly different syntax.
<para>
The pre-defined constants are __FILE__ and __LINE__, which
correspond to the filename and line number being processed when
they are encountered.
! <example><title>Using __FILE__ and __LINE__</title>
<programlisting>
<?php
! function report_error($file, $line, $message) {
! echo "An error occured in $file on line $line: $message.";
! }
!
! report_error(__FILE__,__LINE__, "Something went wrong!");
?>
</programlisting>
</example>
<para>
! You can define additional constants using the <function>define</function>
! and <function>undefine</function> functions.
! <example><title>Defining Constants</title>
<programlisting>
<?php
! define("CONSTANT", "Hello world.");
! echo CONSTANT; // outputs "Hello world."
! undefine ("CONSTANT");
?>
</programlisting>
</example>
<sect1 id="expressions">
<title>Expressions</title>
<simpara>
Expressions are the most important building stones of PHP. In PHP,
almost anything you write is an expression. The simplest yet
most accurate way to define an expressions is "anything that has a
value".
<simpara>
Simple examples that come in mind are constants and variables.
When you type "$a = 5", you're assigning '5' into $a. '5',
obviously, has the value 5, or in other words '5' is an expression
with the value of 5 (in this case, '5' is an integer constant).
<simpara>
After this assignment, you'd expect $a's value to be 5 as
well, so if you wrote $b = $a, you'd expect it to behave just as
if you wrote $b = 5. In other words, $a is an expression with the
value of 5 as well. If everything works right, this is exactly
what will happen.
<para>
Slightly more complex examples for expressions are functions. For
instance, consider the following function:
<informalexample>
<programlisting>
! function foo()
! {
! return 5;
! }
</programlisting>
</informalexample>
--- 14,90 ----
<sect1 id="constants">
<title>Constants</title>
+
<simpara>
PHP defines a few constants and provides a mechanism for defining
more at run-time. Constants are much like variables, but they have
a slightly different syntax.
+
<para>
The pre-defined constants are __FILE__ and __LINE__, which
correspond to the filename and line number being processed when
they are encountered.
!
! <example>
! <title>Using __FILE__ and __LINE__</title>
<programlisting>
<?php
! function report_error($file, $line, $message) {
! echo "An error occured in $file on line $line: $message.";
! }
!
! report_error(__FILE__,__LINE__, "Something went wrong!");
?>
</programlisting>
</example>
+
<para>
! You can define additional constants using the
! <function>define</function> and <function>undefine</function>
! functions.
!
! <example>
! <title>Defining Constants</title>
<programlisting>
<?php
! define("CONSTANT", "Hello world.");
! echo CONSTANT; // outputs "Hello world."
! undefine ("CONSTANT");
?>
</programlisting>
</example>
<sect1 id="expressions">
<title>Expressions</title>
+
<simpara>
Expressions are the most important building stones of PHP. In PHP,
almost anything you write is an expression. The simplest yet
most accurate way to define an expressions is "anything that has a
value".
+
<simpara>
Simple examples that come in mind are constants and variables.
When you type "$a = 5", you're assigning '5' into $a. '5',
obviously, has the value 5, or in other words '5' is an expression
with the value of 5 (in this case, '5' is an integer constant).
+
<simpara>
After this assignment, you'd expect $a's value to be 5 as
well, so if you wrote $b = $a, you'd expect it to behave just as
if you wrote $b = 5. In other words, $a is an expression with the
value of 5 as well. If everything works right, this is exactly
what will happen.
+
<para>
Slightly more complex examples for expressions are functions. For
instance, consider the following function:
+
<informalexample>
<programlisting>
! function foo () {
! return 5;
! }
</programlisting>
</informalexample>
***************
*** 177,182 ****
--- 189,195 ----
<para>
The following example should help you understand pre- and
post-increment and expressions in general a bit better:
+
<informalexample>
<programlisting>
function double($i) {
***************
*** 208,213 ****
--- 221,227 ----
expression followed by a semicolon. In '$b=$a=5;', $a=5 is a
valid expression, but it's not a statement by itself. '$b=$a=5;'
however is a valid statement.
+
<simpara>
One last thing worth mentioning is the truth value of expressions.
In many events, mainly in conditional execution and loops, you're
***************
*** 221,226 ****
--- 235,241 ----
With non-scalar values (arrays and objects) - if the value
contains no elements it's considered FALSE, otherwise it's
considered TRUE.
+
<simpara>
PHP provides a full and powerful implementation of expressions, and
documenting it entirely goes beyond the scope of this manual. The
***************
*** 229,236 ****
--- 244,253 ----
rest of this manual we'll write 'expr' to indicate any valid PHP
expression.
+
<sect1 id="construct.if">
<title>IF</title>
+
<para>
The IF construct is one of the most important features of many
languages, PHP included. It allows for conditional execution of
***************
*** 238,244 ****
that of C:
<informalexample><programlisting>
if (expr)
! statement
</programlisting></informalexample>
<simpara>
--- 255,261 ----
that of C:
<informalexample><programlisting>
if (expr)
! statement
</programlisting></informalexample>
<simpara>
***************
*** 252,258 ****
<informalexample><programlisting>
if ($a > $b)
! print "a is bigger than b";
</programlisting></informalexample>
<para>
--- 269,275 ----
<informalexample><programlisting>
if ($a > $b)
! print "a is bigger than b";
</programlisting></informalexample>
<para>
***************
*** 264,272 ****
value of $a into $b:
<informalexample><programlisting>
! if ($a>$b) {
! print "a is bigger than b";
! $b = $a;
}
</programlisting></informalexample>
--- 281,289 ----
value of $a into $b:
<informalexample><programlisting>
! if ($a > $b) {
! print "a is bigger than b";
! $b = $a;
}
</programlisting></informalexample>
***************
*** 274,283 ****
If statements can be nested indefinitely within other IF
statements, which provides you with complete flexibility for
conditional execution of the various parts of your program.
</sect1>
! <sect1 id="construct.else">
<title>ELSE</title>
<para>
Often you'd want to execute a statement if a certain condition is
met, and a different statement if the condition is not met. This
--- 291,303 ----
If statements can be nested indefinitely within other IF
statements, which provides you with complete flexibility for
conditional execution of the various parts of your program.
+
</sect1>
!
! <sect1 id="construct.else">
<title>ELSE</title>
+
<para>
Often you'd want to execute a statement if a certain condition is
met, and a different statement if the condition is not met. This
***************
*** 294,306 ****
} else {
print "a is NOT bigger than b";
}
!
! </programlisting></informalexample>
The ELSE statement is only executed if the IF expression evaluated
to FALSE, and if there were any ELSEIF expressions - only if they
evaluated to FALSE as well (see below).
<sect1 id="construct.elseif">
<title>ELSEIF</title>
--- 314,327 ----
} else {
print "a is NOT bigger than b";
}
! </programlisting>
! </informalexample>
The ELSE statement is only executed if the IF expression evaluated
to FALSE, and if there were any ELSEIF expressions - only if they
evaluated to FALSE as well (see below).
+
<sect1 id="construct.elseif">
<title>ELSEIF</title>
***************
*** 313,319 ****
following code would display 'a is bigger than b' if $a>$b, 'a is
equal to b' if $a==$b, and 'a is smaller than b' if $a<$b:
! <informalexample><programlisting>
if ($a > $b) {
print "a is bigger than b";
} elseif ($a == $b) {
--- 334,341 ----
following code would display 'a is bigger than b' if $a>$b, 'a is
equal to b' if $a==$b, and 'a is smaller than b' if $a<$b:
! <informalexample>
! <programlisting>
if ($a > $b) {
print "a is bigger than b";
} elseif ($a == $b) {
***************
*** 321,327 ****
} else {
print "a is smaller than b";
}
! </programlisting></informalexample>
<simpara>
There may be several ELSEIFs within the same IF statement. The
--- 343,350 ----
} else {
print "a is smaller than b";
}
! </programlisting>
! </informalexample>
<simpara>
There may be several ELSEIFs within the same IF statement. The
***************
*** 348,358 ****
list of one or more statements, and end with ENDIF;. Consider
the following example:
! <informalexample><programlisting>
<?php if ($a==5): ?>
A = 5
<?php endif; ?>
! </programlisting></informalexample>
<simpara>
In the above example, the HTML block "A = 5" is nested within an
--- 371,383 ----
list of one or more statements, and end with ENDIF;. Consider
the following example:
! <informalexample>
! <programlisting>
<?php if ($a==5): ?>
A = 5
<?php endif; ?>
! </programlisting>
! </informalexample>
<simpara>
In the above example, the HTML block "A = 5" is nested within an
***************
*** 364,380 ****
The following is an IF statement with ELSEIF and ELSE in the
alternative format:
! <informalexample><programlisting>
! if ($a==5):
print "a equals 5";
print "...";
! elseif ($a==6):
print "a equals 6";
print "!!!";
else:
print "a is neither 5 nor 6";
endif;
! </programlisting></informalexample>
<sect1 id="construct.while">
<title>WHILE</title>
--- 389,408 ----
The following is an IF statement with ELSEIF and ELSE in the
alternative format:
! <informalexample>
! <programlisting>
! if ($a == 5):
print "a equals 5";
print "...";
! elseif ($a == 6):
print "a equals 6";
print "!!!";
else:
print "a is neither 5 nor 6";
endif;
! </programlisting>
! </informalexample>
!
<sect1 id="construct.while">
<title>WHILE</title>
***************
*** 384,392 ****
just like their C counterparts. The basic form of a WHILE
statement is:
! <informalexample><programlisting>
! WHILE(expr) statement
! </programlisting></informalexample>
<simpara>
The meaning of a WHILE statement is simple. It tells PHP to
--- 412,422 ----
just like their C counterparts. The basic form of a WHILE
statement is:
! <informalexample>
! <programlisting>
! WHILE(expr) statement
! </programlisting>
! </informalexample>
<simpara>
The meaning of a WHILE statement is simple. It tells PHP to
***************
*** 400,410 ****
beginning, the nested statement(s) won't even be run once.
<para>
! Like with the IF statement, you can group multiple statements
! within the same WHILE loop by surrounding a group of statements
! with curly braces, OR by using the alternate syntax:
! <informalexample><programlisting>
WHILE(expr): statement ... ENDWHILE;
</programlisting></informalexample>
--- 430,441 ----
beginning, the nested statement(s) won't even be run once.
<para>
! Like with the IF statement, you can group multiple statements
! within the same WHILE loop by surrounding a group of statements
! with curly braces, OR by using the alternate syntax:
! <informalexample>
! <programlisting>
WHILE(expr): statement ... ENDWHILE;
</programlisting></informalexample>
***************
*** 412,437 ****
The following examples are identical, and both print numbers from
1 to 10:
! <informalexample><programlisting>
/* example 1 */
! $i=1;
! while ($i<=10) {
print $i++; /* the printed value would be
$i before the increment
(post-increment) */
}
/* example 2 */
! $i=1;
! while ($i<=10):
print $i;
$i++;
endwhile;
! </programlisting></informalexample>
<sect1 id="construct.do.while">
<title>DO..WHILE</title>
<simpara>
DO..WHILE loops are very similar to WHILE loops, except the truth
expression is checked at the end of each iteration instead of in
--- 443,473 ----
The following examples are identical, and both print numbers from
1 to 10:
! <informalexample>
! <programlisting>
/* example 1 */
!
! $i = 1;
! while ($i <= 10) {
print $i++; /* the printed value would be
$i before the increment
(post-increment) */
}
/* example 2 */
!
! $i = 1;
! while ($i <= 10):
print $i;
$i++;
endwhile;
! </programlisting>
! </informalexample>
<sect1 id="construct.do.while">
<title>DO..WHILE</title>
+
<simpara>
DO..WHILE loops are very similar to WHILE loops, except the truth
expression is checked at the end of each iteration instead of in
***************
*** 445,456 ****
<para>
There is just one syntax for DO..WHILE loops:
! <informalexample><programlisting>
$i = 0;
do {
print $i;
} while ($i>0);
! </programlisting></informalexample>
<simpara>
The above loop would run one time exactly, since after the first
--- 481,494 ----
<para>
There is just one syntax for DO..WHILE loops:
! <informalexample>
! <programlisting>
$i = 0;
do {
print $i;
} while ($i>0);
! </programlisting>
! </informalexample>
<simpara>
The above loop would run one time exactly, since after the first
***************
*** 462,468 ****
DO..WHILE loop, to allow stopping execution in the middle of code
blocks, by encapsulating them with DO..WHILE(0), and using the
BREAK statement. The following code fragment demonstrates this:
! <informalexample><programlisting>
do {
if ($i < 5) {
print "i is not big enough";
--- 500,508 ----
DO..WHILE loop, to allow stopping execution in the middle of code
blocks, by encapsulating them with DO..WHILE(0), and using the
BREAK statement. The following code fragment demonstrates this:
!
! <informalexample>
! <programlisting>
do {
if ($i < 5) {
print "i is not big enough";
***************
*** 475,487 ****
print "i is ok";
...process i...
} while(0);
! </programlisting></informalexample>
<simpara>
Don't worry if you don't understand this right away or at all.
You can code scripts and even powerful scripts without using this
`feature'.
<sect1 id="construct.for">
<title>FOR</title>
--- 515,529 ----
print "i is ok";
...process i...
} while(0);
! </programlisting>
! </informalexample>
<simpara>
Don't worry if you don't understand this right away or at all.
You can code scripts and even powerful scripts without using this
`feature'.
+
<sect1 id="construct.for">
<title>FOR</title>
***************
*** 489,525 ****
FOR loops are the most complex loops in PHP. They behave like
their C counterparts. The syntax of a FOR loop is:
! <informalexample><programlisting>
! FOR (expr1; expr2; expr3) statement
! </programlisting></informalexample>
<simpara>
The first expression (expr1) is evaluated (executed)
unconditionally at the beginning of the loop.
<simpara>
In the beginning of each iteration, expr2 is evaluated. If it
evaluates to TRUE, the loop continues and the nested statement(s)
are executed. If it evaluates to FALSE, the execution of the loop
ends.
<simpara>
At the end of each iteration, expr3 is evaluated (executed).
<simpara>
Each of the expressions can be empty. expr2 being empty means the
loop should be run indefinitely (PHP implicitly considers it as
TRUE, like C). This may not be as useless as you might think,
since often you'd want to end the loop using a conditional BREAK
statement instead of using the FOR truth expression.
<para>
Consider the following examples. All of them display numbers from
1 to 10:
! <informalexample><programlisting>
/* example 1 */
! for ($i=1; $i<=10; $i++) {
print $i;
}
/* example 2 */
for ($i = 1;;$i++) {
if ($i > 10) {
break;
--- 531,576 ----
FOR loops are the most complex loops in PHP. They behave like
their C counterparts. The syntax of a FOR loop is:
! <informalexample>
! <programlisting>
! FOR (expr1; expr2; expr3) statement
! </programlisting>
! </informalexample>
<simpara>
The first expression (expr1) is evaluated (executed)
unconditionally at the beginning of the loop.
+
<simpara>
In the beginning of each iteration, expr2 is evaluated. If it
evaluates to TRUE, the loop continues and the nested statement(s)
are executed. If it evaluates to FALSE, the execution of the loop
ends.
+
<simpara>
At the end of each iteration, expr3 is evaluated (executed).
+
<simpara>
Each of the expressions can be empty. expr2 being empty means the
loop should be run indefinitely (PHP implicitly considers it as
TRUE, like C). This may not be as useless as you might think,
since often you'd want to end the loop using a conditional BREAK
statement instead of using the FOR truth expression.
+
<para>
Consider the following examples. All of them display numbers from
1 to 10:
! <informalexample>
! <programlisting>
/* example 1 */
!
! for ($i = 1; $i <= 10; $i++) {
print $i;
}
/* example 2 */
+
for ($i = 1;;$i++) {
if ($i > 10) {
break;
***************
*** 528,533 ****
--- 579,585 ----
}
/* example 3 */
+
$i = 1;
for (;;) {
if ($i > 10) {
***************
*** 538,546 ****
}
/* example 4 */
- for ($i=1; $i<=10; print $i, $i++) ;
! </programlisting></informalexample>
<simpara>
Of course, the first example appears to be the nicest one (or
--- 590,599 ----
}
/* example 4 */
! for ($i = 1; $i <= 10; print $i, $i++) ;
! </programlisting>
! </informalexample>
<simpara>
Of course, the first example appears to be the nicest one (or
***************
*** 550,558 ****
<para>
PHP also supports the alternate "colon syntax" for FOR loops.
! <informalexample><programlisting>
! FOR (expr1; expr2; expr3): statement; ...; endfor;
! </programlisting></informalexample>
<para>
Other languages have a foreach statement to traverse an array or
--- 603,613 ----
<para>
PHP also supports the alternate "colon syntax" for FOR loops.
! <informalexample>
! <programlisting>
! FOR (expr1; expr2; expr3): statement; ...; endfor;
! </programlisting>
! </informalexample>
<para>
Other languages have a foreach statement to traverse an array or
***************
*** 561,573 ****
for this. See the documentation for these functions for an
example.
<sect1 id="construct.break">
<title>BREAK</title>
<para>
BREAK breaks out of the current looping construct.
! <informalexample><programlisting>
$i = 0;
while ($i < 10) {
if ($arr[$i] == "stop") {
--- 616,630 ----
for this. See the documentation for these functions for an
example.
+
<sect1 id="construct.break">
<title>BREAK</title>
<para>
BREAK breaks out of the current looping construct.
! <informalexample>
! <programlisting>
$i = 0;
while ($i < 10) {
if ($arr[$i] == "stop") {
***************
*** 575,596 ****
}
$i++;
}
! </programlisting></informalexample>
<sect1 id="construct.continue">
<title>CONTINUE</title>
<para>
! CONTINUE jumps to the beginning of enclosing the looping construct.
! <informalexample><programlisting>
while (list($key,$value) = each($arr)) {
if ($key % 2) { // skip even members
continue;
}
! do_something_odd($value);
}
! </programlisting></informalexample>
<sect1 id="construct.switch">
<title>SWITCH</title>
--- 632,658 ----
}
$i++;
}
! </programlisting>
! </informalexample>
!
<sect1 id="construct.continue">
<title>CONTINUE</title>
<para>
! CONTINUE jumps to the beginning of enclosing the looping
! construct.
! <informalexample>
! <programlisting>
while (list($key,$value) = each($arr)) {
if ($key % 2) { // skip even members
continue;
}
! do_something_odd ($value);
}
! </programlisting>
! </informalexample>
<sect1 id="construct.switch">
<title>SWITCH</title>
***************
*** 601,613 ****
the same variable (or expression) with many different values, and
execute a different piece of code depending on which value it
equals to. This is exactly what the SWITCH statement is for.
<para>
The following two examples are two different ways to write the
same thing, one using a series of IF statements, and the other
using the SWITCH statement:
! <informalexample><programlisting>
/* example 1 */
if ($i == 0) {
print "i equals 0";
}
--- 663,678 ----
the same variable (or expression) with many different values, and
execute a different piece of code depending on which value it
equals to. This is exactly what the SWITCH statement is for.
+
<para>
The following two examples are two different ways to write the
same thing, one using a series of IF statements, and the other
using the SWITCH statement:
! <informalexample>
! <programlisting>
/* example 1 */
+
if ($i == 0) {
print "i equals 0";
}
***************
*** 619,624 ****
--- 684,690 ----
}
/* example 2 */
+
switch ($i) {
case 0:
print "i equals 0";
***************
*** 630,636 ****
print "i equals 2";
break;
}
! </programlisting></informalexample>
<para>
It is important to understand how the SWITCH statement is executed
--- 696,703 ----
print "i equals 2";
break;
}
! </programlisting>
! </informalexample>
<para>
It is important to understand how the SWITCH statement is executed
***************
*** 644,651 ****
the end of a case's statement list, PHP will go on executing the
statements of the following case. For example:
! <informalexample><programlisting>
/* example 3 */
switch ($i) {
case 0:
print "i equals 0";
--- 711,720 ----
the end of a case's statement list, PHP will go on executing the
statements of the following case. For example:
! <informalexample>
! <programlisting>
/* example 3 */
+
switch ($i) {
case 0:
print "i equals 0";
***************
*** 654,660 ****
case 2:
print "i equals 2";
}
! </programlisting></informalexample>
<simpara>
Here, if $i equals to 0, PHP would execute all of the print
--- 723,730 ----
case 2:
print "i equals 2";
}
! </programlisting>
! </informalexample>
<simpara>
Here, if $i equals to 0, PHP would execute all of the print
***************
*** 669,676 ****
A special case is the default case. This case matches anything
that wasn't matched by the other cases. For example:
! <informalexample><programlisting>
/* example 4 */
switch ($i) {
case 0:
print "i equals 0";
--- 739,748 ----
A special case is the default case. This case matches anything
that wasn't matched by the other cases. For example:
! <informalexample>
! <programlisting>
/* example 4 */
+
switch ($i) {
case 0:
print "i equals 0";
***************
*** 684,727 ****
default:
print "i is not equal to 0, 1 or 2";
}
! </programlisting></informalexample>
! <para>
Another fact worth mentioning is that the CASE expression may be
any expression that evaluates to a scalar type, that is, integer
or real numbers and strings. Arrays or objects won't crash PHP,
but they're meaningless in that context.
<sect1 id="function.require">
<title>REQUIRE</title>
<simpara>
The REQUIRE statement replaces itself with the specified file,
much like the C preprocessor's #include works.
<para>
This means that you can't put a require() statement inside of
a loop structure and expect it to include the contents of a
different file on each iteration. To do that, use an INCLUDE
statement.
! <informalexample><programlisting>
! require('header.inc');
! </programlisting></informalexample>
<sect1 id="function.include">
<title>INCLUDE</title>
<simpara>
The INCLUDE statement includes and evaluates the specified file.
<para>
This happens each time the INCLUDE statement is encountered,
so you can use an INCLUDE statement within a looping structure
to include a number of different file.
! <informalexample><programlisting>
! $files = array('first.inc', 'second.inc', 'third.inc');
for ($i = 0; $i < count($files); $i++) {
! include($files[$i]);
}
! </programlisting></informalexample>
<para>
<function>include</function> differs from
--- 756,809 ----
default:
print "i is not equal to 0, 1 or 2";
}
! </programlisting>
! </informalexample>
! <para>
Another fact worth mentioning is that the CASE expression may be
any expression that evaluates to a scalar type, that is, integer
or real numbers and strings. Arrays or objects won't crash PHP,
but they're meaningless in that context.
+
<sect1 id="function.require">
<title>REQUIRE</title>
+
<simpara>
The REQUIRE statement replaces itself with the specified file,
much like the C preprocessor's #include works.
+
<para>
This means that you can't put a require() statement inside of
a loop structure and expect it to include the contents of a
different file on each iteration. To do that, use an INCLUDE
statement.
! <informalexample>
! <programlisting>
! require ('header.inc');
! </programlisting>
! </informalexample>
<sect1 id="function.include">
<title>INCLUDE</title>
+
<simpara>
The INCLUDE statement includes and evaluates the specified file.
+
<para>
This happens each time the INCLUDE statement is encountered,
so you can use an INCLUDE statement within a looping structure
to include a number of different file.
! <informalexample>
! <programlisting>
! $files = array ('first.inc', 'second.inc', 'third.inc');
for ($i = 0; $i < count($files); $i++) {
! include($files[$i]);
}
! </programlisting>
! </informalexample>
<para>
<function>include</function> differs from
***************
*** 734,757 ****
evaluated to false).
<para>
! Because <function>include</function> is a special language construct, you
! must enclose it within a statement block if it is inside a conditional
! block.
! <informalexample><programlisting>
/* This is WRONG and will not work as desired. */
if ($condition)
include($file);
else
include($other);
/* This is CORRECT. */
if ($condition) {
include($file);
} else {
include($other);
}
! </programlisting></informalexample>
<para>
When the file is evaluated, the parser beings in "HTML-mode" which
--- 816,843 ----
evaluated to false).
<para>
! Because <function>include</function> is a special language
! construct, you must enclose it within a statement block if it is
! inside a conditional block.
! <informalexample>
! <programlisting>
/* This is WRONG and will not work as desired. */
+
if ($condition)
include($file);
else
include($other);
/* This is CORRECT. */
+
if ($condition) {
include($file);
} else {
include($other);
}
! </programlisting>
! </informalexample>
<para>
When the file is evaluated, the parser beings in "HTML-mode" which
***************
*** 762,798 ****
<sect1 id="keyword.function">
<title>FUNCTION</title>
<para>
A function may be defined using syntax such as the following:
<informalexample>
<programlisting>
! function foo( $arg_1, $arg_2, ..., $arg_n ) {
! echo "Example function.\n";
! return $retval;
}
</programlisting>
</informalexample>
<simpara>
Any valid PHP3 code may appear inside a function, even other
! functions and <link linkend="keyword.class">class</link> definitions.
<simpara>
Functions must be defined before they are referenced.
<sect2 id="keyword.function.return">
<title>Returning values</title>
<para>
! Values are returned by using the optional return statement. Any
! type may be returned, including lists and objects.
<informalexample>
<programlisting>
! function my_sqrt( $num ) {
! return $num * $num;
}
! echo my_sqrt( 4 ); // outputs '16'.
</programlisting>
</informalexample>
--- 848,887 ----
<sect1 id="keyword.function">
<title>FUNCTION</title>
+
<para>
A function may be defined using syntax such as the following:
<informalexample>
<programlisting>
! function foo ($arg_1, $arg_2, ..., $arg_n) {
! echo "Example function.\n";
! return $retval;
}
</programlisting>
</informalexample>
<simpara>
Any valid PHP3 code may appear inside a function, even other
! functions and <link linkend="keyword.class">class</link>
! definitions.
<simpara>
Functions must be defined before they are referenced.
<sect2 id="keyword.function.return">
<title>Returning values</title>
+
<para>
! Values are returned by using the optional return statement. Any
! type may be returned, including lists and objects.
<informalexample>
<programlisting>
! function my_sqrt ($num) {
! return $num * $num;
}
! echo my_sqrt (4); // outputs '16'.
</programlisting>
</informalexample>
***************
*** 803,815 ****
<informalexample>
<programlisting>
function foo() {
! return array( 0, 1, 2 );
}
! list( $zero, $one, $two ) = foo();
</programlisting>
</informalexample>
</sect2>
<sect2 id="keyword.function.arguments">
<title>Arguments</title>
--- 892,906 ----
<informalexample>
<programlisting>
function foo() {
! return array (0, 1, 2);
}
! list ($zero, $one, $two) = foo();
</programlisting>
</informalexample>
+
</sect2>
+
<sect2 id="keyword.function.arguments">
<title>Arguments</title>
***************
*** 825,835 ****
values</link>. Variable-length argument lists are not supported,
but a similar effect may be achieved by passing arrays.
! <informalexample><programlisting>
function takes_array($input) {
! echo "$input[0] + $input[1] = ", $input[0]+$input[1];
}
! </programlisting></informalexample>
<sect3 id="keyword.function.arguments.reference">
<title>Passing by reference</title>
--- 916,928 ----
values</link>. Variable-length argument lists are not supported,
but a similar effect may be achieved by passing arrays.
! <informalexample>
! <programlisting>
function takes_array($input) {
! echo "$input[0] + $input[1] = ", $input[0]+$input[1];
}
! </programlisting>
! </informalexample>
<sect3 id="keyword.function.arguments.reference">
<title>Passing by reference</title>
***************
*** 850,856 ****
$bar .= ' and something extra.';
}
$str = 'This is a string, ';
! foo2( $str );
echo $str; // outputs 'This is a string, and something extra.'
</programlisting>
</informalexample>
--- 943,949 ----
$bar .= ' and something extra.';
}
$str = 'This is a string, ';
! foo ($str);
echo $str; // outputs 'This is a string, and something extra.'
</programlisting>
</informalexample>
***************
*** 862,874 ****
<informalexample>
<programlisting>
! function foo( $bar ) {
$bar .= ' and something extra.';
}
$str = 'This is a string, ';
! foo2( $str );
echo $str; // outputs 'This is a string, '
! foo2( &$str );
echo $str; // outputs 'This is a string, and something extra.'
</programlisting>
</informalexample>
--- 955,967 ----
<informalexample>
<programlisting>
! function foo ($bar) {
$bar .= ' and something extra.';
}
$str = 'This is a string, ';
! foo ($str);
echo $str; // outputs 'This is a string, '
! foo (&$str);
echo $str; // outputs 'This is a string, and something extra.'
</programlisting>
</informalexample>
***************
*** 877,897 ****
<sect3 id="keyword.function.arguments.default">
<title>Default values</title>
<para>
A function may define C++-style default values for scalar
arguments as follows:
<informalexample>
<programlisting>
! function makecoffee( $type = "cappucino" ) {
echo "Making a cup of $type.\n";
}
! echo makecoffee();
! echo makecoffee( "espresso" );
</programlisting>
</informalexample>
<para>
The output from the above snippet is:
<screen>
Making a cup of cappucino.
Making a cup of espresso.
--- 970,993 ----
<sect3 id="keyword.function.arguments.default">
<title>Default values</title>
+
<para>
A function may define C++-style default values for scalar
arguments as follows:
+
<informalexample>
<programlisting>
! function makecoffee ($type = "cappucino") {
echo "Making a cup of $type.\n";
}
! echo makecoffee ();
! echo makecoffee ("espresso");
</programlisting>
</informalexample>
<para>
The output from the above snippet is:
+
<screen>
Making a cup of cappucino.
Making a cup of espresso.
***************
*** 902,922 ****
example) a variable or class member.
<para>
! Note that when using default arguments, any defaults should be on
! the right side of any non-default arguments; otherwise, things
! will not work as expected. Consider the following code snippet:
<informalexample>
<programlisting>
! function makeyogurt( $type = "acidophilus", $flavour ) {
return "Making a bowl of $type $flavour.\n";
}
! echo makeyogurt( "raspberry" ); // won't work as expected
</programlisting>
</informalexample>
<para>
The output of the above example is:
<screen>
Warning: Missing argument 2 in call to makeyogurt() in
/usr/local/etc/httpd/htdocs/php3test/functest.html on line 41
--- 998,1021 ----
example) a variable or class member.
<para>
! Note that when using default arguments, any defaults should be
! on the right side of any non-default arguments; otherwise,
! things will not work as expected. Consider the following code
! snippet:
<informalexample>
<programlisting>
! function makeyogurt ($type = "acidophilus", $flavour) {
return "Making a bowl of $type $flavour.\n";
}
!
! echo makeyogurt ("raspberry"); // won't work as expected
</programlisting>
</informalexample>
<para>
The output of the above example is:
+
<screen>
Warning: Missing argument 2 in call to makeyogurt() in
/usr/local/etc/httpd/htdocs/php3test/functest.html on line 41
***************
*** 925,951 ****
<para>
Now, compare the above with this:
<informalexample>
<programlisting>
! function makeyogurt( $flavour, $type = "acidophilus" ) {
return "Making a bowl of $type $flavour.\n";
}
! echo makeyogurt( "raspberry" ); // works as expected
</programlisting>
</informalexample>
<para>
The output of this example is:
<screen>
Making a bowl of acidophilus raspberry.
</screen>
</sect3>
</sect2>
</sect1>
<sect1 id="keyword.old-function">
<title>OLD_FUNCTION</title>
<simpara>
The OLD_FUNCTION statement allows you to declare a function using
a syntax identical to PHP/FI2 (except you must replace 'function'
--- 1024,1057 ----
<para>
Now, compare the above with this:
+
<informalexample>
<programlisting>
! function makeyogurt ($flavour, $type = "acidophilus") {
return "Making a bowl of $type $flavour.\n";
}
!
! echo makeyogurt ("raspberry"); // works as expected
</programlisting>
</informalexample>
<para>
The output of this example is:
+
<screen>
Making a bowl of acidophilus raspberry.
</screen>
+
</sect3>
</sect2>
+
</sect1>
+
<sect1 id="keyword.old-function">
<title>OLD_FUNCTION</title>
+
<simpara>
The OLD_FUNCTION statement allows you to declare a function using
a syntax identical to PHP/FI2 (except you must replace 'function'
***************
*** 977,989 ****
// Add $num articles of $artnr to the cart
! function add_item($artnr, $num) {
$this->items[$artnr] += $num;
}
// Take $num articles of $artnr out of the cart
! function remove_item($artnr, $num) {
if ($this->items[$artnr] > $num) {
$this->items[$artnr] -= $num;
return true;
--- 1083,1095 ----
// Add $num articles of $artnr to the cart
! function add_item ($artnr, $num) {
$this->items[$artnr] += $num;
}
// Take $num articles of $artnr out of the cart
! function remove_item ($artnr, $num) {
if ($this->items[$artnr] > $num) {
$this->items[$artnr] -= $num;
return true;
***************
*** 993,1017 ****
}
}
?>
! </programlisting>
! </informalexample>
<para>
This defines a class named Cart that consists of an associative
array of articles in the cart and two functions to add and remove
items from this cart.
</para><para>
Classes are types, that is, they are blueprints for actual
variables. You have to create a variables of the desired type with
the new operator.
</para>
! <informalexample>
! <programlisting role=php>
$cart = new Cart;
$cart->add_item("10", 1);
! </programlisting>
! </informalexample>
<para>
This creates an object $cart of the class Cart. The function
--- 1099,1124 ----
}
}
?>
! </programlisting>
! </informalexample>
<para>
This defines a class named Cart that consists of an associative
array of articles in the cart and two functions to add and remove
items from this cart.
+
</para><para>
Classes are types, that is, they are blueprints for actual
variables. You have to create a variables of the desired type with
the new operator.
</para>
! <informalexample>
! <programlisting role=php>
$cart = new Cart;
$cart->add_item("10", 1);
! </programlisting>
! </informalexample>
<para>
This creates an object $cart of the class Cart. The function
***************
*** 1020,1026 ****
</para><para>
Classes can be extensions of other classes. The extended or
derived class has all variables and functions of the base class
! and what you add in the extended defintion. This is done using
the extends keyword.
</para>
--- 1127,1133 ----
</para><para>
Classes can be extensions of other classes. The extended or
derived class has all variables and functions of the base class
! and what you add in the extended definition. This is done using
the extends keyword.
</para>
***************
*** 1029,1040 ****
class Named_Cart extends Cart {
var $owner;
! function set_owner($name) {
$this->owner = $name;
}
}
! </programlisting>
! </informalexample>
<para>
This defines a class Named_Cart that has all variables and
--- 1136,1147 ----
class Named_Cart extends Cart {
var $owner;
! function set_owner ($name) {
$this->owner = $name;
}
}
! </programlisting>
! </informalexample>
<para>
This defines a class Named_Cart that has all variables and
***************
*** 1046,1057 ****
<informalexample>
<programlisting role=php>
! $ncart = new Named_Cart; // Create a named cart
! $ncart->set_owner("kris"); // Name that cart
! print $ncart->owner; // print the cart owners name
! $ncart->add_item("10", 1); // (inherited functionality from cart)
! </programlisting>
! </informalexample>
<para>
Within functions of a class the variable $this means this
--- 1153,1164 ----
<informalexample>
<programlisting role=php>
! $ncart = new Named_Cart; // Create a named cart
! $ncart->set_owner ("kris"); // Name that cart
! print $ncart->owner; // print the cart owners name
! $ncart->add_item ("10", 1); // (inherited functionality from cart)
! </programlisting>
! </informalexample>
<para>
Within functions of a class the variable $this means this
***************
*** 1064,1077 ****
called when you create a new instance of a class. A function
becomes a constructur when it has the same name as the class.
</para>
<informalexample>
<programlisting role=php>
class Auto_Cart extends Cart {
! function Auto_Cart() {
! $this->add_item("10", 1);
}
}
! </programlisting>
</informalexample>
<para>
--- 1171,1185 ----
called when you create a new instance of a class. A function
becomes a constructur when it has the same name as the class.
</para>
+
<informalexample>
<programlisting role=php>
class Auto_Cart extends Cart {
! function Auto_Cart () {
! $this->add_item ("10", 1);
}
}
! </programlisting>
</informalexample>
<para>
***************
*** 1081,1091 ****
can also take arguments and these arguments can be optional, which
makes them much more useful.
</para>
<informalexample>
<programlisting role=php>
class Constructor_Cart {
! function Constructor_Cart($item = "10", $num = 1) {
! $this->add_item($item, $num);
}
}
--- 1189,1200 ----
can also take arguments and these arguments can be optional, which
makes them much more useful.
</para>
+
<informalexample>
<programlisting role=php>
class Constructor_Cart {
! function Constructor_Cart ($item = "10", $num = 1) {
! $this->add_item ($item, $num);
}
}
***************
*** 1095,1103 ****
// Shop for real...
! $different_cart = new Constructor_Cart("20", 17);
! </programlisting>
</informalexample>
</sect1>
<!-- Keep this comment at the end of the file
--- 1204,1213 ----
// Shop for real...
! $different_cart = new Constructor_Cart ("20", 17);
! </programlisting>
</informalexample>
+
</sect1>
<!-- Keep this comment at the end of the file
--
PHP Development Mailing List http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net
For help: php-dev-help@lists.php.net