Home > php教程 > php手册 > body text

Operators and control structures in php

WBOY
Release: 2016-07-09 09:08:06
Original
746 people have browsed it

Operators in php

1. Assignment operator
2. Arithmetic operator
3. Comparison operator
4. String operator
5. Increment operator
6. Logical operator ( Exclusive OR operation
1). Boolean value - String
2). String - Conversion of numbers
3). String - Conversion of Boolean value
4). Number - - Boolean value Conversion of
5). Conversion of numbers to strings



Control structure

1. if single-way branch

2. if /else double-way branch, the two code segments in if/else in the double-way branch, one and only one section must be executed

if/else if/else if.../else multi-way branch

3. switch case, which is convenient for comparing multiple possible values ​​of the variable being judged.


4. while (expression){

code segment}

5. do{

code segment;}while(expression);

6. for(statement 1; statement 2; statement 3) {

code segment;}

Statement 1, execute first and only once

then judge statement 2, if true, execute the code segment; then execute statement 3;
then judge statement 2... loop

until Statement 2 is false, for ends



Code display


<span style="color: #008080;"> 1</span> <span style="color: #008000;">//</span><span style="color: #008000;">============================== =============
</span><span style="color: #008080;"> 2</span>
<span style="color: #008080;"> 3</span> <span style="color: #008000;">//2. Arithmetic operations </span>Return to original position
<span style="color: #008080;"> 4</span> <span style="color: #008000;">//
</span><span style="color: #008080;"> 5</span> <span style="color: #008000;">//How to add, subtract, multiply and divide in php program
</span><span style="color: #008080;"> 6</span> <span style="color: #008000;">//In order - </span>
<span style="color: #008080;"> 7</span>
<span style="color: #008080;"> 8</span>
<span style="color: #008080;"> 9</span> <span style="color: #800080;">$a</span> = 3<span style="color: #000000;">;
</span><span style="color: #008080;"> 10</span> <span style="color: #800080;">$b</span> =5<span style="color: #000000;">;
</span><span style="color: #008080;"> 11</span> <span style="color: #800080;">$c</span> = <span style="color: #800080;">$a</span> <span style="color: #800080;">$b</span>;<span style="color: #008000;">//</span><span style="color: #008000;">This sentence The meaning is that the calculation result of $a $b is assigned to $c</span>
<span style="color: #008080;"> 12</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$c</span>,'<br />'<span style="color: #000000;">;
</span><span style="color: #008080;"> 13</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$b</span>-<span style="color: #800080;">$a</span>,'<br />'<span style="color: #000000;">;
</span><span style="color: #008080;"> 14</span>
<span style="color: #008080;"> 15</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$a</span> * <span style="color: #800080;">$b</span>,'<br />';<span style="color: #008000;">//</span><span style="color: #008000;">Calculate the result of $a * $b and assign it to $c </span>
<span style="color: #008080;"> 16</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$b</span>/<span style="color: #800080;">$a</span>,'<br />';<span style="color: #008000;">//</span><span style="color: #008000;">Calculate the result of $b/$a and assign it to $c
</span><span style="color: #008080;"> 17</span>
<span style="color: #008080;"> 18</span> <span style="color: #008000;">//If it is in C language, $b and $a are both integer types, so the calculated $c is also an integer type
</span><span style="color: #008080;"> 19</span> <span style="color: #008000;">//In php, the value of a variable can be changed at will, and the type of the variable can also be changed.
</span><span style="color: #008080;"> 20</span> <span style="color: #008000;">//
</span><span style="color: #008080;"> 21</span> <span style="color: #008000;">//For example: variable $c is an integer, and you can suddenly assign it a floating-point value.
</span><span style="color: #008080;"> 22</span> <span style="color: #008000;">//So php is called a dynamic language.</span><span style="color: #008080;"> 23</span>
<span style="color: #008080;"> 24</span>
<span style="color: #008080;"> 25</span>
<span style="color: #008080;"> 26</span>
<span style="color: #008080;"> 27</span> <span style="color: #008000;">//----------------------------------------- ---------------</span>
<span style="color: #008080;"> 28</span> <span style="color: #000000;">Finding the remainder is to take the modulo operation and return to the original position
</span><span style="color: #008080;"> 29</span> <span style="color: #000000;">When calculating division on the computer, 2 results are calculated
</span><span style="color: #008080;"> 30</span>
<span style="color: #008080;"> 31</span> <span style="color: #000000;">Quotient and remainder
</span><span style="color: #008080;"> 32</span>
<span style="color: #008080;"> 33</span> */
<span style="color: #008080;"> 34</span> <span style="color: #800080;">$a</span> = 3<span style="color: #000000;">;
</span><span style="color: #008080;"> 35</span> <span style="color: #800080;">$b</span> = 5<span style="color: #000000;">;
</span><span style="color: #008080;"> 36</span> <span style="color: #800080;">$c</span> = <span style="color: #800080;">$b</span>%<span style="color: #800080;">$a</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> gets 1 Yu 2</span>
<span style="color: #008080;"> 37</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$c</span>,'<br />'<span style="color: #000000;">;
</span><span style="color: #008080;"> 38</span>
<span style="color: #008080;"> 39</span>
<span style="color: #008080;"> 40</span> <span style="color: #008000;">//</span><span style="color: #008000;">Quick test to determine whether a number is even or odd?
</span><span style="color: #008080;"> 41</span> <span style="color: #008000;">//You can take modulo 2, the remainder is 1, then it is an odd number, if the remainder is 0, it is an odd number</span>
<span style="color: #008080;"> 42</span>
<span style="color: #008080;"> 43</span> <span style="color: #800080;">$d</span> = 3<span style="color: #000000;">;
</span><span style="color: #008080;"> 44</span> <span style="color: #800080;">$res</span> = <span style="color: #800080;">$d</span> % 2<span style="color: #000000;">;
</span><span style="color: #008080;"> 45</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$res</span> == 1<span style="color: #000000;">){
</span><span style="color: #008080;"> 46</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$d</span>,'is an odd number'<span style="color: #000000;">;
</span><span style="color: #008080;"> 47</span> <span style="color: #000000;">}
</span><span style="color: #008080;"> 48</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$res</span> == 0<span style="color: #000000;">){
</span><span style="color: #008080;"> 49</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$d</span>,'is an even number'<span style="color: #000000;">;
</span><span style="color: #008080;"> 50</span> <span style="color: #000000;">}</span><span style="color: #008080;"> 51</span>
<span style="color: #008080;"> 52</span> <span style="color: #008000;">//</span><span style="color: #008000;">I am in junior high school and have the concept of negative numbers</span>
<span style="color: #008080;"> 53</span>
<span style="color: #008080;"> 54</span> <span style="color: #800080;">$b</span> = -5<span style="color: #000000;">;
</span><span style="color: #008080;"> 55</span> <span style="color: #800080;">$a</span> = 3<span style="color: #000000;">;
</span><span style="color: #008080;"> 56</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$b</span>%<span style="color: #800080;">$a</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 57</span> <span style="color: #800080;">$b</span> = -5<span style="color: #000000;">;
</span><span style="color: #008080;"> 58</span> <span style="color: #800080;">$a</span> = -3<span style="color: #000000;">;
</span><span style="color: #008080;"> 59</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$b</span>%<span style="color: #800080;">$a</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 60</span>
<span style="color: #008080;"> 61</span> <span style="color: #008000;">//</span><span style="color: #008000;">Note: During the modulo operation,
</span><span style="color: #008080;"> 62</span> <span style="color: #008000;">//The positive or negative result of the operation depends on the sign of the dividend
</span><span style="color: #008080;"> 63</span> <span style="color: #008000;">//The sign of the result is consistent with the sign of the dividend
</span><span style="color: #008080;"> 64</span> <span style="color: #008000;">//==================================== =============
</span><span style="color: #008080;"> 65</span>
<span style="color: #008080;"> 66</span> <span style="color: #008000;"> // Comparison operator Return to original position
</span><span style="color: #008080;"> 67</span> <span style="color: #008000;">// > ,< ,<= ,>= ==,!= ,=== (not only the values ​​are equal, but the types are also equal),!= =</span>
<span style="color: #008080;"> 68</span> <span style="color: #800080;">$a</span> = 3<span style="color: #000000;">;
</span><span style="color: #008080;"> 69</span> <span style="color: #800080;">$b</span> = 2<span style="color: #000000;">;
</span><span style="color: #008080;"> 70</span> <span style="color: #0000ff;">if</span>(3 > 2<span style="color: #000000;">){
</span><span style="color: #008080;"> 71</span> <span style="color: #0000ff;">echo</span> '3 is greater than 2','<br />'<span style="color: #000000;">;
</span><span style="color: #008080;"> 72</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;"> 73</span> <span style="color: #0000ff;">echo</span> '3 is not greater than 2','<br />'<span style="color: #000000;">;
</span><span style="color: #008080;"> 74</span> <span style="color: #000000;">}
</span><span style="color: #008080;"> 75</span>
<span style="color: #008080;"> 76</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$a</span> >= <span style="color: #800080;">$b</span>){ <span style="color: #008000;">//</span><span style="color: #008000;"> >= It means that $a is greater than $b, or $a is equal to $b, either of which is true, that is, the condition satisfies </span>
<span style="color: #008080;"> 77</span> <span style="color: #0000ff;">echo</span> '3 is greater than or equal to 2','<br />'<span style="color: #000000;">;
</span><span style="color: #008080;"> 78</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;"> 79</span> <span style="color: #0000ff;">echo</span> '3 is not greater than or equal to 2','<br />'<span style="color: #000000;">;
</span><span style="color: #008080;"> 80</span> <span style="color: #000000;">}
</span><span style="color: #008080;"> 81</span> <span style="color: #008000;">//</span><span style="color: #008000;">-------------------------------- ------------------</span>
<span style="color: #008080;"> 82</span>
<span style="color: #008080;"> 83</span> <span style="color: #800080;">$a</span> = 3<span style="color: #000000;">;
</span><span style="color: #008080;"> 84</span> <span style="color: #800080;">$b</span> = '3'<span style="color: #000000;">;
</span><span style="color: #008080;"> 85</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$a</span> === <span style="color: #800080;">$b</span><span style="color: #000000;">){
</span><span style="color: #008080;"> 86</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$a</span>,'all equal to',<span style="color: #800080;">$b</span>,'<br />'<span style="color: #000000;">;
</span><span style="color: #008080;"> 87</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;"> 88</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$a</span>,'not exactly equal',<span style="color: #800080;">$b</span>,'<br />'<span style="color: #000000;">;
</span><span style="color: #008080;"> 89</span> <span style="color: #000000;">}
</span><span style="color: #008080;"> 90</span>
<span style="color: #008080;"> 91</span> <span style="color: #008000;">//</span><span style="color: #008000;">================================ ==========</span>
<span style="color: #008080;"> 92</span>
<span style="color: #008080;"> 93</span> <span style="color: #800080;">$a</span> = 3<span style="color: #000000;">;
</span><span style="color: #008080;"> 94</span> <span style="color: #800080;">$b</span> = <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 95</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$a</span> == <span style="color: #800080;">$b</span><span style="color: #000000;">){
</span><span style="color: #008080;"> 96</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$a</span>,'equal to',<span style="color: #800080;">$b</span>,'<br />'<span style="color: #000000;"> ;
</span><span style="color: #008080;"> 97</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;"> 98</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$a</span>,'not equal to',<span style="color: #800080;">$b</span>,'<br />'<span style="color: #000000;">;
</span><span style="color: #008080;"> 99</span> <span style="color: #000000;">}</span><span style="color: #008080;">100</span> <span style="color: #008000;">//</span><span style="color: #008000;">When comparing different types
</span><span style="color: #008080;">101</span> <span style="color: #008000;">//php program itself
</span><span style="color: #008080;">102</span> <span style="color: #008000;">//
</span><span style="color: #008080;">103</span> <span style="color: #008000;">//The variable will be converted into a consistent type and then compared
</span><span style="color: #008080;">104</span> <span style="color: #008000;">//
</span><span style="color: #008080;">105</span> <span style="color: #008000;">//Remember the following conversions to return to the original position
</span><span style="color: #008080;">106</span> <span style="color: #008000;">//1. Boolean value - string
</span><span style="color: #008080;">107</span> <span style="color: #008000;">///2. String - Number conversion
</span><span style="color: #008080;">108</span> <span style="color: #008000;">///3. String - Boolean value conversion
</span><span style="color: #008080;">109</span> <span style="color: #008000;">//4. Number - - Boolean value conversion
</span><span style="color: #008080;">110</span> <span style="color: #008000;">///5. Number - Conversion of string
</span><span style="color: #008080;">111</span> <span style="color: #008000;">//
</span><span style="color: #008080;">112</span> <span style="color: #008000;">//Take 5 == true comparison as an example
</span><span style="color: #008080;">113</span> <span style="color: #008000;">//Convert the integer type to Boolean type
</span><span style="color: #008080;">114</span> <span style="color: #008000;">//
</span><span style="color: #008080;">115</span> <span style="color: #008000;">//In fact, in some languages, there is no Boolean type at all, and 1 and 0 are used as true/false
</span><span style="color: #008080;">116</span> <span style="color: #008000;">//0 / 0.0 These two are converted to boolean type to false
</span><span style="color: #008080;">117</span> <span style="color: #008000;">//Everything else is true, and negative numbers are also true
</span><span style="color: #008080;">118</span>
<span style="color: #008080;">119</span>
<span style="color: #008080;">120</span>
<span style="color: #008080;">121</span>
<span style="color: #008080;">122</span> <span style="color: #008000;">//----------------------------------------- -----
</span><span style="color: #008080;">123</span> <span style="color: #008000;">//1. Boolean value - string </span>
<span style="color: #008080;">124</span> <span style="color: #0000ff;">true</span> ---'1'
<span style="color: #008080;">125</span> <span style="color: #0000ff;">false</span>--- ''
<span style="color: #008080;">126</span>
<span style="color: #008080;">127</span> <span style="color: #008000;">//</span><span style="color: #008000;">-------------------------------- ----------
</span><span style="color: #008080;">128</span> <span style="color: #008000;">//1. String - conversion of numbers
</span><span style="color: #008080;">129</span> <span style="color: #008000;">//Start looking for numbers from the left end of the string until the end of the first non-number. The part taken out is the converted value
</span><span style="color: #008080;">130</span> <span style="color: #008000;">//'3q'----3</span>
<span style="color: #008080;">131</span> '123asdf'---123
<span style="color: #008080;">132</span> 'sdfds789'----0
<span style="color: #008080;">133</span>
<span style="color: #008080;">134</span> <span style="color: #800080;">$num</span> = 'sdfds789'<span style="color: #000000;">;
</span><span style="color: #008080;">135</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$num</span> 0; <span style="color: #008000;">//</span><span style="color: #008000;">The result of the operation is 0
</span><span style="color: #008080;">136</span>
<span style="color: #008080;">137</span> <span style="color: #008000;">//----------------------------------------- -----
</span><span style="color: #008080;">138</span> <span style="color: #008000;">///3. String - Boolean conversion
</span><span style="color: #008080;">139</span> <span style="color: #008000;">//''String, '0', this is converted to false
</span><span style="color: #008080;">140</span> <span style="color: #008000;">//Everything else is true</span>
<span style="color: #008080;">141</span>
<span style="color: #008080;">142</span>
<span style="color: #008080;">143</span>
<span style="color: #008080;">144</span> <span style="color: #0000ff;">if</span>(''==<span style="color: #0000ff;">false</span><span style="color: #000000;">){
</span><span style="color: #008080;">145</span> <span style="color: #0000ff;">echo</span> 'Empty string equals false'<span style="color: #000000;">;
</span><span style="color: #008080;">146</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">147</span> <span style="color: #0000ff;">echo</span> 'Empty string is not equal to false'<span style="color: #000000;">;
</span><span style="color: #008080;">148</span> <span style="color: #000000;">}
</span><span style="color: #008080;">149</span>
<span style="color: #008080;">150</span> <span style="color: #0000ff;">echo</span> '<br />'<span style="color: #000000;">;
</span><span style="color: #008080;">151</span> <span style="color: #0000ff;">if</span>('0'==<span style="color: #0000ff;">false</span><span style="color: #000000;">){
</span><span style="color: #008080;">152</span> <span style="color: #0000ff;">echo</span> 'String 0 equals false'<span style="color: #000000;">;
</span><span style="color: #008080;">153</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">154</span> <span style="color: #0000ff;">echo</span> 'String 0 is not equal to false'<span style="color: #000000;">;
</span><span style="color: #008080;">155</span> <span style="color: #000000;">}</span><span style="color: #008080;">156</span>
<span style="color: #008080;">157</span>
<span style="color: #008080;">158</span> <span style="color: #008000;">//</span><span style="color: #008000;">-------------------------------- ----------
</span><span style="color: #008080;">159</span> <span style="color: #008000;">///4. String - Boolean conversion
</span><span style="color: #008080;">160</span> <span style="color: #008000;">// Only integer 0, and floating point 0.0, convert to false
</span><span style="color: #008080;">161</span> <span style="color: #008000;">//Others, even negative numbers, are converted to true
</span><span style="color: #008080;">162</span>
<span style="color: #008080;">163</span>
<span style="color: #008080;">164</span>
<span style="color: #008080;">165</span> <span style="color: #008000;">//----------------------------------------- -----
</span><span style="color: #008080;">166</span> <span style="color: #008000;">//5. Number - Conversion of string
</span><span style="color: #008080;">167</span> <span style="color: #008000;">//Directly the literal itself </span>
<span style="color: #008080;">168</span> <span style="color: #0000ff;">echo</span> '<br />'<span style="color: #000000;">;
</span><span style="color: #008080;">169</span> <span style="color: #0000ff;">echo</span> 'aaa'. 2323<span style="color: #000000;">;
</span><span style="color: #008080;">170</span>
<span style="color: #008080;">171</span>
<span style="color: #008080;">172</span>
<span style="color: #008080;">173</span> <span style="color: #008000;">//</span><span style="color: #008000;">-------------------------------- ----------
</span><span style="color: #008080;">174</span> <span style="color: #008000;">//String Return to original position
</span><span style="color: #008080;">175</span>
<span style="color: #008080;">176</span> <span style="color: #008000;">//In php strings, there is an operation, which is splicing (the difference between ',' and '.' in echo)</span>
<span style="color: #008080;">177</span>
<span style="color: #008080;">178</span> <span style="color: #800080;">$str1</span> = 'hello'<span style="color: #000000;">;
</span><span style="color: #008080;">179</span> <span style="color: #800080;">$str2</span> = ''<span style="color: #000000;">;
</span><span style="color: #008080;">180</span>
<span style="color: #008080;">181</span> <span style="color: #800080;">$str3</span> = 'world'<span style="color: #000000;">;
</span><span style="color: #008080;">182</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$str1</span>.<span style="color: #800080;">$str2</span>.<span style="color: #800080;">$str3</span><span style="color: #000000;">;
</span><span style="color: #008080;">183</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$str1</span>,<span style="color: #800080;">$str2</span>,<span style="color: #800080;">$str3</span><span style="color: #000000;">;
</span><span style="color: #008080;">184</span>
<span style="color: #008080;">185</span> <span style="color: #008000;">//</span><span style="color: #008000;"> Questions for newbies </span>
<span style="color: #008080;">186</span>
<span style="color: #008080;">187</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$str1</span>.<span style="color: #800080;">$str2</span>.<span style="color: #800080;">$str3</span><span style="color: #000000;">;
</span><span style="color: #008080;">188</span> <span style="color: #008000;">//</span><span style="color: #008000;">This line is the splicing operation of $str1, $str2, $str3 in memory
</span><span style="color: #008080;">189</span> <span style="color: #008000;">//Calculate a 'hello world' a large string,
</span><span style="color: #008080;">190</span> <span style="color: #008000;">//Then echo it to get the result of the judgment hello world
</span><span style="color: #008080;">191</span> <span style="color: #008000;">//==================================== =======</span>
<span style="color: #008080;">192</span>
<span style="color: #008080;">193</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$str1</span>,<span style="color: #800080;">$str2</span>,<span style="color: #800080;">$str3</span><span style="color: #000000;">;
</span><span style="color: #008080;">194</span> <span style="color: #008000;">//</span><span style="color: #008000;">echo is a statement, responsible for output
</span><span style="color: #008080;">195</span> <span style="color: #008000;">//You can use ',' to split and output multiple variables or values ​​at once.
</span><span style="color: #008080;">196</span> <span style="color: #008000;">//
</span><span style="color: #008080;">197</span> <span style="color: #008000;">//To summarize:
</span><span style="color: #008080;">198</span> <span style="color: #008000;">//Use ' . ' to splice first and then output the whole
</span><span style="color: #008080;">199</span><span style="color: #008000;">//Use ' , ' to output the three variables one by one.</span><span style="color: #008080;">200</span> <span style="color: #008000;">//
</span><span style="color: #008080;">201</span> <span style="color: #008000;">//During the interview, you will encounter questions like:
</span><span style="color: #008080;">202</span> <span style="color: #008000;">//Use ' . ', use ' , ' which outputs quickly and efficiently
</span><span style="color: #008080;">203</span> <span style="color: #008000;">//
</span><span style="color: #008080;">204</span> <span style="color: #008000;">//Answer: Of course ',' The output is fast and the efficiency is high</span>
<span style="color: #008080;">206</span> <span style="color: #008000;">//======================================== ====
</span><span style="color: #008080;">207</span> <span style="color: #008000;">//The assignment operator is =. Its function is to assign the value on the right side of '=' to the variable on the left <span style="color: #008000;">Return to the original position</span></span>
<span style="color: #008080;">208</span> <span style="color: #800080;">$a</span> = 3<span style="color: #000000;">;
</span><span style="color: #008080;">209</span> <span style="color: #800080;">$b</span> = <span style="color: #800080;">$c</span> = 5<span style="color: #000000;">;
</span><span style="color: #008080;">210</span>
<span style="color: #008080;">211</span>
<span style="color: #008080;">212</span> <span style="color: #008000;">//</span><span style="color: #008000;">================================== ==========
</span><span style="color: #008080;">213</span> <span style="color: #008000;">//The result of the operator
</span><span style="color: #008080;">214</span> <span style="color: #008000;">//
</span><span style="color: #008080;">215</span> <span style="color: #008000;">//Everyone thinks: since it is an operation, after the operation is completed, there must be a result - the result value
</span><span style="color: #008080;">216</span> <span style="color: #008000;">//3 2; Operation result 5
</span><span style="color: #008080;">217</span> <span style="color: #008000;">//
</span><span style="color: #008080;">218</span> <span style="color: #008000;">//1. Comparison operator Return to original position
</span><span style="color: #008080;">219</span> <span style="color: #008000;">//Note: Comparing descriptors returns results, there are only 2 possibilities: true/false
</span><span style="color: #008080;">220</span> <span style="color: #008000;">//
</span><span style="color: #008080;">221</span> <span style="color: #008000;">//Take $a == $b as an example, either wait or not.
</span><span style="color: #008080;">222</span> <span style="color: #008000;">//It’s either true or false
</span><span style="color: #008080;">223</span> <span style="color: #008000;">//2 == 3; the operation result is: false
</span><span style="color: #008080;">224</span>
<span style="color: #008080;">225</span> <span style="color: #008000;">//======================================== ====
</span><span style="color: #008080;">226</span> <span style="color: #008000;">//2. String operator Return to original position
</span><span style="color: #008080;">227</span> <span style="color: #008000;">//String operator
</span><span style="color: #008080;">228</span> <span style="color: #008000;">//$a.$b
</span><span style="color: #008080;">229</span> <span style="color: #008000;">//Return result: spliced ​​result
</span><span style="color: #008080;">230</span>
<span style="color: #008080;">231</span> <span style="color: #008000;">//======================================== ====
</span><span style="color: #008080;">232</span> <span style="color: #008000;">//Assignment operator, what is the return value?</span>
<span style="color: #008080;">233</span> <span style="color: #800080;">$a</span> = 3<span style="color: #000000;">;
</span><span style="color: #008080;">234</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$a</span> = 33<span style="color: #000000;">){
</span><span style="color: #008080;">235</span> <span style="color: #0000ff;">echo</span> 'aa'<span style="color: #000000;">;
</span><span style="color: #008080;">236</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">237</span> <span style="color: #0000ff;">echo</span> 'bb'<span style="color: #000000;">;
</span><span style="color: #008080;">238</span> <span style="color: #000000;">}
</span><span style="color: #008080;">239</span> <span style="color: #008000;">//</span><span style="color: #008000;">The result of the long assignment operator is the value on the right side of = </span>
<span style="color: #008080;">240</span> <span style="color: #800080;">$a</span> = <span style="color: #800080;">$b</span> = <span style="color: #800080;">$c</span> =88
<span style="color: #008080;">241</span> <span style="color: #008000;">//</span><span style="color: #008000;">The process is like this, $b = 88 is performed first, and 88 is returned after assignment
</span><span style="color: #008080;">242</span> <span style="color: #008000;">//The returned 88 is assigned to $c
</span><span style="color: #008080;">243</span> <span style="color: #008000;">//Similarly…
</span><span style="color: #008080;">244</span> <span style="color: #008000;">//Finally $a is also assigned a value of 88
</span><span style="color: #008080;">245</span> <span style="color: #008000;">//
</span><span style="color: #008080;">246</span> <span style="color: #008000;">//
</span><span style="color: #008080;">247</span> <span style="color: #008000;">//Two difficulties;
</span><span style="color: #008080;">248</span> <span style="color: #008000;">//1. There is a type conversion problem during comparison operations
</span><span style="color: #008080;">249</span> <span style="color: #008000;">//2. Every operation must have a return value
</span><span style="color: #008080;">250</span> <span style="color: #008000;">//The comparison operation returns a Boolean value
</span><span style="color: #008080;">251</span> <span style="color: #008000;">//
</span><span style="color: #008080;">252</span> <span style="color: #008000;">//Assignment operation, returns the assigned value itself
</span><span style="color: #008080;">253</span>
<span style="color: #008080;">254</span> <span style="color: #008000;">//======================================== ====
</span><span style="color: #008080;">255</span> <span style="color: #008000;">//6. Logical operator Return to original position
</span><span style="color: #008080;">256</span> <span style="color: #008000;">//
</span><span style="color: #008080;">257</span> <span style="color: #008000;">//Proposition:
</span><span style="color: #008080;">258</span> <span style="color: #008000;">//True proposition, false proposition
</span><span style="color: #008080;">259</span> <span style="color: #008000;">//
</span><span style="color: #008080;">260</span> <span style="color: #008000;">//Logical operator: logical or || or
</span><span style="color: #008080;">261</span> <span style="color: #008000;">//Logical AND && and </span>
<span style="color: #008080;">262</span>
<span style="color: #008080;">263</span> <span style="color: #800080;">$house</span> = 180<span style="color: #000000;">;
</span><span style="color: #008080;">264</span> <span style="color: #800080;">$car</span> = 'BYD'<span style="color: #000000;">;
</span><span style="color: #008080;">265</span>
<span style="color: #008080;">266</span> <span style="color: #008000;">//</span><span style="color: #008000;">$res = $house && $car == 'BMW' If written like this, who will combine it first, which involves the issue of operation priority
</span><span style="color: #008080;">267</span> <span style="color: #008000;">//We recommend that when multiple quantities are to be moved, they need to be wrapped in a unit first and wrapped with ().For example: $res = $house && ($car == 'BMW')
</span><span style="color: #008080;">268</span> <span style="color: #008000;">//This can make the program easy to read and understand, and less likely to cause ambiguity </span>
<span style="color: #008080;">269</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$res</span> = <span style="color: #800080;">$house</span> && <span style="color: #800080;">$car</span> == 'BMW'<span style="color: #000000;">) {
</span><span style="color: #008080;">270</span> <span style="color: #0000ff;">echo</span> 'You can still marry'<span style="color: #000000;">;
</span><span style="color: #008080;">271</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">272</span> <span style="color: #0000ff;">echo</span> 'Wait two more years'<span style="color: #000000;">;
</span><span style="color: #008080;">273</span> <span style="color: #000000;">}
</span><span style="color: #008080;">274</span>
<span style="color: #008080;">275</span> <span style="color: #000000;">Priority of logical operations
</span><span style="color: #008080;">276</span> && has a higher priority than ||<span style="color: #000000;">
</span><span style="color: #008080;">277</span> <span style="color: #000000;">But in development, it is not recommended to write them together and wrap them with application ()
</span><span style="color: #008080;">278</span> <span style="color: #800080;">$a</span> = 3<span style="color: #000000;">;
</span><span style="color: #008080;">279</span> <span style="color: #800080;">$b</span> = 5<span style="color: #000000;">;
</span><span style="color: #008080;">280</span> <span style="color: #0000ff;">if</span>((<span style="color: #800080;">$a</span>==8)||(<span style="color: #800080;">$b</span>=9<span style="color: #000000;">)){
</span><span style="color: #008080;">281</span> <span style="color: #800080;">$a</span> <span style="color: #000000;">;
</span><span style="color: #008080;">282</span> <span style="color: #800080;">$b</span> <span style="color: #000000;">;
</span><span style="color: #008080;">283</span> <span style="color: #000000;">}
</span><span style="color: #008080;">284</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$a</span>,'<br />',<span style="color: #800080;">$b</span><span style="color: #000000;">;
</span><span style="color: #008080;">285</span>
<span style="color: #008080;">286</span> <span style="color: #008000;">//</span><span style="color: #008000;">================================ ==========
</span><span style="color: #008080;">287</span>
<span style="color: #008080;">288</span> <span style="color: #008000;">//Logical inverse
</span><span style="color: #008080;">289</span> <span style="color: #008000;">//If a proposition is a true proposition, its counter-proposition is a false proposition
</span><span style="color: #008080;">290</span> <span style="color: #008000;">//Use ! to express </span><span style="color: #008080;">291</span>
<span style="color: #008080;">292</span> <span style="color: #008000;">//======================================== ====
</span><span style="color: #008080;">293</span> <span style="color: #008000;">//Exclusive OR operation xor returns to the original position
</span><span style="color: #008080;">294</span> <span style="color: #008000;">//XOR means that two conditions must be ‘one true’ and ‘one false’ to be true </span>
<span style="color: #008080;">295</span>
<span style="color: #008080;">296</span> <span style="color: #800080;">$li</span> = 'Male'<span style="color: #000000;">;
</span><span style="color: #008080;">297</span> <span style="color: #800080;">$mei</span> = 'Female'<span style="color: #000000;">;
</span><span style="color: #008080;">298</span> <span style="color: #008000;">//</span><span style="color: #008000;">What this XOR expresses is that one must be male, </span>
<span style="color: #008080;">299</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$li</span> == 'Male') xor (<span style="color: #800080;">$mei</span> == 'Male'<span style="color: #000000;">){
</span><span style="color: #008080;">300</span> <span style="color: #0000ff;">echo</span> 'Can get married'<span style="color: #000000;">;
</span><span style="color: #008080;">301</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">302</span> <span style="color: #0000ff;">echo</span> 'Law prohibited, please go to Northern Europe'<span style="color: #000000;">;
</span><span style="color: #008080;">303</span> <span style="color: #000000;">}</span><span style="color: #008080;">304</span>
<span style="color: #008080;">305</span>
<span style="color: #008080;">306</span> <span style="color: #008000;">//</span><span style="color: #008000;">================================ ==========
</span><span style="color: #008080;">307</span> <span style="color: #008000;">//Ternary operator Return to original position
</span><span style="color: #008080;">308</span> <span style="color: #008000;">//
</span><span style="color: #008080;">309</span> <span style="color: #008000;">//Condition? Return value 1: Return value 2
</span><span style="color: #008080;">310</span> <span style="color: #008000;">//If the condition is true, return value 1, otherwise return value 2
</span><span style="color: #008080;">311</span> <span style="color: #008000;">//
</span><span style="color: #008080;">312</span> <span style="color: #008000;">//
</span><span style="color: #008080;">313</span> <span style="color: #008000;">//For example, determine whether a person is logged in on the website
</span><span style="color: #008080;">314</span> <span style="color: #008000;">//If logged in and the username is not empty, the username will be displayed
</span><span style="color: #008080;">315</span> <span style="color: #008000;">//If you are not logged in and the username is empty, the visitor will be displayed. .
</span><span style="color: #008080;">316</span>
<span style="color: #008080;">317</span> <span style="color: #008000;">//$user = 'zhangsan';
</span><span style="color: #008080;">318</span> <span style="color: #008000;">//if(!=''){
</span><span style="color: #008080;">319</span> <span style="color: #008000;">// echo $user;
</span><span style="color: #008080;">320</span> <span style="color: #008000;">//}else{
</span><span style="color: #008080;">321</span> <span style="color: #008000;">// echo 'visitor';
</span><span style="color: #008080;">322</span> <span style="color: #008000;">//}
</span><span style="color: #008080;">323</span> <span style="color: #008000;">//
</span><span style="color: #008080;">324</span> <span style="color: #008000;">//$user = 'zhangsan';
</span><span style="color: #008080;">325</span> <span style="color: #008000;">//echo $user?$user:'Visitor';
</span><span style="color: #008080;">326</span>
<span style="color: #008080;">327</span> <span style="color: #008000;">//If the program does not have the ternary operator, what can be used instead?
</span><span style="color: #008080;">328</span> <span style="color: #008000;">//
</span><span style="color: #008080;">329</span> <span style="color: #008000;">//can be replaced by if/else
</span><span style="color: #008080;">330</span> <span style="color: #008000;">//Sanyuanzhong is a simplified way of writing
</span><span style="color: #008080;">331</span> <span style="color: #008000;">//And the efficiency is not as high as if/esle
</span><span style="color: #008080;">332</span>
<span style="color: #008080;">333</span>
<span style="color: #008080;">334</span> <span style="color: #008000;">//======================================== ====
</span><span style="color: #008080;">335</span> <span style="color: #008000;">//Increment and decrement operators Return to original position
</span><span style="color: #008080;">336</span> <span style="color: #008000;">//
</span><span style="color: #008080;">337</span> <span style="color: #008000;">//Even if there is no increment or decrement in the language, it will not affect our writing of programs.</span><span style="color: #008080;">338</span> <span style="color: #008000;">//This is also a simplified way of writing some scenes
</span><span style="color: #008080;">339</span>
<span style="color: #008080;">340</span> <span style="color: #008000;">//Analysis, $b 1 is an arithmetic operation, and the calculated result is 3. This 3 is a ‘calculated result’ and has nothing to do with $b
</span><span style="color: #008080;">341</span> <span style="color: #008000;">//The result of this calculation is worth $a
</span><span style="color: #008080;">342</span> <span style="color: #008000;">//Therefore, $a is 3 and $b is 2</span>
<span style="color: #008080;">343</span> <span style="color: #800080;">$b</span> = 2<span style="color: #000000;">;
</span><span style="color: #008080;">344</span> <span style="color: #800080;">$a</span> = <span style="color: #800080;">$b</span> 1<span style="color: #000000;">;
</span><span style="color: #008080;">345</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$a</span>,'~',<span style="color: #800080;">$b</span>,'<br />'<span style="color: #000000;"> ;
</span><span style="color: #008080;">346</span>
<span style="color: #008080;">347</span> <span style="color: #008000;">//</span><span style="color: #008000;">$b, this is an incremental operation, and there must be a destination result when going.
</span><span style="color: #008080;">348</span> <span style="color: #008000;">//Question: What is the result of $b?
</span><span style="color: #008080;">349</span> <span style="color: #008000;">//Answer: $b, the result of the operation is $b itself
</span><span style="color: #008080;">350</span> <span style="color: #008000;">//Then change the value of $b to 1
</span><span style="color: #008080;">351</span> <span style="color: #008000;">//It is equivalent to return $b; $b = $b 1;
</span><span style="color: #008080;">352</span> <span style="color: #008000;">//$b =1;
</span><span style="color: #008080;">353</span> <span style="color: #008000;">//$a = $b;
</span><span style="color: #008080;">354</span> <span style="color: #008000;">//echo $a,'<br />',$b;
</span><span style="color: #008080;">355</span> <span style="color: #008000;">//
</span><span style="color: #008080;">356</span> <span style="color: #008000;">//
</span><span style="color: #008080;">357</span> <span style="color: #008000;">//==================================== =======</span>
<span style="color: #008080;">358</span> <span style="color: #008000;">/*</span><span style="color: #008000;">Bitwise operators, to put it bluntly, binary operators Return to original position
</span><span style="color: #008080;">359</span> <span style="color: #008000;">//People have 10 fingers, use decimal for easy calculation
</span><span style="color: #008080;">360</span> <span style="color: #008000;">//
</span><span style="color: #008080;">361</span> <span style="color: #008000;">//Mayans, don’t wear shoes, even toes are counted, round 20
</span><span style="color: #008080;">362</span> <span style="color: #008000;">//It doesn’t matter how much the carry is, the key is to facilitate our use
</span><span style="color: #008080;">363</span> <span style="color: #008000;">//
</span><span style="color: #008080;">364</span> <span style="color: #008000;">//For computers, all bits have only two states 0 1
</span><span style="color: #008080;">365</span> <span style="color: #008000;">//Because in the hundredth digit, 1 represents 100
</span><span style="color: #008080;">366</span> <span style="color: #008000;">//In carry of 10, 1 represents 10
</span><span style="color: #008080;">367</span> <span style="color: #008000;">//The ones digit, 1 represents 1
</span><span style="color: #008080;">368</span> <span style="color: #008000;">//
</span><span style="color: #008080;">369</span> <span style="color: #008000;">//We know that the information in the computer is stored as 0, 1
</span><span style="color: #008080;">370</span> <span style="color: #008000;">//One can store 0/1 units, called bits
</span><span style="color: #008080;">371</span> <span style="color: #008000;">//
</span><span style="color: #008080;">372</span> <span style="color: #008000;">//8 bits make up a byte, Byte
</span><span style="color: #008080;">373</span> <span style="color: #008000;">//Bytes are the most basic unit in computers.</span><span style="color: #008080;">374</span>
<span style="color: #008080;">375</span>
<span style="color: #008080;">376</span> <span style="color: #008000;">12 = 8 4;
</span><span style="color: #008080;">377</span> <span style="color: #008000;"> =1*2^3 1*2^2 0*2^1 0*2^0
</span><span style="color: #008080;">378</span> <span style="color: #008000;"> =1100
</span><span style="color: #008080;">379</span> <span style="color: #008000;">5=101
</span><span style="color: #008080;">380</span> <span style="color: #008000;">But in a computer, the most basic unit is a byte. There are 8 bits, but what if only 3 are used?
</span><span style="color: #008080;">381</span> <span style="color: #008000;">Answer: Write the 3 or 4 bits used on the right side of the byte
</span><span style="color: #008080;">382</span> <span style="color: #008000;"> These bits on the left are filled with 0s.
</span><span style="color: #008080;">383</span> <span style="color: #008000;">Therefore, in the computer:
</span><span style="color: #008080;">384</span> <span style="color: #008000;">12=0000 1100
</span><span style="color: #008080;">385</span> <span style="color: #008000;">5 =0000 0101
</span><span style="color: #008080;">386</span>
<span style="color: #008080;">387</span>
<span style="color: #008080;">388</span> <span style="color: #008000;">12 & 5 = 0000 0100 = 4
</span><span style="color: #008080;">389</span> <span style="color: #008000;">12 | 5 = 0000 1101 = 13
</span><span style="color: #008080;">390</span> <span style="color: #008000;">12 ^ 5 = 0000 1001 =9
</span><span style="color: #008080;">391</span> <span style="color: #008000;">Bitwise operator
</span><span style="color: #008080;">392</span> <span style="color: #008000;">& Bitwise AND , align the two binary values, treat 1 as true and 0 as false, and then perform AND operations on each bit.
</span><span style="color: #008080;">393</span> <span style="color: #008000;">|Bitwise OR, combine the two binary values, treat 1 as true, treat 0 as false, and then perform OR operation on each bit.
</span><span style="color: #008080;">394</span>
<span style="color: #008080;">395</span> <span style="color: #008000;">^Bitwise XOR: Align two binary values, treat 1 as true and 0 as false, and then perform XOR operation when each bit is different.</span><span style="color: #008080;">396</span>
<span style="color: #008080;">397</span>
<span style="color: #008080;">398</span> <span style="color: #008000;">Remind everyone: Computers are based on bit calculations. When we operate, we often use decimal directly.
</span><span style="color: #008080;">399</span> <span style="color: #008000;">*/</span>
<span style="color: #008080;">400</span> <span style="color: #008000;">//</span><span style="color: #008000;">================================ ==========</span>
<span style="color: #008080;">401</span>
<span style="color: #008080;">402</span> <span style="color: #800080;">$a</span> = 12<span style="color: #000000;">;
</span><span style="color: #008080;">403</span> <span style="color: #800080;">$a</span> = 0000 1100
<span style="color: #008080;">404</span> <span style="color: #008000;">//</span><span style="color: #008000;">Bitwise negation</span>
<span style="color: #008080;">405</span> <span style="color: #800080;">$a</span> = 1111 0011
<span style="color: #008080;">406</span> <span style="color: #0000ff;">echo</span> ~12
<span style="color: #008080;">407</span> <span style="color: #008000;">//</span><span style="color: #008000;">Representing negative numbers in computers is calculated using the two’s complement rule
</span><span style="color: #008080;">408</span> <span style="color: #008000;">//This is how to calculate negative numbers, assuming there are 8 digits
</span><span style="color: #008080;">409</span> <span style="color: #008000;">//The highest bit is 1, and the sign is negative
</span><span style="color: #008080;">410</span> <span style="color: #008000;">//
</span><span style="color: #008080;">411</span> <span style="color: #008000;">//Start the complement rule
</span><span style="color: #008080;">412</span> <span style="color: #008000;">//Calculate the absolute value of the other 7 bits except the sign bit
</span><span style="color: #008080;">413</span> <span style="color: #008000;">//(1)111 0011 =115
</span><span style="color: #008080;">414</span> <span style="color: #008000;">//Then absolute value -2^7 = 115 -128 = -13
</span><span style="color: #008080;">415</span> <span style="color: #008000;">//
</span><span style="color: #008080;">416</span> <span style="color: #008000;">//Therefore:
</span><span style="color: #008080;">417</span> <span style="color: #008000;">//1000 0001 ---Don’t take it for granted -1
</span><span style="color: #008080;">418</span> <span style="color: #008000;">//But 1-2^7 = -127</span>
<span style="color: #008080;">419</span> 1111 1111 --- 127-128 = -1
<span style="color: #008080;">420</span> 1000 0000 --- 0-128 =-128
<span style="color: #008080;">421</span>
<span style="color: #008080;">422</span> 0000 0000 --- 0
<span style="color: #008080;">423</span> 0111 1111 --- 127
<span style="color: #008080;">424</span>
<span style="color: #008080;">425</span> <span style="color: #008000;">//</span><span style="color: #008000;">Why someone asked: -128 127
</span><span style="color: #008080;">426</span> <span style="color: #008000;">//
</span><span style="color: #008080;">427</span> <span style="color: #008000;">//Intuitive understanding
</span><span style="color: #008080;">428</span> <span style="color: #008000;">//-128 --- 0 0 --- 127
</span><span style="color: #008080;">429</span> <span style="color: #008000;">//Bit operations are better mastered. Don’t worry if you are not familiar with them.
</span><span style="color: #008080;">430</span>
<span style="color: #008080;">431</span>
<span style="color: #008080;">432</span> <span style="color: #008000;">//======================================== ====
</span><span style="color: #008080;">433</span> <span style="color: #008000;">//Shift operation Return to original position</span>
<span style="color: #008080;">434</span>
<span style="color: #008080;">435</span> 5 = 0000 0101<span style="color: #000000;">;
</span><span style="color: #008080;">436</span> 5 << 1; <span style="color: #008000;">//</span><span style="color: #008000;">Move left
</span><span style="color: #008080;">437</span> <span style="color: #008000;">//Equivalent to multiplying by 2</span>
<span style="color: #008080;">438</span> 0000 1010; <span style="color: #008000;">//</span><span style="color: #008000;">The result is 10 </span>
<span style="color: #008080;">439</span> 5 >> 1; <span style="color: #008000;">//</span><span style="color: #008000;">Move right</span>
<span style="color: #008080;">440</span> 0000 0010
<span style="color: #008080;">441</span> <span style="color: #008000;">//</span><span style="color: #008000;">Equivalent to dividing by 2
</span><span style="color: #008080;">442</span>
<span style="color: #008080;">443</span>
<span style="color: #008080;">444</span> <span style="color: #008000;">//======================================== ====
</span><span style="color: #008080;">445</span> <span style="color: #008000;">//I learned about ‘quantity’ before – variables, constants
</span><span style="color: #008080;">446</span> <span style="color: #008000;">//Arithmetic operations can be performed between quantities, operators
</span><span style="color: #008080;">447</span> <span style="color: #008000;">//
</span><span style="color: #008080;">448</span> <span style="color: #008000;">//If there are only operations on quantities and quantities, then the knowledge we learn is only enough to make a calculator
</span><span style="color: #008080;">449</span>
<span style="color: #008080;">450</span> <span style="color: #008000;">//======================================== ====
</span><span style="color: #008080;">451</span> <span style="color: #008000;">//If you want to write a program: you must have a control structure
</span><span style="color: #008080;">452</span> <span style="color: #008000;">//
</span><span style="color: #008080;">453</span> <span style="color: #008000;">//The control structure is the conditions and logic that controls the execution order of the code
</span><span style="color: #008080;">454</span> <span style="color: #008000;">//
</span><span style="color: #008080;">455</span> <span style="color: #008000;">//Programmer uses php --- training --- computer
</span><span style="color: #008080;">456</span> <span style="color: #008000;">//if --- if
</span><span style="color: #008080;">457</span> <span style="color: #008000;">//else if ---else if
</span><span style="color: #008080;">458</span> <span style="color: #008000;">//otherwise ---- else </span>
<span style="color: #008080;">459</span> <span style="color: #800080;">$gender</span> = <span style="color: #800080;">$_GET</span>['gender'<span style="color: #000000;">];
</span><span style="color: #008080;">460</span> <span style="color: #0000ff;">if</span><span style="color: #000000;">(){
</span><span style="color: #008080;">461</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">462</span>
<span style="color: #008080;">463</span> <span style="color: #000000;">}
</span><span style="color: #008080;">464</span> <span style="color: #008000;">//</span><span style="color: #008000;">============================== =============
</span><span style="color: #008080;">465</span>
<span style="color: #008080;">466</span> <span style="color: #008000;">//There are several control structures
</span><span style="color: #008080;">467</span> <span style="color: #008000;">//Answer: 1. Sequence, 2. Branch, 3. Loop
</span><span style="color: #008080;">468</span> <span style="color: #008000;">//These three control structures
</span><span style="color: #008080;">469</span> <span style="color: #008000;">//This is true for all languages ​​Return to original position
</span><span style="color: #008080;">470</span>
<span style="color: #008080;">471</span> <span style="color: #008000;">//======================================== ====
</span><span style="color: #008080;">472</span> <span style="color: #008000;">// 1. Order
</span><span style="color: #008080;">473</span> <span style="color: #008000;">// The sequential structure means that the statements are executed in the order you wrote them, from front to back.
</span><span style="color: #008080;">474</span> <span style="color: #008000;">//Easiest to understand </span>
<span style="color: #008080;">475</span> <span style="color: #0000ff;">echo</span> 'a:8 o'clock','<br />'<span style="color: #000000;">;
</span><span style="color: #008080;">476</span> <span style="color: #0000ff;">echo</span> 'b:来YY:88354001','<br />'<span style="color: #000000;">;
</span><span style="color: #008080;">477</span> <span style="color: #0000ff;">echo</span> 'c:I understand','<br />'<span style="color: #000000;">;
</span><span style="color: #008080;">478</span> <span style="color: #0000ff;">echo</span> 'd: I started blogging','<br />'<span style="color: #000000;">;
</span><span style="color: #008080;">479</span>
<span style="color: #008080;">480</span> <span style="color: #008000;">//</span><span style="color: #008000;">================================== ==========
</span><span style="color: #008080;">481</span> <span style="color: #008000;">// 2. Branch <span style="color: #008000;">Return to original position</span>
</span><span style="color: #008080;">482</span> <span style="color: #008000;">//As the name suggests: there are branches, just like a road, after taking 3 forks, it’s time to choose one to go on.</span><span style="color: #008080;">483</span> <span style="color: #008000;">//The simplest, single-way branch
</span><span style="color: #008080;">484</span>
<span style="color: #008080;">485</span> <span style="color: #008000;">//Single branch </span>
<span style="color: #008080;">486</span> <span style="color: #800080;">$age</span> = 18<span style="color: #000000;">;
</span><span style="color: #008080;">487</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$age</span> >= 18<span style="color: #000000;"> ){
</span><span style="color: #008080;">488</span> <span style="color: #0000ff;">echo</span> 'You are an adult'<span style="color: #000000;">;
</span><span style="color: #008080;">489</span> }<span style="color: #008000;">//</span><span style="color: #008000;">The code in this if will either be executed or not
</span><span style="color: #008080;">490</span> <span style="color: #008000;">//The expression in brackets, if true, is executed, otherwise, is not executed </span>
<span style="color: #008080;">491</span> -----------------------------------------------
<span style="color: #008080;">492</span> <span style="color: #008000;">//</span><span style="color: #008000;">Double branch if /else </span>
<span style="color: #008080;">493</span> <span style="color: #800080;">$year</span> = 2012<span style="color: #000000;">;
</span><span style="color: #008080;">494</span> <span style="color: #800080;">$birth</span> = 1987<span style="color: #000000;">;
</span><span style="color: #008080;">495</span> <span style="color: #800080;">$age</span> = <span style="color: #800080;">$year</span> - <span style="color: #800080;">$birth</span>
<span style="color: #008080;">496</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$age</span> > 28<span style="color: #000000;">){
</span><span style="color: #008080;">497</span> <span style="color: #0000ff;">echo</span> 'You are middle-aged and old'<span style="color: #000000;">;
</span><span style="color: #008080;">498</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">499</span> <span style="color: #0000ff;">echo</span> 'You are a promising young man'<span style="color: #000000;">;
</span><span style="color: #008080;">500</span> <span style="color: #000000;">}
</span><span style="color: #008080;">501</span> <span style="color: #008000;">//</span><span style="color: #008000;">if is true, then code segment 1 is executed
</span><span style="color: #008080;">502</span> <span style="color: #008000;">//if is false, then code segment 2 is executed
</span><span style="color: #008080;">503</span> <span style="color: #008000;">//Of the two codes in if else, one and only one will be executed.</span><span style="color: #008080;">504</span> <span style="color: #008000;">//---------------------------------- --------
</span><span style="color: #008080;">505</span> <span style="color: #008000;">//Multiple branches </span>
<span style="color: #008080;">506</span> <span style="color: #800080;">$age</span> = 34<span style="color: #000000;">;
</span><span style="color: #008080;">507</span> <span style="color: #0000ff;">if</span>( <span style="color: #800080;">$age</span> <=16<span style="color: #000000;"> ){
</span><span style="color: #008080;">508</span> <span style="color: #0000ff;">echo</span> 'Middle-aged'<span style="color: #000000;">;
</span><span style="color: #008080;">509</span> } <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$age</span> > 16 && <span style="color: #800080;">$age</span> <= 28 <span style="color: #000000;">){
</span><span style="color: #008080;">510</span> <span style="color: #0000ff;">echo</span> 'elderly'<span style="color: #000000;">;
</span><span style="color: #008080;">511</span> }<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>( <span style="color: #800080;">$age</span> >28 && <span style="color: #800080;">$age</span> <=300 <span style="color: #000000;">){
</span><span style="color: #008080;">512</span> <span style="color: #0000ff;">echo</span> 'Other'<span style="color: #000000;">;
</span><span style="color: #008080;">513</span> <span style="color: #000000;">}
</span><span style="color: #008080;">514</span> ---------------------------------------- ---
<span style="color: #008080;">515</span> <span style="color: #008000;">//</span><span style="color: #008000;">switch branch statement <span style="color: #008000;">Return to original position</span></span>
<span style="color: #008080;">516</span>
<span style="color: #008080;">517</span> <span style="color: #800080;">$day</span> = 2<span style="color: #000000;">;
</span><span style="color: #008080;">518</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$day</span> == 1<span style="color: #000000;">){
</span><span style="color: #008080;">519</span> <span style="color: #0000ff;">echo</span> 'Monday'<span style="color: #000000;">;
</span><span style="color: #008080;">520</span> }<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$day</span> == 2<span style="color: #000000;">){
</span><span style="color: #008080;">521</span> <span style="color: #0000ff;">echo</span> 'Tuesday'<span style="color: #000000;">;
</span><span style="color: #008080;">522</span> }<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$day</span> == 3<span style="color: #000000;">){
</span><span style="color: #008080;">523</span> <span style="color: #0000ff;">echo</span> 'Wednesday'<span style="color: #000000;">;
</span><span style="color: #008080;">524</span> }<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$day</span> == 4<span style="color: #000000;">){
</span><span style="color: #008080;">525</span> <span style="color: #0000ff;">echo</span> 'Thursday'<span style="color: #000000;">;
</span><span style="color: #008080;">526</span> }<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$day</span> == 5<span style="color: #000000;">){
</span><span style="color: #008080;">527</span> <span style="color: #0000ff;">echo</span> 'Friday'<span style="color: #000000;">;
</span><span style="color: #008080;">528</span> }<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$day</span> == 6<span style="color: #000000;">){
</span><span style="color: #008080;">529</span> <span style="color: #0000ff;">echo</span> 'Saturday'<span style="color: #000000;">;
</span><span style="color: #008080;">530</span> }<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$day</span> == 7<span style="color: #000000;">){
</span><span style="color: #008080;">531</span> <span style="color: #0000ff;">echo</span> 'Sunday'<span style="color: #000000;">;
</span><span style="color: #008080;">532</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">533</span> <span style="color: #0000ff;">echo</span> 'Does not exist'<span style="color: #000000;">;
</span><span style="color: #008080;">534</span> <span style="color: #000000;">}
</span><span style="color: #008080;">535</span> <span style="color: #008000;">//</span><span style="color: #008000;">For the value of a variable, there are many possibilities. It is of course possible to use if / if else to compare.</span><span style="color: #008080;">536</span> <span style="color: #008000;">//However, there is a simpler branch structure to replace it</span>
<span style="color: #008080;">537</span>
<span style="color: #008080;">538</span>
<span style="color: #008080;">539</span> <span style="color: #800080;">$day</span> =2<span style="color: #000000;">;
</span><span style="color: #008080;">540</span> <span style="color: #0000ff;">switch</span>(<span style="color: #800080;">$day</span><span style="color: #000000;">){
</span><span style="color: #008080;">541</span> <span style="color: #0000ff;">case</span> 1:
<span style="color: #008080;">542</span> <span style="color: #0000ff;">echo</span> 'Monday'<span style="color: #000000;">;
</span><span style="color: #008080;">543</span> <span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #008080;">544</span> <span style="color: #0000ff;">case</span> 2:
<span style="color: #008080;">545</span> <span style="color: #0000ff;">echo</span> 'Tuesday'<span style="color: #000000;">;
</span><span style="color: #008080;">546</span> <span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #008080;">547</span> <span style="color: #0000ff;">case</span> 3:
<span style="color: #008080;">548</span> <span style="color: #0000ff;">echo</span> 'Wednesday'<span style="co"></span>
Copy after login
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!