PHP3 Chinese document Continuation 3_php Tutorial

Source: Internet
Author: User
Tags php define scalar
Any PHP scripting language is created using successive statements. A statement can be an assignment statement, a function call, a loop, a conditional statement, or even a statement that doesn't do anything (an empty statement). Statements often end with semicolons (;). In addition, the use of volume support can compress a set of statements so that statements can be composed of statement groups. A statement group is a declaration about itself.   The different statement types will be described in this chapter. Constant PHP defines a number of constant feed structures that enable it to define more types at run time. Constants and variables are very similar, but they are slightly different in syntax. The predefined constants are __file__ and __line__, and when they are processed, they are found to match the file name and line number. Please refer to the following example: Example 6-1. Using __file__ and __line__//use __file__ and __line__ You can use the functions define () and undefine () to define other constants. Example 6-2. Defining Constants//Defining constants Expression expressions are the most important cornerstone of PHP. In PHP, almost all of the content you write is an expression.   The simplest and most precise way to define an expression is "anything has its value." A simple example of immediately entering the brain is constants and variables. When you enter "$a = 5", you assign the value "5" to the variable "$a", obviously, get the value 5, or ' 5 ' is an expression with a value of 5 (in this case, ' 5 ' is an integer constant). The value of $ A after assignment is 5, so if you write $b = $a, you have the same meaning as $b = 5. In other words, the $a is an expression with a value of 5. If everything works, these will be what happens.   An example of an expression that is slightly more complex is the function. For example, consider the following functions: function foo () {return 5;} Assuming you are familiar with the concept of a function (if you are unfamiliar with it, look at the fourth chapter), you may assume that $c=foo () is essentially consistent with $c=5 and you are right. The function is to have their return value as a worthwhile expression. Since foo () returns 5, the value of expression ' foo () ' is 5. In general, functions do not simply return the value of a state, they generally calculate something. Of course, the values inside PHP do not have to be integers, and often they are not PHP supports three scalar types of values: integers, floating-point numbers, and strings. (A scalar value is a value that cannot be subdivided into smaller numbers, for example, it cannot be an array). PHP also supports two types of synthetic values: arrays and objects. Each data type can be assigned to a variable or returned through a function. So far, Php/fi 2 of users should not feel any change. However, PHP brings the expression to a deeper level, as is done in many other languages. PHP is an expression-oriented language, and almost everything is an expression. Consider the expression "$a = 5" that we have already processed. It is very easy to see that it contains two values, a constant ' 5 ' value, and a value of $ A. The value of $a has also been updated to 5. But the fact is that there is an extra value in it, and this value is the assignment statement itself. The assignment statement itself computes the value of the assignment, which in this case is 5. In fact, it means "$a = 5", whatever he does, is an expression with a value of 5. Therefore, some expressions like ' $b = ($a =5) ' are actually the same as the ' $a =5; $b = 5 ' (the semicolon represents the end of the expression) is the same. Since the assignment statement is parsed in a right-to-left order, you can also write ' $b = $a = 5 '. Another example of a good expression is the directionality of the pre-and post-increment quantities. Php/fi 2 Users and users of many other languages may have symbols (VarIable (variable) + + and variable--) are very familiar. These are the increment and decrement symbols. In Php/fi 2, the statement ' $a + + ' has no value (not an expression), and therefore you cannot assign it or use it in any way. PHP enhances the ability to increment (increment)/decrement (decrement) by developing these expressions, as in C. In PHP, there are two incremental forms-pre-increment and post-increment. These two increments are essentially adding 1 to the variable, And the effect on the variables is the same. Their difference is the increment of the value of the expression itself. The first increment is the ' + + $variable (variable) ', the increment value (PHP increments the value of the variable before the value is read out, so it is incremented) after the increment in the form of ' $variable + + ', Computes the original value of the $variable before the variable is incremented (PHP reads the value of the variable first, then increments it, so it is called after incrementing). In the last example of an expression, we will work with the combination operator assignment expression. You already know. If you want to add 1 to the value of the variable $ A, you can simply write ' $a + + ' or ' + + $a '. But what if you want to add more than 1, say 3? You can use ' $a + + ' multiple times, But obviously this is not an effective, concise way. The usual approach is to write ' $a = $a +3 '. ' $a = $a +3 ' evaluates the variable $ A plus 3 and is re-assigned to the variable $ A, and the end result is a value of $ A of $3. In PHP, as in some other languages like C, you can do this in a shorter way. Add 3 to the current value of the variable $ A and write ' $a +=3 '. The correct meaning of this expression is "read $ A value, add 3, and assign it to $ a". In addition to being more concise and abbreviated, such statements execute faster. The value of the expression ' $a +=3 ', like the value of a regular assignment statement. Is the value assigned to it. Note that it is not 3, but the value of the $a+3 combination ( This is a value assigned to $ A). Any binary operator can be used to combine a compound assignment operator. For example, ' $a-=5 ' ($a = $a-5), ' $b *=7 ' ($b multiplied by 7), and so on. If you do not add in other languages, you will think that the following expression looks very strange. This is the ternary condition operator: $first? $second: $third If the value of the first subexpression is true (not 0), then his second subexpression is evaluated, and that is the value of the entire conditional expression. Otherwise, the third subexpression is budgeted, and the result is the value of the entire conditional expression. The following examples can help you to understand the general understanding of before, after increment and expression. function double ($i) {return $i * *;} $b = $A = 5; /* Assign 5 to $ A and $b */$c = $a + +; /* increments, assigns a value of $ A (5) to $c */$e = $d = + + $b; /* Before incrementing, assign the value of the increment operation $b (6) to $e and $d */* At this time, both $d and $e are equal to (6)/$f = double ($d + +); /* Double the original value of $d (6) and assign it to $f. $f equals (a)/$g = double (+ + $e); /* First $e is incremented, then doubled, 2*7 = 14 is assigned to $g*/$h = $g + = 10; /* First, $g plus 10, and the end result is 24. Assign this value to $h, and $h The final result is 24.*/at the beginning of this chapter, we said, "we will describe various types of statements." An expression can be a statement, however, not every expression is a statement. In that case, A statement with ' expr '; ' In the form of an expression plus a semicolon. In ' $b = $a = 5; ' , $a =5 is a valid expression, but he cannot make a statement by himself. but ' $b = $a = 5; ' is a valid statement. The last thing worth mentioning is that the value of an expression is true or false. In many cases, you are not interested in the exact value of an expression, primarily in conditional execution and looping, and you simply do not think it is true (true) or False (PHP does not have a specific Boolean type). The way in which expressions are calculated and true in PHP is very similar to Perl. Any nonzero numeric value is true, and 0 is false! note that negative numbers are also nonzero, so it's true! The empty string and the character ' 0 ' are false; for non-scalar values (arrays or objects)- If the value does not contain any elements, it is considered false, otherwise true. PHP provides a complete and powerful expression tool, and fully illustrates that he is beyond the scope of this manual. about what the expression is, how you can construct a valid expression city, the above example should give you a good hint. Throughout the remainder of this manual, we will use ' expr '   Represents a valid PHP expression. The IF structure is one of the most important features in any language, and PHP includes it. It allows conditional judgments to be performed at the same time to execute corresponding program segments. The attributes of the IF statement in PHP are very similar to C: if (expr) statement, after describing the expression fragment, determines whether the value of the expression is true.   If the value of the expression is true, PHP executes the statement, and if the value is false, then PHP skips the statement. In the next example, if$a larger than $b, the "A is bigger than B" will be displayed.   if ($a > $b) print "A is bigger than B"; The usual scenario is that the user wants to use more than one statement to be conditionally executed. Of course, there is no need for each statement to be judged using the IF condition. Users can use a set of statements to implement this functionality. For example, if $ A is larger than $b, the following code displays "A is bigger than B" and assigns a value of $ A to $b.   if ($a > $b) {print "A is bigger than B"; $b = $a;}   If statements can be nested within another if statement, this allows users to execute different parts of the program depending on the situation. ELSE Typically, you might want to run a statement when the condition is met, and you want to run another program if the condition does not match. This is the role of the Else statement. else extends the action of the IF statement, and when the condition is false, the statement following the else will be executed. For example, the next program segment will display "A is bigger than B" when $ A is greater than $b and, in other cases, "A is not bigger than B".   if ($a > $b) {print "A is bigger than B";} else {print "A was not bigger than B";}   The Else statement does not only serve the purpose of executing the statement if the value of the IF expression is false, but if it is followed by an if, it becomes the ElseIf statement, which can be used to further analyze the false case (see below). ELSEIF as its name implies, ELSEIF is a combination of if and else. As with else, it expands the handling of false (false) if statements. But unlike else, ElseIf will have a second judgment on the false conditions and deal with the results of the judgment. For example, the following code will display "A is bigger than B" in the case of $a> $b, and if $ A is not more than $b, it will be judged if $a== $b, if equal $b will display "A is $a< than B ". 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";} You can have more than one ElseIf statement in the same if statement. The first ElseIf expression (if any) is executed if it is true. In PHP3, you can also write "else if" (using two words) while the effect is the same as using "ElseIf" (a word).   Their language is only slightly different (if you're familiar with C, you'll find it's different and similar in C) but eventually their results are exactly the same. The ElseIf statement is false only in the IF statement or the previous ElseIf expression, while the current ElseIf statement table

http://www.bkjia.com/PHPjc/532206.html www.bkjia.com true http://www.bkjia.com/PHPjc/532206.html techarticle any PHP scripting language is created using successive statements. A statement can be an assignment statement, a function call, a loop, a conditional statement, or even one that doesn't do anything ...

  • Related Article

    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.