Fifth Chapter
syntax, variables, and output
This chapter highlights
Basic Law for understanding PHP program code
Storing information in a variable
Displaying output in a Web page
In this chapter, we will explain the basic syntax of PHP, which is the code of all the normal format usage of PHP program to follow the rules. We'll also show you how to use variables to store and retrieve information in the PHP program code, and finally, the simplest way to display text in the User's browser window.
The easing and elasticity of PHP syntax
The first thing to mention about PHP is that it has tried to be as loose and elastic as possible. Depending on the severity of the grammar rules to be followed, the differences between the programming languages are quite large. It's best to have a choice because it helps to ensure that the program code you write is really what you need. If you are writing a program that controls the nuclear reactor furnace, and you forget to specify a variable, it is much better to let the program refuse execution than to deviate from the original design intent. PHP's basic design principles, however, are on a different model, because PHP itself is a quick and easy Web page tool that focuses on making it easy for the designer to use it properly, rather than having the program do extra work to indicate the meaning of the program code. PHP requires very little program code, but it can express the meaning of the program code with maximum effort. This means that some of the syntax features in other languages, such as variable declarations and function prototypes, are not required in PHP.
Again, PHP can't read your mind, it has to be expressed by you and there are some grammatical rules to follow. If you see the "parse error" in the browser window, rather than the Web page results that you originally intended to make, this means that the program code you have written has been made to the extent that PHP does not tolerate in the rules.
HTML is not PHP
Another important thing to keep in mind is that the syntax should only be used within PHP. Because PHP is embedded in HTML, in such a file, each part will be interpreted by PHP or HTML, which is based on the PHP tag to distinguish.
PHP syntax is only valid within PHP, so we assume that in this chapter the content is referred to in PHP mode, also ice is said, most of the program code fragments are assumed to be embedded in the HTML page is, and by the appropriate tag to distinguish the mark.
PHP has a C-language grammar style
The third important point to understand is that PHP is a programming language that is much like the C language style. If the reader has a C language, then you are learning PHP quickly: If this is not possible, then consult the manual. The remainder of this chapter is for others who do not know the C language, if you are a C programming designer, then you can quickly turn over the title of this chapter, you can also refer to this book specifically for the C Language Program designer prepared by the appendix, which will save you valuable time.
PHP is insensitive to semi-blank spaces
The half-blank is the part of the program designer that is typed over the screen, including spaces (Spacehar), tab characters, and enter (end-of-line symbols), and so on. PHP is not sensitive to this type of whitespace, but this does not mean that spaces and such content are of little importance (in fact, they are still important for separating "words and phrases" in the PHP language), but how many space characters in a row do not matter, by a space character and a lot of these are the same.
For example, each PHP syntax that follows the $four and assigns to the variable is equal.
$four =2+2; Single space
$four =2+2;//Space and TAB key
$four =
2
+
2;//Multi-line
It is convenient to use the line ending symbol of the ENTER key as a blank, because this means that you do not have to make sure that the syntax must be on a single line, which makes it much easier to write programs.
PHP is sometimes case-sensitive
As already mentioned before, PHP is not overly picky, and readers may be surprised that it is sometimes case-sensitive (that is, the difference between the case of the English letter). This is especially important in all variable settings. If you embed such a program code in an HTML page:
<?php$capital=67;print ("Variable Capital is $capital <BR>"), Print ("Variable Capital is $ capital<br>"); ?>
The output is
Variable Capital is 67
Variable Capital is
This is because the variables are in a different case, so they are divided into two different variables. (Surprisingly, a program snippet like this does not produce PHP errors in the default debug settings.) See the "Unspecified variables" section later in this chapter. )
PHP, on the other hand, differs from the C language in that its function names are not case-sensitive, as are the basic syntax structures (if, then, else and while, etc.).
The statement is terminated as a semicolon.
The following is a typical statement in PHP that assigns a string to the $greeting variable:
$greeting = "Welcome to php!" ;
The remainder of this section describes how smaller components can be used to construct such statements, as well as an evaluation of how the PHP literal translation program is represented (if you are already familiar with these statements and expressions, skip them).
PHP's smallest construct is an irreducible token (token), such as a number (3.14159), a string (? tow), a variable ($tow), a constant (TRUE), and a special word that forms the syntax of PHP itself (if, else, and so on). They are separated from each other by blanks and other special characters (parentheses and curly braces).
The most complex construct in PHP is an expression, which is any combination of tags with [value]. A single number is an expression, and a single variable is an expression. Simple expressions can also be combined to form more complex expressions, usually by adding an operator between the expressions (for example, A + +), or by using an expression as input to a function call (for example, pow (2*3,3*2)). An expression number with two inputs is treated as input, so that the input function puts the input in parentheses after the function name, and each input (called a parameter) is separated by commas.
Expression evaluation Operations
Whenever the PHP interpreter encounters an expression in the program code, the expression is evaluated all at once. This means that PHP starts from the value of the smallest element of the expression, and then continues to combine the values that are concatenated by the operator or function until the entire value of the expression is generated. For example, the steps in the judgment process should be imagined as follows:
$result =2*2+3*3+5;
(=4+3*3+5)//Imaginary estimation process
(=4+9+5)
(=13+5)
(=18)
The result is a number 18 stored in the $result variable.
Order of precedence, adhesion, and evaluation evaluations
There are two possible types of problems in evaluating evaluation of PHP Expressions: What is the order of evaluation evaluations if the pairs of expressions are combined or combined? For example, in the evaluation process that has just been shown, multiplication is more binding than addition, which has an effect on the end result.
The specific way of combining expressions is called precedence rules, and high-priority operators first get the other surrounding expressions to perform the operations. If necessary, remember that these rules, such as [*] are always higher precedence than [+] (these rules are explained in detail in subsequent chapters). Or use this first rule to judge: when unsure, use parentheses to combine expressions.
For example:
$RESULTL =2+3*4+5//results are 19
$result 2 = (2+3) * (4+5)//result is 45
The arithmetic sub-precedence rule takes away the ambiguity about how the expression is combined, but what happens when the operator has the same precedence? For an example, consider the following machine expression:
$how _much=3.0/4.0/5.0;
Whether the expression equals 0.15 or equal to 3.75 will depend on which division operator first processes the number 4.0. The online description has an exhaustive list of binding rules, but the most common rule to remember is that the binding is usually left to right, that is, the result of the above expression evaluation is 0.15, because the one on the left in the two division operator has precedence.
The final question evaluates the order of evaluation, and it is not the same as the binding. For example, the following arithmetic expression:
3*4+5*6
We know that multiplication should happen before addition, but this does not mean knowing what multiplication the PHP will take first. In general, users need not care about the order of evaluation, because in most cases this has no effect on the results. We can build some odd examples of how the results are related to the order of evaluation, usually because the sub-expressions are specified in the rest of the expression. For example
$hun = ($this = $that +5) + ($that + $this +3);//Bad Example
But please don't write like that, okay. PHP may have ... Depending on it, we will not tell you its result (the only reasonable application that relies on the order of left-to-right evaluation is in the "short path" mode of the Boolean expression, which we will explain in chapter seventh).
Expressions and Types
Usually the programmer will be careful to match the type of expressions and the operands or functions that combine them. Common expressions are mathematical expressions (with mathematical operands combined with numbers), Boolean expressions (statements that combine true or false with and or or), or string literals (using operators and functions to construct strings). As with the rest of PHP, the processing of types is very loose. For example, the following is an example of an expression, and it is obvious that it has been improperly mixed with two operations:
2 + 2 *? nonsense? + TRUE
This expression does not produce an error, but instead evaluates to a numeric "3" (you can temporarily use this example as a guessing puzzle, and the next chapter will explain why such a result occurs).
Specifying an expression
The most common type of expression is (Assignment), in which a variable is set equal to the evaluation evaluation result of an expression. The expression is in the form of a variable name (beginning with "$") followed by an equal sign and then an expression to evaluate the evaluation. For example
$eight = 2 * (2 * 2)
$eight "will be specified in the way that we want it to be.
One important thing to keep in mind is that the specified expression is also an expression, so they are inherently "value"! The objects specified by the operator are the same regardless of the variables or values. This means that an expression can be used in the middle of a more complex expression. If the following expression is evaluated:
$ten = ($two = 2) + ($eight = (2 * 2))
Each variable is specified as a numeric value equal to its name.
In summary, the narrative statement (statement) in PHP can be any expression with a semicolon (;) at the end. If the expression is regarded as a phrase, then the narrative statement is considered "the whole sentence", the semicolon is even the end of the sentence period. Any valid PHP statement within PHP tags is a valid program code for PHP.
Reasons to use expressions and statements
There are usually only two reasons to write an expression in PHP: To get its value, or to get a secondary effect (side effect). The value of the expression is passed to the more complex expression that contains it, and the secondary action is anything that occurs except the evaluation result. The most typical secondary effects include specifying or changing variables, where the consumer screen is displaying something, or some other persistent change to the program environment (such as interacting with the database).
Narrative statements are expressions only, but they cannot be contained in more complex expressions, which means that the only reason to use narrative statements is its "sub-function"! This also means that the editor can write some legitimate but completely useless statements, such as the following second statement:
Print ("Hello");//Sub-add effect to display the output to the screen
2 * 3 + 4;//useless, no effect generated
$value _num= 3 * 4 + 5;//secondary plus effect for designation
Store_in_database (49.5);//secondary add effect to database
Use of curly braces
Although the narrative cannot be merged as an expression, it can be placed in curly braces by placing a string of statements where the statement can be used.
For example, the IF structure in PHP has a test condition (in parentheses), followed by a statement that should be executed if the result is judged to be true. If you want to execute multiple statements when judging as true, you can use this heap of narrative statements enclosed in curly braces. The following two if program code snippets are equal (both to judge a fidelity narrative and to print the same message):
if (3==2+1) print ("Good–i haven′t totally Lost My mind.<br>"), if (3 ==2+1) {print ("Good–i haven′t totally"), print (" Lost my Mind.<br> ");}
In the middle of the brace, any statement can be placed, including the if narrative with braces in its own block. This means that if statements can contain another if narrative, nested nesting can be nested in any layer as needed.
Comments
Annotations are part of the program, and are used by us to explain the procedure, to help explain the code of the interpreter. The first thing the program executor does with the program code is to reject the comment, so the comment has no effect on the program's functionality. But it is also invaluable when it comes to helping others to read the code of the program, as well as the idea of the original designer writing the program, even if the programmer has written it for a while and then looks back at his own program.
PHP is inspired by several different programming languages, including C, Perl, and Unix shell script. PHP therefore supports all of these language annotation styles, and these styles can be used freely in PHP program code.
Multi-line comments in C language style
Multiline annotation styles are the same as in C: Comments start with a character pair of "/*" and end with "*/" as characters. For example:
* This IsA comment inphp */
The most important thing to remember about multiline annotations is that annotations cannot be written in nested nesting ways. You cannot put another comment inside a comment. If you attempt to do this, the comment will end after the first "*/" character pair, and the rest of the comment will be interpreted as a program code, which may result in an incorrect result. For example:
/* This comment would/* fail horribly on thelast word of this */sentence*/
This is an inadvertent error that is often encountered when trying to "annotate" a program that already has a commented language, so please be careful.
Single-line comments: "#" and "//"
In addition to/*...*/'s multiline annotations, PHP supports two different annotation methods for single-line use, one inherited from C and Java, and the other after Perl and Shell script inheritance. The Shell script style comments begin with the "#" symbol, while the C + + style comments begin with a double slash "//". Both of these methods treat the remainder of the raised line as comments, as shown in the following example:
# This was a comment, and# this is the second line of the comment//this is a comment too. Each style comments Only//one The last word of this sentence would fail horribly.
Very clever readers think that the single-line comment is not compatible with the whitespace that we described earlier. This is true if you use a single line of comments and replace one of the spaces with the Enter end symbol, the results are all different. To be more precise, the code is not sensitive to whitespace when PHP removes comments from the program code.
Variables
The primary way to store information in the middle of a PHP program is by using a "variable", which is the way to take a name and then save any values to be used after the post.
For variables in PHP, here are a few things to be aware of (more on this later):
All variables in PHP are preceded by the "$" notation.
The value in the variable is the value it was recently specified.
The variable is specified with the "=" operator, and the variable is on the left, and the expression to evaluate evaluates to the right.
Variables are not required to be declared before they are specified.
In addition to the type of the current value, the variable has no intrinsic type.
The variable that was used before the specified has its default value.
The above is the PHP learning treasure-The fifth chapter of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!