分类:
2008-05-14 09:03:34
if (expr)
statement
if ($a >$b)
print "a is bigger than b";
if ($a>$b) {
print "a is bigger than b";
}
else {
print "a is NOT bigger than b";
}
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;
WHILE(expr): statement ... ENDWHILE;
下面例子完全相同, 都打出数字 1 到 10: /* 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;
5、DO..WHILE语句 $i = 0;
do {
print $i;
} while ($i>0);
FOR (expr1; expr2; expr3) statement
第一个表达式(expr1)在循环开始时无条件的计算(执行)。 /* example 1 */
for ($i=1; $i<=10; $i++) {
print $i;
}
/* example 2 */
for ($i = 1;;$i++) {
if ($i >10) {
break;
}
print $i;
}
/* example 3 */
$i = 1;
for (;;) {
if ($i >10) {
break;
}
print $i;
$i++;
}
/* example 1 */
if ($i == 0) {
print "i equals 0";
}
if ($i == 1) {
print "i equals 1";
}
if ($i == 2) {
print "i equals 2";
}
/* example 2 */
switch ($i) {
case 0:
print "i equals 0";
break;
case 1:
print "i equals 1";
break;
case 2:
print "i equals 2";
break;
}
require(’header.inc’);
(三)、 INCLUDE语句 $files = array(’first.inc’, ’second.inc’, ’third.inc’);
for ($i = 0; $i < count($files); $i++) {
include($files[$i]);
}
function foo( $arg_1, $arg_2, ..., $arg_n ) {
echo "Example function.\n";
return $retval;
}
function my_sqrt( $num ) {
return $num * $num;
}
echo my_sqrt( 4 ); // outputs ’16’.
function foo() {
return array( 0, 1, 2 );
}
list( $zero, $one, $two ) = foo();
function foo( &$bar ) {
$bar .= ’ and something extra.’;
}
$str = ’This is a string, ’;
foo( $str );
echo $str; // outputs ’This is a string, and something extra.’
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.’
function makecoffee( $type = "cappucino" ) {
echo "Making a cup of $type.\n";
}
echo makecoffee();
echo makecoffee( "espresso" );
Making a cup of cappucino.
Making a cup of espresso.
注意,当使用默认参数时,所有有默认值的参数应在无默认值的参数的后边定义;否则,将不会按所想的那样工作。 <?php
class Cart {
var $items; // Items in our shopping cart
// 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;
} else {
return false;
}
}
}
?>
$cart = new Cart;
$cart->add_item("10", 1);
class Named_Cart extends Cart {
var $owner;
function set_owner($name) {
$this->owner = $name;
}
}
$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)
class Auto_Cart extends Cart {
function Auto_Cart() {
$this->add_item("10", 1);
}
}
class Constructor_Cart {
function Constructor_Cart($item = "10", $num = 1) {
$this->add_item($item, $num);
}
}
// Shop the same old boring stuff.
$default_cart = new Constructor_Cart;
// Shop for real...
$different_cart = new Constructor_Cart("20", 17);