Chapter 3 php operators and control structure code

Source: Internet
Author: User

I. String insertion
Difference between double quotation marks and single quotation marks:
1. Use of double quotation marks:
Copy codeThe Code is as follows:
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<? Php
// Double quotation marks can parse variables and escape characters
$ Username = "jack ";
Echo "his name is $ username! ";
Echo "<br/> ";
$ Username = "Xiaodong ";
// If the exclamation point is in English, the variable will be parsed normally.
Echo "His name is $ username! "; // His name is Xiaodong!
Echo "<br/> ";
// If it is a Chinese exclamation point, it cannot be parsed.
Echo "His name is $ username! "; // His name is
Echo "<br/> ";
// Although the escape characters are parsed here, \ n is a line break in the source code.
// Only one character is displayed in the browser
Echo "His name is $ username, \ n he is 20 years old this year"; // his name is Xiao Dong, he is 20 years old this year
Echo "<br/> ";
// To avoid errors, string connection is recommended.
Echo "His name is". $ username. "He is 20 years old."; // his name is Xiaodong. He is 20 years old.
?>

2. Use of single quotes:
Copy codeThe Code is as follows:
<? Php
// Single quotes only output the string literal value,
// The variables and escape characters are not parsed.
// The syntax is not highlighted.
$ Username = 'anllin ';
Echo 'his name is $ username, \ n his age is 20 .';
// Output
// His name is $ username, \ n his age is 20.
?>

Some common escape characters

Escape Sequence

Description

\ N

Line Break

\ R

Enter

\ T

Horizontal tabulation chart

\\

Backslash

\ $

Dollar sign

\"

Double quotation marks


Ii. Operators
Copy codeThe Code is as follows:
<? Php
// Arithmetic Operator
$ A = 5;
$ B = 3;
Echo $ a + $ B;
Echo '<br/> ';
Echo $ a-$ B;
Echo '<br/> ';
Echo $ a * $ B;
Echo '<br/> ';
Echo $ a/$ B;
Echo '<br/> ';
Echo $ a % $ B;
?>

8
2
15
1.66666666667
2
Copy codeThe Code is as follows:
<? Php
// Compound value assignment operator
$ A = 5;
$ B = 3;
Echo $ a + = $ B;
Echo '<br/> ';
Echo $ a-= $ B;
Echo '<br/> ';
Echo $ a * = $ B;
Echo '<br/> ';
Echo $ a/= $ B;
Echo '<br/> ';
Echo $ a % = $ B;
Echo '<br/> ';
Echo $ a. = $ B;
?>

8
5
15
5
2
23
Copy codeThe Code is as follows:
<? Php
// Increment/decrease Operator
$ A = 5;
Echo ++ $;
Echo '<br/> ';
Echo $ a ++;
Echo '<br/> ';
Echo -- $;
Echo '<br/> ';
Echo $ --;
?>

6
6
6
6
Copy codeThe Code is as follows:
<? Php
$ A = 5;
$ B = 3;
$ C = 5;
$ D = '5 ';
Echo $ a = $ c;
Echo '<br/> ';
Echo $ a ===$ c;
Echo '<br/> ';
Echo $ a = $ d;
Echo '<br/> ';
Echo $! = $ B;
Echo '<br/> ';
Echo $! ==$ D;
Echo '<br/> ';
Echo $! = $ B;
Echo '<br/> ';
Echo $ a> $ B;
Echo '<br/> ';
Echo $ B <$ c;
Echo '<br/> ';
Echo $ a >=$ c;
Echo '<br/> ';
Echo $ a <= $ c;
?>

1
1
1
1
1
1
1
1
1
1
Copy codeThe Code is as follows:
<? Php
$ A = false;
Echo! $;
Echo '<br/> ';
$ B = 5;
$ C = 3;
Echo $ B> 0 & $ c> 0;
Echo '<br/> ';
Echo $ B> 0 and $ c> 0;
Echo '<br/> ';
Echo $ B! = 0 | $ c! = 0;
Echo '<br/> ';
Echo $ B! = 0 or $ c! = 0;
Echo '<br/> ';
?>

1
1
1
1
1
The "and" or "operator has a lower priority than & |
Ternary Operators
Copy codeThe Code is as follows:
<? Php
$ A = 100;
Echo $ a> 60? 'Success': 'fail ';
?>

Success
Error suppression Operator
Copy codeThe Code is as follows:
<? Php
Echo @ (1, 100/0 );
?>

 

 

Iii. Control Structure
If condition judgment statement
Copy codeThe Code is as follows:
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/>
<? Php
$ A = 10;
If ($ a> 0)
{
Echo 'integer greater than 0 ';
}
Echo '<br/> ';
If ($ a> 0)
{
Echo 'integer greater than 0 ';
}
Else if ($ a <0)
{
Echo 'integer less than 0 ';
}
Else
{
Echo 'integer equal to zero ';
}
?>

Switch statement
Copy codeThe Code is as follows:
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/>
<? Php
$ Role = 'admin ';
Switch ($ role)
{
Case 'admin ':
Echo 'admin ';
Break;
Case 'user ':
Echo 'normal user ';
Break;
Case 'guest ':
Echo 'tourists ';
Break;
Default:
Echo 'tourists ';
Break;
}
?>

While loop statement
Copy codeThe Code is as follows:
<? Php
$ A = 10;
While ($ a> 0)
{
Echo $ --;
Echo '<br> ';
}
?>

Do while LOOP statement
Copy codeThe Code is as follows:
<? Php
$ A = 10;
Do
{
Echo $ --;
Echo '<br/> ';
}
While ($ a> 0)
?>

For Loop statement
Copy codeThe Code is as follows:
<? Php
For ($ a = 0; $ a <10; $ a ++)
{
Echo $;
Echo '<br/> ';
}
?>

Break statement
Copy codeThe Code is as follows:
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/>
<? Php
For ($ a = 0; $ a <10; $ a ++)
{
Echo $;
Echo '<br/> ';
If ($ a = 5)
{
Break; // terminate the loop, but the statement after the loop is executed
}
}
Echo 'loop termination ';
?>

Exit statement
Copy codeThe Code is as follows:
<? Php
For ($ a = 0; $ a <10; $ a ++)
{
Echo $;
Echo '<br/> ';
If ($ a = 5)
{
Exit; // exit directly. The statement following the loop is not executed.
}
}
Echo 'loop termination ';
?>

Continue statement
Copy codeThe Code is as follows:
<? Php
For ($ a = 0; $ a <10; $ a ++)
{
Echo $;
Echo '<br/> ';
If ($ a = 5)
{
Continue; // end the loop and continue the next loop. The statement after the loop is still executed.
}
}
Echo 'loop termination ';
?>

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.