Home > Backend Development > PHP Tutorial > PHP3 Chinese Documentation Continued 3_PHP Tutorial

PHP3 Chinese Documentation Continued 3_PHP Tutorial

WBOY
Release: 2016-07-13 17:23:30
Original
707 people have browsed it

Any PHP scripting language is created using consecutive statements. A statement can be an assignment statement, a function call, a loop, a conditional statement or even a statement that does nothing (an empty statement). Statements often end with a semicolon (;). In addition, using volume support, a group of statements can be compressed so that statements can be grouped into statement groups. A statement group is a statement about itself. The different statement types are described in this chapter. Constants PHP defines some constants to provide structures to enable it to define more types at runtime. Constants and variables are very similar, but their syntax is slightly different. The predefined constants are __FILE__ and __LINE__. When processing them, you will find that they match the file name and line number. Please refer to the following examples: Example 6-1. Using __FILE__ and __LINE__ //Using __FILE__ and __LINE__ You can use the functions define() and undefine() to define other constants. Example 6-2. Defining Constants //Defining constants Expressions Expressions are the most important cornerstone of PHP. In PHP, almost everything you write is an expression. The simplest and most precise way to define an expression is "everything has a value".A simple example that immediately comes to mind is: constants and variables. When you enter "$a = 5", you assign the value "5" to the variable "$a". Obviously, the value 5 is obtained, or '5' is an expression with a value of 5 (in this In the example, '5' is an integer constant). After assignment, the value of $a is 5, so if you write $b = $a, it has the same meaning as $b = 5. In other words, $a is an expression with a value of 5. If everything is working fine, this is what will happen. An example of a slightly more complex expression is a function. For example, consider the following function: function foo () { return 5; } Assume you are familiar with functions concept (if you're not familiar with it, take a look at Chapter 4), you might assume that $c=foo() is essentially the same as $c=5, and you would be correct. Functions are valued by their return values Expression. Since foo() returns 5, the value of expression 'foo()' is 5. Generally speaking, functions don't just return a state value, they usually compute something. Of course, values ​​in PHP don't have to be integers, and often they aren't. PHP supports three scalar types of values: integers, floats, and strings. (A scalar value is a numeric value that cannot be subdivided into smaller parts, e.g. it cannot be an array). PHP also supports two synthetic types of values: arrays and objects. Each data type can be assigned to a variable or returned by a function. So far, users of PHP/FI 2 should not notice any changes. However, PHP takes expressions to a deeper level, as many other languages ​​do. PHP is an expression-oriented language, and almost everything is an expression. Consider the expression "$a=5" we have already dealt with. It can be easily seen that there are two values ​​contained here, The value of the constant '5', and the value of $a. The value of $a has also been updated to 5. But the fact is that there is an extra value involved, and that value is the assignment statement itself. The assignment statement itself calculates the assigned value, which is 5 in this example. In effect, it means that "$a=5", no matter what it does, is an expression that evaluates to 5. Therefore, some expressions like '$b=($a=5)' are actually the same as '$a=5;$b=5' (the semicolon represents the end of the expression). Since the assignment statement is To parse from right to left, you can also write '$b=$a=5'.Another good example of an expression is the directionality of pre and post increments. Users of PHP/FI 2 and many other languages ​​may be very familiar with the notations (variable++ and variable--). 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 a value to it or use it in any way. PHP is enhanced by developing these expressions It has the ability to increment/decrement, just like in C language. In PHP, there are two forms of increment-pre-increment and post-increment. Both increments essentially increase the variable by 1, and add 1 to the variable. The effect of variables is the same. Their difference is the value of the increment expression itself. The pre-increment form is '++$variable(variable)', which calculates the increment value (PHP increments the value of the variable before reading its value , so it is called pre-increment) The form of post-increment is '$variable++', which calculates the original value of $variable before the variable is incremented (PHP first reads the value of the variable and then increments it, so it is called post-increment). The last one of the expression Example We will deal with combining operator assignment expressions. You already know that if you want to increment the value of variable $a by 1, you can simply write '$a++' or '++$a'. But if you want to increment Is the value not just 1, for example 3? You can use '$a++' multiple times, but obviously this is not an efficient and concise way. The usual way is to write '$a=$a+3'. '$ a=$a+3' calculates the value of variable $a plus 3, and is reassigned to variable $a. The final result is that the value of variable $a is increased by 3. In PHP, like other programs like C In some languages, you can do this in a shorter way. To add 3 to the current value of a variable $a you can write '$a+=3'. The correct meaning of this expression is "Read the value of $a, add 3, Then assign it to $a". In addition to being more concise and concise, such a statement executes faster. The value of the expression '$a+=3', like the value of a regular assignment statement, is the assigned value. Note that it is not 3, but the value of the combination of $a+3 (which is the value assigned to $a). Any binary operator can be used to combine into a compound assignment operator. For example, '$a -=5'($a=$a-5),'$b*=7'($b times 7), etc. If you haven't added it in other languages, you will think of the following expression It looks very strange. This is the ternary conditional operator: $first ? $second : $third If the value of the first subexpression is true (non-zero), then its second subexpression will be evaluated, And this is the value of the entire conditional expression. Otherwise, the third subexpression will be evaluated, and the result is used as the value of the entire conditional expression. The following example can help you generally understand the pre-, post-increment and expressions. function double( $i) { return $i*2;} $b = $a = 5; /* assign 5 to $a and $b */ $c = $a++; /* then increment, and add the original value of $a (5 ) is assigned to $c */ $e = $d = ++$b; /* before incrementing, assign the value (6) of $b after the increment operation 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 is equal to (12)*/ $g = double(++$e); /* First perform an increment operation on $e, and then double it. 2*7 = 14 is assigned to $g*/ $h = $g += 10; /* First, $g is increased by 10, And the final result is 24. Assign this value to $h, and the final result of $h is also 24.*/ At the beginning of this chapter, we said "we will describe various types of statements". The expression can become Statements, however, not every expression is a statement. In this case, a statement has the form 'expr'';', an expression plus a semicolon. In '$b=$a=5;' , $a=5 is a valid expression, but it cannot form a statement by itself. But '$b=$a=5;' is a valid statement. The last thing worth mentioning is that an expression The value of an expression is true or false. In many cases, mainly in conditional execution and loops, you are not interested in the exact value of the expression, you are only interested in whether it means TRUE (true) or FALSE (false) (PHP There is no special boolean type). The way to calculate the truth or falseness of an expression in PHP is very similar to that in Perl. Any non-zero value is TRUE, and zero is FALSE! Note that negative numbers are also non-zero, so they are also true! The empty string and character '0' is FALSE; all other strings are TRUE. For non-scalar numeric 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 describing it is beyond the scope of this manual. The above example should give you a good hint as to what an expression is and how you can construct an effective one. Throughout this manual For the rest, we will use 'expr' to represent a valid PHP expression. IF The IF construct is one of the most important features of any language, and PHP includes it. It allows conditional judgments to be made while executing corresponding program segments. The characteristics of the IF statement in PHP are very similar to C: if (expr) statement, after describing the expression fragment, will determine whether the value of the expression is true. If the expression evaluates to true, PHP will execute the statement. If the value is false, PHP will skip the statement. In the following example, if $a is bigger than $b, then "a is bigger than b" will be displayed.if ($a > $b) print "a is bigger than b"; Usually, the user wants to use more than one statement to be executed conditionally. Of course, there is no need to use IF conditional judgment for every statement here. Users can use a set of statements to achieve this function. For example, if $a is bigger than $b, then the following code will display "a is bigger than b" and assign the value of $a to $b. if ($a > $b) { print "a is bigger than b"; $b = $a; } The IF statement can be nested within another IF statement. Using this, the user can execute the program according to different situations. different parts of it. ELSE Generally, you may want to run a certain statement when a condition is met, and you want to run another program when the condition is not met. This is what the ELSE statement does. ELSE extends the function of the IF statement. When the condition is false, the statement following ELSE will be executed. For example, the following program segment will display "a is bigger than b" when $a is greater than $b, and "a is NOT bigger than b" otherwise. if ($a > $b) { print "a is bigger than b"; } else { print "a is NOT bigger than b"; } The ELSE statement does not only execute the statement when the IF expression value is false. If followed by IF, it becomes an ELSEIF statement, which can be used to further analyze false situations (see below). ELSEIF As its name suggests, ELSEIF is a combination of IF and ELSE. Like ELSE, it extends the IF statement's handling of FALSE. But unlike ELSE, ELSEIF will judge the condition that is already false again and process it according to the result of the judgment. For example, the following code will display "a is bigger than b" when $a>$b. When $a is not greater than $b, it will make another judgment. If $a==$b, it will display "a is equal to 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"; } There can be multiple ELSEIF statements in the same IF statement. The first ELSEIF expression (if any) will be executed if true. In PHP3, you can also write "else if" (using two words) and the effect is the same as using "elseif" (one word). Their languages ​​are only slightly different (if you are familiar with the C language, you will find that the difference is similar to that in C) but in the end their results are exactly the same. The ELSEIF statement is false only if the IF statement or the expression of the previous ELSEIF is false, and the current ELSEIF statement table

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532206.htmlTechArticleAny PHP scripting language is created using consecutive statements. A statement can be an assignment statement, a function call, a loop, a conditional statement or even one that does nothing...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template