非履行语句:
所谓非履行语句,就是PHP需求盘算,但其实不需求履行甚么举措的语句。例如:语句10 + 20,其值是30,但因为没有改动哪一个变量值,所以也不需求作甚么举措。了局值将不保存,当下一条语句呈现时,它很快就被抛弃了。
甚么举措也不需求做,那末非履行语句又有甚么用呢?我不清晰,假如你能发明它们的用处请告知我。
履行语句:
履行语句是经由过程表达式来履行某些举措。它们可以增添或减小某一变量值,或挪用一个函数。履行语句是PHP中利用最多的一种语句类型。
赋值语句:
赋值语句其实不庞杂,它们可以给一个变量付与一个数值。关于赋值操作符在前一章“PHP中的数据处置”中已讲过,在此不再过量赘述。
判别语句:
判别语句利用if 和switch关头字,基于一个表达式的盘算了局以决意履行某一段语句,或基于表达式的了局在两段语句行当选择履行个中的一个。例如,假如要处置的支票值大于1000美圆,则履行一段法式;假如支票数小于1000美圆,就履行另外一段法式。
if关头字
在if语句中经由过程盘算表达式,得出真或假值,依据所求出的真、假值来决意履行哪一段法式。
最多见的有以下三品种型的if语句:
1.
if ( EXPRESSION ) {
// code block to be executed when
// expression is true.
}
2.
if ( EXPRESSION ) {
// code block to be executed when
// expression is true.
}
else {
// code block to be executed when
// expression is false.
}
3.
if ( EXPRESSION_1 ) {
// code block to be executed when
// expression_1 is true.
}
elseif ( EXPRESSION_2 ) {
// code block to be executed when
// expression_2 is true.
}
else {
// code blcok to be executed when
// all expression are false.
}
if ($str_input == 'A') {
// do A statements.
}
elseif ($str_input == '8') {
// do B statements.
}
else {
echo "Unrecognized Input Error: '$str_input' is unknown.<br>";
}
switch ($str_input) {
// The print and echo case perform the same task,so
// the print case needs no break keyword.
case 'print':
case 'echo':
// do the echo task.
break;
case 'check_balance':
// do the check balance task.
break;
case 1:
case 2:
// add $str_input to something.
break;
default:
echo "You have entered an unrecognized command or are ";
echo "trying to add a number other than 1 or 2.";
break;
}
// The following for loop places each
// expression in the loop header on a separate
// line to enhance readability.
For ($first_time = 1,$index =1;
$index <= $int_number_of_items;
$index += 2, $first_time = 0
) {
if ($first_time) {
echo "Report Header<br><br>";
}
each "Report Line $index<br>";
}
if (! $first_time) {
echo "<br>Report Footer<br>";
}
代码行输入显示以下:
Report Line 1
Report Line 3
Report Line 4
Report Line 5
Report Line 9
$iindex = 0;
while ( $iindex < 5 ) :
echo "inside while statement: $iindex<br>";
$iindex++;
endwhile;
echo "outside while statement: $iindex<br>";
此例子了局将显示
inside while statement: 0
inside while statement: 1
inside while statement: 2
inside while statement: 3
inside while statement: 4
outside while statement: 5
几近一切的轮回都可以用while语句或do语句来暗示。喜好如何表达法式的逻辑布局,感觉哪一种办法利用起来更随手,都可有可无。有时,利用do print_the_page while the number of pages is less than 20如许表述比while the number of page is less than 20的表述更容易于了解。
以下do语句是本章较早利用while表述例子的另外一个版本。
$iindex = 0;
do {
echo "Inside Do Statement: $iindex<br>";
$iindex++;
} while ($iindex < 5);
echo "Outside Do Statement: $iindex<br>";
此例子的了局将显示
Inside Do Statement: 0
Inside Do Statement: 1
Inside Do Statement: 2
Inside Do Statement: 3
Inside Do Statement: 4
Outside Do Statement: 5
清单4.1 break.php3--利用break语句从轮回中加入
<?php
for ($index = 0; $index < 10; $index++) {
// A. When $index is three, the loop ends.
if ($index == 3) {
break;
}
echo "$index<br>";
}
// B. After the break,execution starts here.
echo "After the loop: index=$index<br>";
?>
<?php font('This line is blue - size 2.'); ?>
<br>
<?php font('This line is red - size 2.', 'red'); ?>
<br>
<?php font('This line is green - size 4', 'green',4); ?>
<br>
function increment_by_three( $int_parameter ) {
$int_parameter++;
// make a nested function call to increment_by_two().
Return ( increment_by_two($int_parameter) );
}
// The gettype function returns the data type
// of its parameter.
switch (gettype($var)) {
// inter, double, and strings are simply
// displayed.
case 'integer':
case 'double':
case 'string':
echo "$var";
break;
// array datatypes need to specially
// handled.
case 'array':
// if the array has no entries, display
// a message.
if (! Count($var)) {
echo 'Empty Array.<br>';
}
else {
// the array has entries, so start an
// HTML table to display them.
echo '<table border="1" width="100%">';
echo '<tr>';
echo '<th>Key</th>';
echo '<th>Value</th>';
echo '</tr>';
// use a do loop to iterate over the
// entries in the array.
do {
echo '<tr><td align="left" valign="top">';
echo key($var);
echo '</td><td align="left" valign="top">';
echo gettype(key($var));
echo '</td><td align="left" valign="top">';
// perform the magic of recursion using the
// VALUE of the current key/value pair.
array_dump($var[key($var)]);
echo "</td></tr>";
} while (next($var));
// end the HTML table after all of the
//array entries have been displayed.
echo "</table>";
}
break;
// switch statements should always have a default
// clause - just in case the data has a value
// your program doesn't expect.
default:
echo "unknown data type";
break;
}
}
?>
用以下体例挪用函数array_dump:
<?php
// include the array_dump function
// in this script.
require('array_dump.inc');