PHP3 Chinese Document continued 3

Source: Internet
Author: User
Tags php define

Any PHP scripting language is created using continuous statements. A statement can be a value assignment statement, a function call, a loop, a condition statement, or even a statement (an empty statement) that does not do anything ). Statements often end with semicolons. In addition, you can use volume support to compress a group of statements so that the statements can be grouped into statement groups. A statement group is a statement about itself. Different statement types are described in this chapter. Constant PHP defines some constants that are provided to the structure so that it can define more types at runtime. Constants are similar to variables, but they are slightly different in syntax. The predefined constants are _ FILE _ and _ LINE __. when processing them, they are found to match the FILE name and row number. See the following Example: Example 6-1. Using _ FILE _ and _ LINE _ // use _ FILE _ and _ LINE __ You can use the define () and undefine () functions to define other constants. Example 6-2. Defining Constants // define a constant Expression expressions are the most important cornerstone of PHP. In PHP, almost all the content you write is an expression. The simplest and most precise way to define an expression is to "all content has its value ". A simple example of entering the brain right away is constants and variables. When you enter "$ a = 5", you will assign the value "5" to the variable "$ a". Obviously, this value is 5, or '5' is an expression with a value of 5 (in this example, '5' is an integer constant ). After the value is assigned, the value of $ a is 5. Therefore, if you write $ B = $ a in this way, it means the same as $ B = 5. in other words, $ a is an expression with a value of 5. if everything works normally, it will happen. an example of a slightly more complex expression is a function ). for example, consider the following function: function foo () {return 5;}. Suppose you are familiar with the function concept (if you are not familiar with it, refer to chapter 4 ), you may assume that $ c = foo () is essentially the same as $ c = 5, and you are correct. functions are worthwhile expressions based on their return values. since foo () returns 5, the value of the expression 'foo () 'is 5. In general, functions do not return only one State value. They generally calculate something. Of course, the values in PHP must not be integers, and they are often not supported by PHP in three scalar types: integers, floating-point numbers, and strings. (Scalar values cannot be further divided into smaller numbers. For example, they cannot be arrays ). Php also supports two types of merging values: array and object. Each data type can be assigned to a variable or returned through a function. So far, PHP/FI 2 users should not feel any changes. However, PHP brings expressions into a deeper layer, just as many other languages do. PHP is an expression-oriented language. Almost everything is an expression. consider the processed expression "$ a = 5 ". it is very easy to see that there are two values, the value of the constant '5', and the value of $. The value of $ a has also been updated to 5. However, the fact is that there is an additional value here, and this value is the value assignment statement itself. This value assignment statement itself calculates the value assigned, which is 5 in this example. In fact, it means "$ a = 5", no matter what he does, it is an expression with a value of 5. Therefore, some expressions like '$ B = ($ a = 5)' are actually the same as '$ a = 5; $ B = 5' (semicolons represent the end of the expression) yes. since the value assignment statement is parsed from right to left, you can also write '$ B = $ a = 5 '. Another example of a good expression is the direction of the increment before and after. PHP/FI 2 users and many other language users may be very familiar with symbols (variable) ++ and variable. these are the ascending and descending symbols. in PHP/FI 2, the '$ a ++' statement has no value (not an expression), and therefore you cannot assign a value to it or use it in any way. PHP improves the increment/decrement Capability by developing these expressions, just like in C. in PHP, there are two incremental forms: Pre-increment and post-increment. in essence, these two increments Add 1 to the variable and have the same effect on the variable. the difference between them is the incremental expression value. the increment is in the form of '+ + $ variable (variable)' and calculates the increment value (PHP increments the variable value before reading its value, so it is called the increment) the incremental form is '$ variable ++'. Calculate the original value of $ variable before the variable increments (PHP first reads the value of the variable and then increments, so it is called "post-increment ). expression In the last example, we will process the value assignment expression of the combined operator. you already know that if you want to add 1 to the value of variable $ a, you can simply write it as '$ a ++' or '++ $ '. but what if you want to add more than 1, for example, 3? You can use '$ a ++' multiple times, but this is obviously not an effective and concise method. the general method is to write '$ a = $ a + 3 '. '$ a = $ a + 3' calculates the value of variable $ a plus 3, and is assigned to variable $, the final result is that the value of variable $ a is added with 3. in PHP, other languages like C, you can do this in a shorter way. you can add 3 to the current value of the variable $ a to write '$ a + = 3 '. the correct expression means "read the value of $ a, add 3, and assign it to $ ". in addition to being more concise and simple, such statements can be executed more quickly. the value of the expression '$ a + = 3', like the value of a regular expression assignment statement. is the assigned value. note that it is not 3, but the value of the combination of $ a + 3 (this is a value assigned to $ ). any binary operator can be combined into a composite value assignment operator. for example, '$ a-= 5' ($ a = $ A-5),' $ B * = 7' ($ B multiplied by 7), and so on. if you haven't added any other language, you will think the following expression looks very strange. this is a Trielement conditional OPERATOR: $ first? $ Second: $ third if the value of the first subexpression is true (not 0), the second subexpression is calculated, and this is the value of the entire conditional expression. otherwise, the third subexpression is budgeted and the result is used as the value of the entire conditional expression. the following example can help you understand the increment and expression before and after. function double ($ I) {return $ I * 2;} $ B = $ a = 5; /* assign 5 values to $ a and $ B */$ c = $ a ++;/* And then increment the original value of $ a (5) assign a value to $ c */$ e = $ d = ++ $ B;/* increment first, and set the value of $ B after increment (6) assign a value to $ e and $ d * // *. Both $ d and $ e are equal to (6) */$ f = double ($ d ++ ); /* double the original $ d value (6) and assign it to $ f. $ f is equal to (12) */$ g = double (++ $ e);/* first increment $ e and then double, 2*7 = 14 Assign a value to $ g */$ h = $ g + = 10;/* First, add $ g to 10 and the final result is 24. assign this value to $ h, and the final result of $ h is 24. */At the beginning of this chapter, we will say "We will describe various types of statements ". an expression can be a statement. However, not every expression is a statement. in this case, a statement has the form of 'expr'; '. A semicolon is added to an expression. in '$ B = $ a = 5;', $ a = 5 is a valid expression, but it cannot constitute a statement by itself. but '$ B = $ a = 5;' is a valid statement. the last thing worth mentioning is the true and false values of an expression. in many events, mainly in conditional execution and loop, you are not interested in the exact value of the expression. You just think TRUE or FALSE) (PHP does not have a special boolean Type ). the true and false expressions in PHP are very similar to perl. any non-zero value is TRUE, and zero is FALSE! Note that the negative number is not zero, so it is true! Null String and character '0' are FALSE; all other strings are TRUE. for non-standard numeric values (arrays or objects)-if the value does not contain any element, it is considered FALSE; otherwise, it is TRUE. PHP provides a complete and powerful expression tool, which fully demonstrates that it is beyond the scope of this Manual. how can you construct a valid expression market for an expression? The example above should give you a good prompt. throughout the rest of this manual, we will use 'exp' to represent a valid PHP expression. the IF structure is one of the most important features in any language, and PHP also includes it. It allows conditional judgment and execution of corresponding program segments. In PHP, IF statements have similar features as C: if (expr) statement, after describing the expression fragment, determines whether the expression value is true. If the expression value is true, PHP executes the statement. If the expression value is false, PHP skips the statement. In the following example, if $ a is larger than $ B, "a is bigger than B" is displayed ". If ($ a> $ B) print "a is bigger than B"; Generally, you want to use more than one statement to be executed conditionally. Of course, there is no need to use the IF condition for each statement here. You can use a group of statements to implement this function. For example, if $ a is larger than $ B, the following code displays "a is bigger than B" and assigns the value of $ a to $ B. If ($ a> $ B) {print "a is bigger than B"; $ B = $ a;} The IF statement can be nested in another IF statement, you can execute different parts of the program according to different situations. In general, ELSE may want to run a statement when the condition is met, but another program is expected to run when the condition is not met. This is the role of the ELSE statement. ELSE extends the function of the IF statement. When the condition is false, the statements following ELSE will be executed. For example, the following program section will display "a is bigger than B" when $ a is greater than $ B. In other cases, "a is NOT bigger than B" is displayed ". If ($ a >$ B) {print "a is bigger than B";} else {print "a is NOT bigger than B ";} the ELSE statement is not only used to execute the statement when the value of the IF expression is false. IF it is followed by the IF statement, it becomes the ELSEIF statement, use it to further analyze false information (see below ). As its name implies, ELSEIF is a combination of IF and ELSE. Like ELSE, it extends the IF statement to FALSE. However, unlike ELSE, ELSEIF makes another judgment based on the false conditions and processes the results. For example, the following code displays "a is bigger than B" in the case of $ a> $ B. When $ a is no greater than $ B, the code will be judged again, if $ a = $ B, "a is equal to B" is displayed. If $ a <$ B, "a is smaller than B" is displayed ". If ($ a> $ B) {print "a is bigger than B";} elseif ($ a = $ B) {print "a is equal to B ";} else {print "a is smaller than B";} multiple ELSEIF statements can exist in the same IF statement. If the first ELSEIF expression (if any) is true, it is executed. In PHP3, you can also write "else if" (using two words) with the same effect as using "elseif" (a word. Their language is slightly different (if you are familiar with the C language, you will find that this is different from that in C), but ultimately their results are exactly the same. The ELSEIF statement is only false in the IF statement or the expression of the previous ELSEIF statement, and the current ELSEIF statement table

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.