1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP Developer''''s Dictionary- P15 pptx

5 269 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 364,52 KB

Nội dung

PHP Developer’s Dictionary IT-SC book 70 The continue statement enables you to stop the execution of the current control iteration and break out to the beginning for the next iteration. The optional num value can be used to tell the parser how many control structures to continue out of. For instance, if you used a continue statement inside a while statement that was inside a second while statement itself, you could use continue 2 to break out of both statements. Using continue 1 , which is the default implied value when not passed, would simply cause you to continue out of the second, or nested, while statement. if($num == 5){ // continue statement will come back here if executed // do something here if($string == "go"){ // do more here $string = "stop"; continue 2; // breaks outside of both if statements back to first if } $num++; } do while Syntax do{ code }while(condition) Description The do while loop is just like the while loop except that it evaluates the condition after the execution of the loop rather than before. The effect of this is that you are guaranteed that the loop will execute at least once, whereas the while loop does not allow for this. else Syntax if(condition){ code [ }elseif(condition){ code ] }else{ code } PHP Developer’s Dictionary IT-SC book 71 Description The else statement, which extends the if or elseif statements, provides a default set of code to be executed if previous if or elseif statements fail condition . The brackets around the elseif portion of the syntax example show that this is an optional part of a statement, whereas the if portion is required for the else statement. elseif Syntax if(condition){ code }elseif(condition){ code [ }else{ code ] } Description The elseif statement, which extends the if statement, provides a second evaluation on a second condition before a set of code is to be executed. This assumes that previous if or other elseif statements fail their respective condition . The brackets around the else portion of the syntax example show that this is an optional part of a statement, whereas the if portion is required for the elseif statement. for Syntax for([assignment];[condition];[change_assignment]{ code } Description The for loop has been labeled as the most complex looping structure not only in PHP, but in other programming languages as well. This loop takes an initial PHP Developer’s Dictionary IT-SC book 72 assignment where a variable is assigned a value. The second parameter specifies a condition that is evaluated before each iteration of the loop. If the condition evaluates to true, the code is executed and the change_assignment , such as increasing or decreasing the assignment by 1, is performed. At that time, the condition is reevaluated with the new value of the assignment and the process repeats itself. This continues until condition is false. These parameters are all optional, which might come as a surprise. Leaving them, or even just condition , blank will cause the for to loop indefinitely, but does allow you to use an internal break statement to end the loop. You can also see the for : endfor entry for a different syntactical way to use the for loop. Note If you want to get extra tricky with the for loop, you can also include comma-separated code to be executed in the change_assignement . For instance, you could use print $counter, $counter++ to print the $counter value on each iteration through the loop. For more information about what the ++ means in this example, see the "Incrementing and Decrementing" section later in this chapter. for : endfor Syntax for([assignment];[condition];[change_assignment]): code endfor Description The for : endfor loop is an alternative syntax for the for loop. Using this method, the opening and closing braces are replaced with : and endfor , respectively. See the for entry for more information on condition and code . foreach Syntax foreach(array as current_value){ code } foreach(array as current_key => current_value){ code PHP Developer’s Dictionary IT-SC book 73 } Description The foreach loop, which was added in PHP4, has two syntactical definitions. In both definitions the loop takes an array and iterates through it. In doing so, it stores the current_value so that it can be referenced and processed in code . For associative arrays, you can use the second syntactical definition to also store the corresponding current_key for processing in code. The following example should help clarify: // create an array $weekdays = array ( "Sunday" => 0, "Monday" => 1, "Tuesday" => 2, "Wednesday" => 3, "Thursday" => 4, "Friday" => 5, "Saturday" => 6 ); // print out each day with the number it is associated with foreach($weekday as $day => $number) { print "\ $weekday = $day.\ n"; } if Syntax if(condition){ code } [ elseif( condition ){ code }else{ code } ] Description The if statement enables you to execute code based on successful validation of condition . This statement is one of the most widely used statements in any programming language, and PHP is no different. Optionally, as the syntax description PHP Developer’s Dictionary IT-SC book 74 shows, it is often used in conjunction with elseif and/or else statements to provide addition levels of control over code execution. if : endif Syntax if(condition): code endif; Description The if : endif statement is an alternative syntax for the if statement. Using this method, the opening and closing braces are replaced with : and endif , respectively. See the entry for if for more information on condition and code . include() Syntax include(string filename) Description The include() function enables you to include other files specified by filename . These secondary files can be valid PHP code or any text, such as HTML. If you do have additional PHP code that needs to be executed, be sure that you use the proper beginning and ending PHP tags or the parser will not execute the code. The require() language construct has similar capabilities. The major difference is that include() is executed each time it is encountered, whereas require() pulls in the contents of the included file once. You can see the difference if you place these functions in the body of a loop statement. Tip You can place a return statement in the included file to terminate the parsing of the file and return to the file that performed the inclusion. require() Syntax . files can be valid PHP code or any text, such as HTML. If you do have additional PHP code that needs to be executed, be sure that you use the proper beginning and ending PHP tags or the parser. as current_key => current_value){ code PHP Developer’s Dictionary IT-SC book 73 } Description The foreach loop, which was added in PHP4 , has two syntactical definitions. In both. the most widely used statements in any programming language, and PHP is no different. Optionally, as the syntax description PHP Developer’s Dictionary IT-SC book 74 shows, it is often used

Ngày đăng: 07/07/2014, 05:20

TỪ KHÓA LIÊN QUAN