PHP3 Chinese document continued 4

Source: Internet
Author: User
To understand the SWITCH statement, it is very important to know how it is executed to avoid lengthy statements. the SWITCH statement is executed by a row (in fact, it is a statement ). at the beginning, no code was executed. PHP continues SyntaxHighlighter only when a CASE statement with the same value as the SWITCH expression is found.

To understand the SWITCH statement, it is very important to know how it is executed to avoid lengthy statements. the SWITCH statement is executed by a row (in fact, it is a statement ). at the beginning, no code was executed. only when a CASE statement with the same value as the SWITCH expression statement is found, PHP continues to execute the statement until the end of the SWITCH body, or the BREAK statement appears. if you do not write a BREAK statement after a branch statement, PHP will continue to execute the following Branch statement. for example:/* example 3 */switch ($ I) {case 0: print "I equals 0"; case 1: print "I equals 1"; case 2: print "I equals 2";} here, if $ I is equal to 0, PHP will execute all print statements. if $ I is equal to 1, PHP will execute the remaining two print statements. only when $ I is equal to 2 can you get the expected execution result. only 'I equal S 2' is displayed. therefore, do not forget the BREAK statement after each branch statement (even if you want to avoid providing it in a certain environment ). A special branch is the default branch. this branch can match any other branch that does not match. for example:/* example 4 */switch ($ I) {case 0: print "I equals 0"; break; case 1: print "I equals 1"; break; case 2: print "I equals 2"; break; default: print "I is not equal to 0, 1 or 2 ";} another fact worth mentioning is that the CASE expression can be any expression that calculates the scalar type, that is, integer, real, and linear. arrays and objects won't cause PHP to crash, but they don't make any sense. the REQUIRE statement replaces itself with the specified file. Lude statements are very similar. This means that you cannot place a require () statement in a loop body, and expect it to contain the content of different files in each repetition. to achieve this, you can use the INCLUDE statement. Require (header. inc); INCLUDE Statements INCLUDE and calculate specified files. this operation is performed every time an INCLUDE statement is encountered. therefore, you can use the INCLUDE statement in a loop body. to contain different files. $ files = array (first. inc, second. inc, third. inc); for ($ I = 0; $ I <count ($ files); $ I ++) {include ($ files [$ I]);} INCLUDE () unlike the REQUIRE () statement, the include statement re-calculates every time (and only when it is executed), and require () statement when it is first encountered, it is replaced by a specified file, regardless of whether the content of the file is calculated (for example, if it appears in an if statement where the condition is FALSE ). because include () is a special language structure, you must close it in a syntax block if it is in a condition block. /* Errors. do not work as expected. */if ($ condition) include ($ file); else include ($ other);/* The following is correct. */if ($ condition) {include ($ file);} else {include ($ other);} when a file is computed, syntax analysis will be in "HTML-mode". this mode will output the content of this file until the first PHP start tag ( 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 ;}}}?> As shown above, a class named Cart is defined. This class consists of multiple arrays describing items and functions for adding and deleting projects. Class is a type, that is, it is the design blueprint of the actual variable. You can create a variable group and perform some new operations on them according to the design. Example: $ cart = new Cart; $ cart-> add_item ("10", 1); as shown above, an object $ Cart of the class cart type is created. The add_item () function in this object is called to add a project with the item number 10. Class can be extended using other classes. This extended or inherited class has all the variables and functions of the basic class, and you can add your own extension definitions in it. To define this definition, you need to use the keywords of the extended definition. Class Named_Cart extends Cart {var $ owner; function set_owner ($ name) {$ this-> owner = $ name ;}} defines a class (class) named Named_Cart ), this class has all the variables and functions contained in the class Cart, and also adds a variable $ owner and the function set_owner (). You can create a cart with a name and get the name of the cart owner. You can also use a normal cart-like function in the Named_Cart class. $ 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) in the function of this class, the variable $ this indicates this object. In the current object, you can use $ this-> something to access any variables and functions. When you create a new class, a constructor function is automatically called. If a function has the same name as the class name, it becomes the constructor: class Auto_Cart extends Cart {function Auto_Cart () {$ this-> add_item ("10 ", 1) ;}} in the preceding example, a class named Auto_Cart is defined. It adds a constructor function to the original class Cart, this constructor initializes the Cart class by adding a project with the item number 10 each time a class is created. The constructor can also display information that can be randomly selected, which makes them very useful. 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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.