PHP basic syntax (2)

Source: Internet
Author: User
Last time I spoke a little bit about the most basic things. now let's take a look at the pig's blog to summarize the following:

Last time I spoke a little bit about the most basic things. now let's take a look at the pig's blog to summarize the following:

1. expression

The most basic expressions are constants and variables. For example, if you type "$ a = 5", assign the value "5" to the variable $.

An example of a slightly complex expression is a function. Example: $ a = sum ();

Using arithmetic operators such as $ a ++, $ a-, ++ $ a, and-$.

Comparison expressions, such as $ a> 5, $ a <10.

A value assignment is also an expression, for example, $ a + = 5 and $ a * = 10.

A ternary operator is also an expression, for example, $ result = $ a> $ B? $ C: $ d.

2. process control. This must be well mastered. this is the core of any programming language. There are three types of process control for structured programming languages:

Sequential structure

Branch structure (condition structure or selection structure)

There are two branch structures: if branch structure and switch branch structure:

If branch structure: in the if statement, select a branch based on the value of the conditional expression true or false. Based on the number of conditions in the if statement, we can divide it into single condition statements, two-way condition statements, and multi-direction condition statements,

Switch branch structure: the conditional expression value of the switch statement can have multiple values. we must provide a case statement to process each value. In addition, a default processing method default should be provided. Note the break application in the switch statement.

Here I will explain the difference between break and continue:

The break statement jumps out of the switch statement. You can also use the break statement in a loop statement to skip the loop and stop executing the loop. If multiple loops are nested, you can add an integer n after the break to terminate the n-layer loop calculated outward by the current loop body.

Continue cannot be used in a separate switch statement. In the Loop statement, the continue jumps out of this loop, does not execute the statement after the continue of this loop, and then carries out the next loop.

Loop structure

I think this is important. in PHP, the loop statements include while, do... While, for, and foreach, among which foreach is the most common (later ).

3. Custom functions. It can greatly improve the code reuse rate. UDFs encapsulate these code blocks and give them a name. you only need to call them by function name. Note the following:

The function name cannot be the same as the existing function name.

The function name can only contain letters, numbers, and underscores, and can only start with a letter or underscore.

The return value of a function has two functions: one is to terminate the execution of the code after the return statement; the other is to return the value in the return statement as the final result of the function. Check the code:
Function GetIP (){
If (getenv ('http _ CLIENT_IP ')){
$ Onlineip = getenv ('http _ CLIENT_IP ');
}
Elseif (getenv ('http _ X_FORWARDED_FOR ')){
$ Onlineip = getenv ('http _ X_FORWARDED_FOR ');
} Elseif (getenv ('remote _ ADDR ')){
$ Onlineip = getenv ('remote _ ADDR ');
} Else {
$ Onlineip = "unknown ";
}
Return $ onlineip;
}
This code is a user-defined function. of course, the if branch structure of process control is also used. Let's talk about this... Report completed

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.