Maison >php教程 >php手册 >php中的运算符、控制结构

php中的运算符、控制结构

WBOY
WBOYoriginal
2016-07-09 09:08:06770parcourir

 php中的运算符

1. 赋值运算符
2. 算术运算符
3. 比较运算符
4. 字符串运算符
5. 递增运算符
6. 逻辑运算符(异或运算 xor )
7. 三元运算符
8. 位运算符 (移位运算)
10 . echo中','和'.' 的区别
9. 类型转换
1). 布尔值  --字符串
2). 字符串 -- 数字的转换
3). 字符串-- 布尔值的转换
4). 数字 - - 布尔值的转换
5). 数字 --字符串的转换

控制结构

1. if 单路分支

2. if /else 双路分支,双路分支中if/else 中的2 个代码段,必有且只有一段被执行
if/else if/else if……/else 多路分支

3. switch case ,用来比较判断的变量的多种可能值时便于用。

4. while (表达式){
代码段}

5. do{
代码段;}while(表达式);

6. for(语句1;语句2;语句3){
代码段;}

语句1,先执行 且只执行1次
然后 语句2判断,如为真,则执行 代码段; ,然后执行  语句3;
再语句2判断……循环
直到语句2为假,for结束

代码展示

复制代码
<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. 算术运算                                    </span>返回原处
<span style="color: #008080;">  4</span> <span style="color: #008000;">//
</span><span style="color: #008080;">  5</span> <span style="color: #008000;">//php程序如何进行加减乘除
</span><span style="color: #008080;">  6</span> <span style="color: #008000;">//依次是+ - </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;">这语句话的含义是$a+$b计算结果赋给$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;">计算$a * $b的结果 赋给$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;">计算$b/$a的结果,赋给$c 
</span><span style="color: #008080;"> 17</span> 
<span style="color: #008080;"> 18</span> <span style="color: #008000;">//如果是在C语言中,$b,$a,都是整型,因此计算出来的$c也是整型
</span><span style="color: #008080;"> 19</span> <span style="color: #008000;">//在php中,变量的值可以随意改变,而且变量的类型也可以改变。
</span><span style="color: #008080;"> 20</span> <span style="color: #008000;">//
</span><span style="color: #008080;"> 21</span> <span style="color: #008000;">//比如:变量$c是整型,可以突然为之赋一个浮点型值。
</span><span style="color: #008080;"> 22</span> <span style="color: #008000;">//因此php称为动态语言。
</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;">求余就是 取模  运算                        返回原处
</span><span style="color: #008080;"> 29</span> <span style="color: #000000;">在计算机里计算除法的时候,算出来2个结果
</span><span style="color: #008080;"> 30</span> 
<span style="color: #008080;"> 31</span> <span style="color: #000000;">商和余数
</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;"> 得1 余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;">小测试,判断一个数是偶数,还是奇数?
</span><span style="color: #008080;"> 41</span> <span style="color: #008000;">//可以对2 取模,余数是1,则为奇数,如果余数为0,则为奇数</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>,'是奇数'<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>,'是偶数'<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;">上初中了,有了负数的概念</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;">注意:在取模运算的时,
</span><span style="color: #008080;"> 62</span> <span style="color: #008000;">//运算的正负结果,取决于被除数的符号
</span><span style="color: #008080;"> 63</span> <span style="color: #008000;">//结果的符号,和被除数的符号一致
</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;"> // 比较运算符                                    返回原处
</span><span style="color: #008080;"> 67</span> <span style="color: #008000;">// > ,= ==,!= ,===(不仅值相等,而且类型也相等),!==</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大于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不大于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;">  >=  意味着,$a比$b大,或者  $a 等于$b 有一个为真都行,即条件满足 </span>
<span style="color: #008080;"> 77</span> <span style="color: #0000ff;">echo</span> '3大于等于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并不大于等于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>,'全等于',<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>,'不全等于',<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>,'等于',<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>,'不等于',<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;">在不同的类型比较时
</span><span style="color: #008080;">101</span> <span style="color: #008000;">//php程序本身
</span><span style="color: #008080;">102</span> <span style="color: #008000;">//
</span><span style="color: #008080;">103</span> <span style="color: #008000;">//会把变量 转换成一致的类型,然后再去比较
</span><span style="color: #008080;">104</span> <span style="color: #008000;">//
</span><span style="color: #008080;">105</span> <span style="color: #008000;">//着重记以下转换                 返回原处
</span><span style="color: #008080;">106</span> <span style="color: #008000;">//1. 布尔值  --字符串
</span><span style="color: #008080;">107</span> <span style="color: #008000;">//2. 字符串 -- 数字的转换
</span><span style="color: #008080;">108</span> <span style="color: #008000;">//3. 字符串-- 布尔值的转换
</span><span style="color: #008080;">109</span> <span style="color: #008000;">//4. 数字 - - 布尔值的转换
</span><span style="color: #008080;">110</span> <span style="color: #008000;">//5. 数字 --字符串的转换
</span><span style="color: #008080;">111</span> <span style="color: #008000;">//
</span><span style="color: #008080;">112</span> <span style="color: #008000;">//以5 ==true 比较为例
</span><span style="color: #008080;">113</span> <span style="color: #008000;">//的把 整型 ,转成布尔型
</span><span style="color: #008080;">114</span> <span style="color: #008000;">//
</span><span style="color: #008080;">115</span> <span style="color: #008000;">//其实有的语言中,根本就没有布尔型,都是拿1,0充当true / false 
</span><span style="color: #008080;">116</span> <span style="color: #008000;">//0   /   0.0    这两个转换为布尔类型为false 
</span><span style="color: #008080;">117</span> <span style="color: #008000;">//其他都是true ,负数也是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. 布尔值  --字符串</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. 字符串 --数字的转换
</span><span style="color: #008080;">129</span> <span style="color: #008000;">//从字符串左端开始找数字,直到第一个非数字结束,取出来的这一部分,即是转换后的值
</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;">运算结果是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. 字符串 --布尔的转换
</span><span style="color: #008080;">139</span> <span style="color: #008000;">//''字符串,'0',这种转换成 false
</span><span style="color: #008080;">140</span> <span style="color: #008000;">//其他的一概是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> '空字符串等于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> '空字符串不等于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> '字符串0等于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> '字符串0不等于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. 字符串 --布尔的转换
</span><span style="color: #008080;">160</span> <span style="color: #008000;">// 只有整型的0 ,和浮点的0.0 ,转换false
</span><span style="color: #008080;">161</span> <span style="color: #008000;">//其他 ,甚至负数  ,也转换成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. 数字 --字符串的转换
</span><span style="color: #008080;">167</span> <span style="color: #008000;">//直接是字面本身    </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;">//字符串                                                     返回原处
</span><span style="color: #008080;">175</span> 
<span style="color: #008080;">176</span> <span style="color: #008000;">//在php中字符串,有一句运算,就是拼接  (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;"> 新手的疑问   </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;">这一行是$str1,$str2,$str3在内存中拼接运算
</span><span style="color: #008080;">189</span> <span style="color: #008000;">//运算出一个'hello world'一个大字符串,
</span><span style="color: #008080;">190</span> <span style="color: #008000;">//然后echo 之,得到判断的结果  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 是个语句,负责输出用的
</span><span style="color: #008080;">195</span> <span style="color: #008000;">//可以通过','分割   一次输出多个变量或值来
</span><span style="color: #008080;">196</span> <span style="color: #008000;">//
</span><span style="color: #008080;">197</span> <span style="color: #008000;">//综上:
</span><span style="color: #008080;">198</span> <span style="color: #008000;">//用' . '是先拼接,再整个输出
</span><span style="color: #008080;">199</span> <span style="color: #008000;">//用' , ' 是挨个儿把3个变量输出。 
</span><span style="color: #008080;">200</span> <span style="color: #008000;">//
</span><span style="color: #008080;">201</span> <span style="color: #008000;">//面试时,会碰到 会问法:
</span><span style="color: #008080;">202</span> <span style="color: #008000;">//用' . ',用' , '那个输出的快,效率高
</span><span style="color: #008080;">203</span> <span style="color: #008000;">//
</span><span style="color: #008080;">204</span> <span style="color: #008000;">//答:当然是 ',' 输出快,效率高</span> 
<span style="color: #008080;">206</span> <span style="color: #008000;">//==========================================
</span><span style="color: #008080;">207</span> <span style="color: #008000;">//赋值运算符,就是 =  ,作用是,把‘=’右边的值,赋给左边变量       <span style="color: #008000;">返回原处</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;">//运算符的运算结果 
</span><span style="color: #008080;">214</span> <span style="color: #008000;">//
</span><span style="color: #008080;">215</span> <span style="color: #008000;">//大家想:既然是运算,运算完之后,必须得有一个结果   ---结果值 
</span><span style="color: #008080;">216</span> <span style="color: #008000;">//3 + 2;  运算结果5
</span><span style="color: #008080;">217</span> <span style="color: #008000;">//
</span><span style="color: #008080;">218</span> <span style="color: #008000;">//1. 比较运算符                                               返回原处
</span><span style="color: #008080;">219</span> <span style="color: #008000;">//注意 :比较去处符返回结果,,只有2个可能   true/false  
</span><span style="color: #008080;">220</span> <span style="color: #008000;">//
</span><span style="color: #008080;">221</span> <span style="color: #008000;">//以$a == $b  为例,,要么等,要么不等
</span><span style="color: #008080;">222</span> <span style="color: #008000;">//要么是真,要么是假
</span><span style="color: #008080;">223</span> <span style="color: #008000;">//2 ==3;运算结果是: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. 字符串运算符                                              返回原处
</span><span style="color: #008080;">227</span> <span style="color: #008000;">//字符串运算符
</span><span style="color: #008080;">228</span> <span style="color: #008000;">//$a.$b 
</span><span style="color: #008080;">229</span> <span style="color: #008000;">//返回结果:拼接后的结果 
</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;">//赋值运算符,返回值是多少呢?</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;">赋值运算长符的运算结果,就是 =右边的值 </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;">过程是这样的, $b = 88先进行,赋值后返回88
</span><span style="color: #008080;">242</span> <span style="color: #008000;">//返回的88,再赋给$c 
</span><span style="color: #008080;">243</span> <span style="color: #008000;">//同理 ……
</span><span style="color: #008080;">244</span> <span style="color: #008000;">//最终$a也赋值为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;">//两个难点;
</span><span style="color: #008080;">248</span> <span style="color: #008000;">//1. 比较运算时,有一个类型转换的问题
</span><span style="color: #008080;">249</span> <span style="color: #008000;">//2. 凡运算,必有返回值 
</span><span style="color: #008080;">250</span> <span style="color: #008000;">//比较运算返回布尔值
</span><span style="color: #008080;">251</span> <span style="color: #008000;">//
</span><span style="color: #008080;">252</span> <span style="color: #008000;">//赋值运算,返回赋的值本身 
</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. 逻辑运算符                                       返回原处
</span><span style="color: #008080;">256</span> <span style="color: #008000;">//
</span><span style="color: #008080;">257</span> <span style="color: #008000;">//命题:
</span><span style="color: #008080;">258</span> <span style="color: #008000;">//真命题,假命题
</span><span style="color: #008080;">259</span> <span style="color: #008000;">//
</span><span style="color: #008080;">260</span> <span style="color: #008000;">//逻辑运算符:逻辑或 ||  or 
</span><span style="color: #008080;">261</span> <span style="color: #008000;">//逻辑与   &&  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'如果这样写,谁先给结合 ,牵涉到运算优先级的问题
</span><span style="color: #008080;">267</span> <span style="color: #008000;">//我们推荐,在多个量进行去处的时候,需要先在一块的单元,用()包起来。如:$res = $house && ($car == 'BMW')
</span><span style="color: #008080;">268</span> <span style="color: #008000;">//这样可以 使程序易读,易理解 ,不容易产生歧义 </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> '还是可以嫁'<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> '再等两年'<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;">逻辑运算的优先级
</span><span style="color: #008080;">276</span> && 的优先级 比 ||<span style="color: #000000;">高
</span><span style="color: #008080;">277</span> <span style="color: #000000;">但是在开发中,不推荐写在一起,应用()包起来
</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;">//逻辑反
</span><span style="color: #008080;">289</span> <span style="color: #008000;">//一个命题如果是真命题,则其反命题,是假命题
</span><span style="color: #008080;">290</span> <span style="color: #008000;">//用!表示 </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;">//异或运算  xor                                         返回原处
</span><span style="color: #008080;">294</span> <span style="color: #008000;">//异或是指,两个条件必须‘一个真’,‘一个假’,才为真</span>
<span style="color: #008080;">295</span> 
<span style="color: #008080;">296</span> <span style="color: #800080;">$li</span> = '男'<span style="color: #000000;">;
</span><span style="color: #008080;">297</span> <span style="color: #800080;">$mei</span> = '女'<span style="color: #000000;">;
</span><span style="color: #008080;">298</span> <span style="color: #008000;">//</span><span style="color: #008000;">这个异或表达的是,必须一个为男,</span>
<span style="color: #008080;">299</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$li</span> == '男') xor (<span style="color: #800080;">$mei</span> == '男'<span style="color: #000000;">){
</span><span style="color: #008080;">300</span>     <span style="color: #0000ff;">echo</span> '可以结婚'<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> '法律禁止,请去北欧'<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;">//三元运算符                                              返回原处
</span><span style="color: #008080;">308</span> <span style="color: #008000;">//
</span><span style="color: #008080;">309</span> <span style="color: #008000;">//条件?返回值1:返回值2
</span><span style="color: #008080;">310</span> <span style="color: #008000;">//如果条件为真,则返回值1,否则返回值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;">//比如,在网站中判断一个人有没有登陆
</span><span style="color: #008080;">314</span> <span style="color: #008000;">//如果登陆,用户名不为空,则显示其用户名
</span><span style="color: #008080;">315</span> <span style="color: #008000;">//如果没登陆,用户名空,则显示游客。。
</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 '游客';
</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:'游客';
</span><span style="color: #008080;">326</span> 
<span style="color: #008080;">327</span> <span style="color: #008000;">//程序如果没有三元运算符,可以用什么代替?
</span><span style="color: #008080;">328</span> <span style="color: #008000;">//
</span><span style="color: #008080;">329</span> <span style="color: #008000;">//可以用if/else代替
</span><span style="color: #008080;">330</span> <span style="color: #008000;">//三元中是一个简化的写法 
</span><span style="color: #008080;">331</span> <span style="color: #008000;">//而且效率没有 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;">//递增,递减运算符                                          返回原处
</span><span style="color: #008080;">336</span> <span style="color: #008000;">//
</span><span style="color: #008080;">337</span> <span style="color: #008000;">//就是语言中没有递增,递减,也不会影响我们写程序。
</span><span style="color: #008080;">338</span> <span style="color: #008000;">//也是一些场景的简化写法 
</span><span style="color: #008080;">339</span> 
<span style="color: #008080;">340</span> <span style="color: #008000;">//分析,$b + 1是一个算术运算,算出的结果是3,这个3是一个‘算出结果’,和$b没有关系
</span><span style="color: #008080;">341</span> <span style="color: #008000;">//这个算出结果,值给了$a
</span><span style="color: #008080;">342</span> <span style="color: #008000;">//因此,$a为3,$b为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++,这是一个递增运算,去处则必有去处结果,
</span><span style="color: #008080;">348</span> <span style="color: #008000;">//问题:  $b++的运算结果是什么?
</span><span style="color: #008080;">349</span> <span style="color: #008000;">//答:$b++,运算结果是$b本身
</span><span style="color: #008080;">350</span> <span style="color: #008000;">//然后再把$b的值+1
</span><span style="color: #008080;">351</span> <span style="color: #008000;">//就相当于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;">位运算符,说白了,就是,二进制运算符                              返回原处
</span><span style="color: #008080;">359</span> <span style="color: #008000;">//人有10个指头,用10进位,便于计算
</span><span style="color: #008080;">360</span> <span style="color: #008000;">//
</span><span style="color: #008080;">361</span> <span style="color: #008000;">//玛雅人,不穿鞋,连脚趾也算上,20进位
</span><span style="color: #008080;">362</span> <span style="color: #008000;">//进位是多少没关系,关键是方便我们我们使用
</span><span style="color: #008080;">363</span> <span style="color: #008000;">// 
</span><span style="color: #008080;">364</span> <span style="color: #008000;">//对于计算机来说,所有的位,都 只有两种状态 0  1
</span><span style="color: #008080;">365</span> <span style="color: #008000;">//因为百进位上 ,1代表100
</span><span style="color: #008080;">366</span> <span style="color: #008000;">//10进位上,1代表10
</span><span style="color: #008080;">367</span> <span style="color: #008000;">//个位,1代表1
</span><span style="color: #008080;">368</span> <span style="color: #008000;">//
</span><span style="color: #008080;">369</span> <span style="color: #008000;">//我们知道计算机里的信息,都是以0,1来存储的
</span><span style="color: #008080;">370</span> <span style="color: #008000;">//一个可以存储0/1单元,称为位bit
</span><span style="color: #008080;">371</span> <span style="color: #008000;">//
</span><span style="color: #008080;">372</span> <span style="color: #008000;">//8个位组成一个字节,Byte
</span><span style="color: #008080;">373</span> <span style="color: #008000;">//字节是计算机里最基本的单位。
</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;">但是计算机里,最基本的单位就是字节,有8个位,只用了3个怎么办?
</span><span style="color: #008080;">381</span> <span style="color: #008000;">答:把用的3个或4个位,写在字节的右边
</span><span style="color: #008080;">382</span> <span style="color: #008000;">左边的这些位,用0补上。
</span><span style="color: #008080;">383</span> <span style="color: #008000;">因此,计算机里:
</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;">按位运算符
</span><span style="color: #008080;">392</span> <span style="color: #008000;">&按位且  ,把2个二进制值齐,把1看成true ,把0看成false,然后每个位上都做 并且 的运算。
</span><span style="color: #008080;">393</span> <span style="color: #008000;">|按位或 , 把2个二进制值齐,把1看成true ,把0看成false,然后每个位上都做  或者  的运算。
</span><span style="color: #008080;">394</span> 
<span style="color: #008080;">395</span> <span style="color: #008000;">^按位异或    把2个二进制值齐,把1看成true ,把0看成false,然后每个位都不一样时做 异或  的运算。
</span><span style="color: #008080;">396</span> 
<span style="color: #008080;">397</span> 
<span style="color: #008080;">398</span> <span style="color: #008000;">提醒大家:计算机是按位算,我们操作,往往直接10进制
</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;">按位求反</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;">计算机里表示负数,是用补码的规则来计算的
</span><span style="color: #008080;">408</span> <span style="color: #008000;">//具体这样来计算负数,假设有8位
</span><span style="color: #008080;">409</span> <span style="color: #008000;">//最高位是1,得出符号是负
</span><span style="color: #008080;">410</span> <span style="color: #008000;">//
</span><span style="color: #008080;">411</span> <span style="color: #008000;">//开始补码规则 
</span><span style="color: #008080;">412</span> <span style="color: #008000;">//计算除符号位之外,其他7位的绝对值
</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;">//然后把   绝对值-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;">//因此:
</span><span style="color: #008080;">417</span> <span style="color: #008000;">//1000 0001 ---不要想当然是 -1
</span><span style="color: #008080;">418</span> <span style="color: #008000;">//而是  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;">为什么有人问:-128    +127
</span><span style="color: #008080;">426</span> <span style="color: #008000;">//
</span><span style="color: #008080;">427</span> <span style="color: #008000;">//直观来理解
</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;">//位运算,掌握更好,掌握的不熟悉也不要纠结
</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;">//移位运算                                          返回原处</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 //<span style="color: #008000;">左移
</span><span style="color: #008080;">437</span> <span style="color: #008000;">//相当于乘2的效果</span>
<span style="color: #008080;">438</span>   0000 1010; <span style="color: #008000;">//</span><span style="color: #008000;">得到的结果是10  </span>
<span style="color: #008080;">439</span> 5 >> 1; <span style="color: #008000;">//</span><span style="color: #008000;">右移</span>
<span style="color: #008080;">440</span> 0000 0010
<span style="color: #008080;">441</span> <span style="color: #008000;">//</span><span style="color: #008000;">相当于除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;">//之前学习了‘量’---变量,常量 
</span><span style="color: #008080;">446</span> <span style="color: #008000;">//量与量之间,可以运算,运算符
</span><span style="color: #008080;">447</span> <span style="color: #008000;">//
</span><span style="color: #008080;">448</span> <span style="color: #008000;">//如果只有量与量与的运算,那么我们学的知识只够做一个计算器
</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;">//想写程序:还得有控制结构
</span><span style="color: #008080;">452</span> <span style="color: #008000;">//
</span><span style="color: #008080;">453</span> <span style="color: #008000;">//控制结构,就是条件与逻辑,控制代码的执行顺序
</span><span style="color: #008080;">454</span> <span style="color: #008000;">//
</span><span style="color: #008080;">455</span> <span style="color: #008000;">//程序员 是用php  --- 培训 --- 计算机 
</span><span style="color: #008080;">456</span> <span style="color: #008000;">//如果 --- if
</span><span style="color: #008080;">457</span> <span style="color: #008000;">//否则如果 ---else if
</span><span style="color: #008080;">458</span> <span style="color: #008000;">//否则 ---- 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;">//有几种控制结构 
</span><span style="color: #008080;">467</span> <span style="color: #008000;">//答 :  1. 顺序   、  2. 分支  、 3. 循环 
</span><span style="color: #008080;">468</span> <span style="color: #008000;">//这个3种控制结构 
</span><span style="color: #008080;">469</span> <span style="color: #008000;">//所有语言都这样                                 返回原处
</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. 顺序
</span><span style="color: #008080;">473</span> <span style="color: #008000;">// 顺序结构就是语句按照你书写的顺序,从前到后顺序执行。
</span><span style="color: #008080;">474</span> <span style="color: #008000;">// 最容易理解 </span>
<span style="color: #008080;">475</span> <span style="color: #0000ff;">echo</span> 'a:8点了','<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:我听懂了','<br>'<span style="color: #000000;">;
</span><span style="color: #008080;">478</span> <span style="color: #0000ff;">echo</span> 'd:我开始写博客了','<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. 分支                                                        <span style="color: #008000;">返回原处</span>
</span><span style="color: #008080;">482</span> <span style="color: #008000;">//顾名思义:有分支 ,就像一条大路,走了3岔口,该选择一个走下去了。
</span><span style="color: #008080;">483</span> <span style="color: #008000;">//最简单的,单路分支 
</span><span style="color: #008080;">484</span> 
<span style="color: #008080;">485</span> <span style="color: #008000;">//单路分支 </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> '你已成年'<span style="color: #000000;">;
</span><span style="color: #008080;">489</span> }<span style="color: #008000;">//</span><span style="color: #008000;">这个if中的代码,要么执行,要么不执行
</span><span style="color: #008080;">490</span> <span style="color: #008000;">//括号中的表达式,如果为真,则执行,否则,不执行</span>
<span style="color: #008080;">491</span> -------------------------------------------
<span style="color: #008080;">492</span> <span style="color: #008000;">//</span><span style="color: #008000;">双路分支  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> '你是中老年'<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> '你是有为青年'<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为真,则代码段1执行
</span><span style="color: #008080;">502</span> <span style="color: #008000;">//if为假,则代码段2执行
</span><span style="color: #008080;">503</span> <span style="color: #008000;">//if else 中的两个代码,有且只有一个执行。
</span><span style="color: #008080;">504</span> <span style="color: #008000;">//-------------------------------------------
</span><span style="color: #008080;">505</span> <span style="color: #008000;">//多路分支 </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>  ){
<span style="color: #008080;">508</span>     <span style="color: #0000ff;">echo</span> '中年'<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> ){
<span style="color: #008080;">510</span>     <span style="color: #0000ff;">echo</span> '老年'<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> ){
<span style="color: #008080;">512</span>     <span style="color: #0000ff;">echo</span> '其他'<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 分支语句                                 <span style="color: #008000;">返回原处</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>  '星期一'<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> '星期二'<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> '星期三'<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> '星期四'<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> '星期五'<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> '星期六'<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> '星期日'<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> '不存在'<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;">对于一个变量的值,有多种可能性,用if / if else 来比较,当然是可以的。
</span><span style="color: #008080;">536</span> <span style="color: #008000;">//但是,有一种,更简单的分支结构来替换他</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> '星期一'<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> '星期二'<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> '星期三'<span style="co"></span>
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn