Operators in PHP and Knowledge Base for PHP

Source: Internet
Author: User
Tags arithmetic operators bitwise operators getting started with php php basics knowledge base

Objective

In the previous chapter, we initially learned about PHP's web base and the basics of PHP, and continue to share more knowledge about PHP today.

Theoretical knowledge may seem more boring, but our practice (knocking at code) is inseparable from it.

Only by combining theory with practice can we achieve more functions with minimal code.

* * This chapter key words : operators; variable variables; access symbols; branches and loops; Process Control Statement Goto.

Let's take a look at the operators in PHP and more about PHP Knowledge Base :

a operator

1, arithmetic operators:+-*/% + +--


2, assignment operator:= + = = *=/=%=. = (Connection string)


3. Comparison operators:> < >= <= = = = = = = = <>,! = ==


= =: Requires equal value, without data type equality


= = = requires value and data type, must be equal


! =: Compare values only, value equal to False


!==: Compare values and types, both values and types are equal to False


4. Logical operator:and/&& or/| | not/! Xor

&& | | A short circuit occurs when the operation is performed:
The right side is no longer executed when the left side is able to determine the result!! (The result is false when the left side of the && is false; | | The result is true when the left side is true)

XOR: Logical XOR. There is only one pair on both sides, the result is true, both sides are true or the same is false, and the result is false.


5, Bitwise operators:

Convert the 10 binary to 2 and then the operation.

&: Bitwise AND, turn into binary, two are 1, the result is 1
|: Bitwise OR, after switching to binary, there is a 1, which is 1
^: Bitwise XOR, after turning into binary, the two are different 1, two are 1, or both are 0, the result is 0.
~: Bitwise reversed, converted to binary number, all digits reversed. 1--->0 0--->1
<<: Left shift: After turning into binary, left a few, the right vacant number 0.
num << n equals num*2^n (※※※)
>>: Right shift: After turning into binary, right shift several, left spare position number 0.
num >> n equals num/2^n (※※※)

6. Other operators:
expression 1-expression 2: expression 3:
expression 1 is an execution expression 2, otherwise an expression 3 is executed.
": Call the command line in the System DOS environment and execute it. However, because of security and cross-platform, we do not support the use of; eg: ' ipconfig '
@: Error Message control: Can temporarily mask to some small error message. But it is not advocated!

two variable Variable

In front of a variable name, add a $ symbol. You can use the value of the first variable as the name of the new variable.

$hello = "Hello1";
$ $hello = "World"; //$hello 1
$$ $hello = "jredu"; //$world

three Access Symbol


&: variable name plus & can take out the address of the variable in memory and assign it to another variable.
$num 2 = & $num 1; //Remove the NUM1 address to num2, which is equivalent to the reference data type we are referring to. The values of num2 and NUM1 will change at the same time.

Four branching and looping

1. If the judging condition
For more details, please click on " Curiosity!" The second part of the previous chapter, "Basics of Getting Started with PHP," here, bloggers don't introduce ...

2, ElseIf
In PHP, ElseIf statements can be ligatures or separated by spaces;

For example:
else if () √
ElseIf () √

3. Switch
In PHP, the switch structure is judged using = = rather than = = =
In PHP, continue can be used with the switch structure, with the same effect as break.
In PHP, continue and break can be followed by numbers, which means skipping several layers of loops or switch;
Eg:break 3; Indicates terminating 3-layer loop

4. Do-while Cycle
Do-while the end of the loop must be added with a semicolon.
do{

}while ();

5. Process Control Statements

1. Break: Terminates the loop of this layer; A number can be followed by a break, which means that several layers of loops are terminated. Break 3 to terminate the 3-layer loop.
2, continue: skip this cycle; Continue can also be followed by numbers, which means skipping several layers of loops
3, return: Terminates the current function, and returns the value (if any), but generally only used in the function, the script does not recommend the use of return;
4, exit (mixed concluding)/die (mixed concluding) function: Directly end the current PHP script!!
If a parameter is passed in, the closing is printed first, and then the current script is ended.

Five Process Control Statement Goto

1. Usage:

Define a jump anchor point, "identifier"--"JR:"
In any position, set the goto statement to jump to the specified anchor point: "Goto JR;"


2. Function:

Jumps directly to the set identifier location when a goto statement is encountered.


3, used to implement the branch.
Note: The goto statement simply jumps the current program control flow to the specified anchor point, but is not responsible for executing a few lines of code down.
That is, all the code down from the anchor point executes sequentially. If you implement a Multipath branch, you need to match the Goto statement to skip the other branches. (see case study below)


4. Goto Implementation Loop:

See the case. However, in Goto, jumping out of a loop cannot use break.


5. The advantages and disadvantages of goto statement:
① Advantages: The use of flexible and convenient, instruction-level statements, faster efficiency, better performance.
② Cons: The massive use of Goto is a disaster for the structuring of the code.
It is not conducive to a clear code structure, is not conducive to understanding the code, and it is very likely to skip some important declaration statements, resulting in code errors.

Theory is inferior to practice, see code below for details ↓↓↓

1//goto Statement Implementation Branch 2    /* $num = true; 3    if (! $num) {4        goto JH; 5    }else{6        Goto JR; 7    } 8     9    jr:10< C8/>echo "3333333333333<br/>";    echo "4444444444444<br/>";    goto jj;13 jh:15    echo "5555555555555<br/>";    echo "6666666666666<br/>";    jj:*/19    20    //[1]goto cycle    $num = 0;22    jr:23    echo "1222222222221<br/>",    $num ++;25    if ($ num<5) {        jj;//goto jump out of the loop, note that break27}28 goto jr;29 can not be used to jj:31    echo " Hahaha ",    /*//[2]goto realization Loop     $num =0;35     jr:36     echo" 11111<br> "; PNs     $num + +;     if ($num <5) {         jr;40 goto     }*/

This is what we want to share with you today, I hope we have some help.

Bloggers once again remind you that theoretical knowledge is the basis of knocking good code, can not be ignored! "Basic PHP Basics" will continue to update, thank you for your attention ~ ~ ~

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.