Rumah > pembangunan bahagian belakang > tutorial php > Java运算符 php中的运算符、控制结构

Java运算符 php中的运算符、控制结构

WBOY
Lepaskan: 2016-07-28 08:27:43
asal
1025 orang telah melayarinya

 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结束
代码展示

 php中的运算符、控制结构

<span>  1</span><span>//</span><span>==========================================
</span><span>  2</span><span>  3</span><span>//2. 算术运算                                    </span>返回原处
<span>  4</span><span>//
</span><span>  5</span><span>//php程序如何进行加减乘除
</span><span>  6</span><span>//依次是+ - </span><span>  7</span><span>  8</span><span>  9</span><span>$a</span> = 3<span>;
</span><span> 10</span><span>$b</span> =5<span>;
</span><span> 11</span><span>$c</span> = <span>$a</span> + <span>$b</span>;<span>//</span><span>这语句话的含义是$a+$b计算结果赋给$c</span><span> 12</span><span>echo</span><span>$c</span>,'<br>'<span>;
</span><span> 13</span><span>echo</span><span>$b</span>-<span>$a</span>,'<br>'<span>;
</span><span> 14</span><span> 15</span><span>echo</span><span>$a</span> * <span>$b</span>,'<br>';<span>//</span><span>计算$a * $b的结果 赋给$c </span><span> 16</span><span>echo</span><span>$b</span>/<span>$a</span>,'<br>';<span>//</span><span>计算$b/$a的结果,赋给$c 
</span><span> 17</span><span> 18</span><span>//如果是在C语言中,$b,$a,都是整型,因此计算出来的$c也是整型
</span><span> 19</span><span>//在php中,变量的值可以随意改变,而且变量的类型也可以改变。
</span><span> 20</span><span>//
</span><span> 21</span><span>//比如:变量$c是整型,可以突然为之赋一个浮点型值。
</span><span> 22</span><span>//因此php称为动态语言。
</span><span> 23</span><span> 24</span><span> 25</span><span> 26</span><span> 27</span><span>//-----------------------------------------------------</span><span> 28</span><span>求余就是 取模  运算                        返回原处
</span><span> 29</span><span>在计算机里计算除法的时候,算出来2个结果
</span><span> 30</span><span> 31</span><span>商和余数
</span><span> 32</span><span> 33</span> */
<span> 34</span><span>$a</span> = 3<span>;
</span><span> 35</span><span>$b</span> = 5<span>;
</span><span> 36</span><span>$c</span> = <span>$b</span>%<span>$a</span>;   <span>//</span><span> 得1 余2</span><span> 37</span><span>echo</span><span>$c</span>,'<br>'<span>;
</span><span> 38</span><span> 39</span><span> 40</span><span>//</span><span>小测试,判断一个数是偶数,还是奇数?
</span><span> 41</span><span>//可以对2 取模,余数是1,则为奇数,如果余数为0,则为奇数</span><span> 42</span><span> 43</span><span>$d</span> = 3<span>;
</span><span> 44</span><span>$res</span> = <span>$d</span> % 2<span>;
</span><span> 45</span><span>if</span>(<span>$res</span> == 1<span>){
</span><span> 46</span><span>echo</span><span>$d</span>,'是奇数'<span>;
</span><span> 47</span><span>}
</span><span> 48</span><span>if</span>(<span>$res</span> == 0<span>){
</span><span> 49</span><span>echo</span><span>$d</span>,'是偶数'<span>;
</span><span> 50</span><span>}
</span><span> 51</span><span> 52</span><span>//</span><span>上初中了,有了负数的概念</span><span> 53</span><span> 54</span><span>$b</span> = -5<span>;
</span><span> 55</span><span>$a</span> = 3<span>;
</span><span> 56</span><span>echo</span><span>$b</span>%<span>$a</span><span>;
</span><span> 57</span><span>$b</span> = -5<span>;
</span><span> 58</span><span>$a</span> = -3<span>;
</span><span> 59</span><span>echo</span><span>$b</span>%<span>$a</span><span>;
</span><span> 60</span><span> 61</span><span>//</span><span>注意:在取模运算的时,
</span><span> 62</span><span>//运算的正负结果,取决于被除数的符号
</span><span> 63</span><span>//结果的符号,和被除数的符号一致
</span><span> 64</span><span>//================================================ 
</span><span> 65</span><span> 66</span><span> // 比较运算符                                    返回原处
</span><span> 67</span><span>// > ,= ==,!= ,===(不仅值相等,而且类型也相等),!==</span><span> 68</span><span>$a</span> = 3<span>;
</span><span> 69</span><span>$b</span> = 2<span>; 
</span><span> 70</span><span>if</span>(3 > 2<span>){
</span><span> 71</span><span>echo</span> '3大于2','<br>'<span>;
</span><span> 72</span> }<span>else</span><span>{
</span><span> 73</span><span>echo</span> '3不大于2','<br>'<span>;
</span><span> 74</span><span>}
</span><span> 75</span><span> 76</span><span>if</span>(<span>$a</span> >= <span>$b</span>){    <span>//</span><span>  >=  意味着,$a比$b大,或者  $a 等于$b 有一个为真都行,即条件满足 </span><span> 77</span><span>echo</span> '3大于等于2','<br>'<span>;
</span><span> 78</span> }<span>else</span><span>{
</span><span> 79</span><span>echo</span> '3并不大于等于2','<br>'<span>;
</span><span> 80</span><span>}
</span><span> 81</span><span>//</span><span>-----------------------------------------------</span><span> 82</span><span> 83</span><span>$a</span> = 3<span>;
</span><span> 84</span><span>$b</span> = '3'<span>; 
</span><span> 85</span><span>if</span>(<span>$a</span> === <span>$b</span><span>){   
</span><span> 86</span><span>echo</span><span>$a</span>,'全等于',<span>$b</span>,'<br>'<span>;
</span><span> 87</span> }<span>else</span><span>{
</span><span> 88</span><span>echo</span><span>$a</span>,'不全等于',<span>$b</span>,'<br>'<span>;
</span><span> 89</span><span>}
</span><span> 90</span><span> 91</span><span>//</span><span>==========================================</span><span> 92</span><span> 93</span><span>$a</span> = 3<span>;
</span><span> 94</span><span>$b</span> = <span>true</span><span>; 
</span><span> 95</span><span>if</span>(<span>$a</span> == <span>$b</span><span>){    
</span><span> 96</span><span>echo</span><span>$a</span>,'等于',<span>$b</span>,'<br>'<span>;
</span><span> 97</span> }<span>else</span><span>{
</span><span> 98</span><span>echo</span><span>$a</span>,'不等于',<span>$b</span>,'<br>'<span>;
</span><span> 99</span><span>}
</span><span>100</span><span>//</span><span>在不同的类型比较时
</span><span>101</span><span>//php程序本身
</span><span>102</span><span>//
</span><span>103</span><span>//会把变量 转换成一致的类型,然后再去比较
</span><span>104</span><span>//
</span><span>105</span><span>//着重记以下转换                 返回原处
</span><span>106</span><span>//1. 布尔值  --字符串
</span><span>107</span><span>//2. 字符串 -- 数字的转换
</span><span>108</span><span>//3. 字符串-- 布尔值的转换
</span><span>109</span><span>//4. 数字 - - 布尔值的转换
</span><span>110</span><span>//5. 数字 --字符串的转换
</span><span>111</span><span>//
</span><span>112</span><span>//以5 ==true 比较为例
</span><span>113</span><span>//的把 整型 ,转成布尔型
</span><span>114</span><span>//
</span><span>115</span><span>//其实有的语言中,根本就没有布尔型,都是拿1,0充当true / false 
</span><span>116</span><span>//0   /   0.0    这两个转换为布尔类型为false 
</span><span>117</span><span>//其他都是true ,负数也是true 
</span><span>118</span><span>119</span><span>120</span><span>121</span><span>122</span><span>//-------------------------------------------
</span><span>123</span><span>//1. 布尔值  --字符串</span><span>124</span><span>true</span> ---'1'
<span>125</span><span>false</span>--- ''
<span>126</span><span>127</span><span>//</span><span>-------------------------------------------
</span><span>128</span><span>//1. 字符串 --数字的转换
</span><span>129</span><span>//从字符串左端开始找数字,直到第一个非数字结束,取出来的这一部分,即是转换后的值
</span><span>130</span><span>//'3q'----3</span><span>131</span> '123asdf'---123
<span>132</span> 'sdfds789'----0
<span>133</span><span>134</span><span>$num</span> = 'sdfds789'<span>;
</span><span>135</span><span>echo</span><span>$num</span> + 0; <span>//</span><span>运算结果是0 
</span><span>136</span><span>137</span><span>//-------------------------------------------
</span><span>138</span><span>//3. 字符串 --布尔的转换
</span><span>139</span><span>//''字符串,'0',这种转换成 false
</span><span>140</span><span>//其他的一概是true</span><span>141</span><span>142</span><span>143</span><span>144</span><span>if</span>(''==<span>false</span><span>){
</span><span>145</span><span>echo</span> '空字符串等于false'<span>;
</span><span>146</span> }<span>else</span><span>{
</span><span>147</span><span>echo</span> '空字符串不等于false'<span>;
</span><span>148</span><span>}
</span><span>149</span><span>150</span><span>echo</span> '<br>'<span>;
</span><span>151</span><span>if</span>('0'==<span>false</span><span>){
</span><span>152</span><span>echo</span> '字符串0等于false'<span>;
</span><span>153</span> }<span>else</span><span>{
</span><span>154</span><span>echo</span> '字符串0不等于false'<span>;
</span><span>155</span><span>}
</span><span>156</span><span>157</span><span>158</span><span>//</span><span>-------------------------------------------
</span><span>159</span><span>//4. 字符串 --布尔的转换
</span><span>160</span><span>// 只有整型的0 ,和浮点的0.0 ,转换false
</span><span>161</span><span>//其他 ,甚至负数  ,也转换成true 
</span><span>162</span><span>163</span><span>164</span><span>165</span><span>//-------------------------------------------
</span><span>166</span><span>//5. 数字 --字符串的转换
</span><span>167</span><span>//直接是字面本身    </span><span>168</span><span>echo</span> '<br>'<span>;
</span><span>169</span><span>echo</span>  'aaa'. 2323<span>;
</span><span>170</span><span>171</span><span>172</span><span>173</span><span>//</span><span>-------------------------------------------
</span><span>174</span><span>//字符串                                                     返回原处
</span><span>175</span><span>176</span><span>//在php中字符串,有一句运算,就是拼接  (echo中','和'.' 的区别)</span><span>177</span><span>178</span><span>$str1</span> = 'hello'<span>;
</span><span>179</span><span>$str2</span>  = ''<span>;
</span><span>180</span><span>181</span><span>$str3</span> = 'world'<span>;
</span><span>182</span><span>echo</span><span>$str1</span>.<span>$str2</span>.<span>$str3</span><span>;  
</span><span>183</span><span>echo</span><span>$str1</span>,<span>$str2</span>,<span>$str3</span><span>;
</span><span>184</span><span>185</span><span>//</span><span> 新手的疑问   </span><span>186</span><span>187</span><span>echo</span><span>$str1</span>.<span>$str2</span>.<span>$str3</span><span>;  
</span><span>188</span><span>//</span><span>这一行是$str1,$str2,$str3在内存中拼接运算
</span><span>189</span><span>//运算出一个'hello world'一个大字符串,
</span><span>190</span><span>//然后echo 之,得到判断的结果  hello world 
</span><span>191</span><span>//==========================================</span><span>192</span><span>193</span><span>echo</span><span>$str1</span>,<span>$str2</span>,<span>$str3</span><span>;
</span><span>194</span><span>//</span><span>echo 是个语句,负责输出用的
</span><span>195</span><span>//可以通过','分割   一次输出多个变量或值来
</span><span>196</span><span>//
</span><span>197</span><span>//综上:
</span><span>198</span><span>//用' . '是先拼接,再整个输出
</span><span>199</span><span>//用' , ' 是挨个儿把3个变量输出。 
</span><span>200</span><span>//
</span><span>201</span><span>//面试时,会碰到 会问法:
</span><span>202</span><span>//用' . ',用' , '那个输出的快,效率高
</span><span>203</span><span>//
</span><span>204</span><span>//答:当然是 ',' 输出快,效率高</span><span>206</span><span>//==========================================
</span><span>207</span><span>//赋值运算符,就是 =  ,作用是,把‘=’右边的值,赋给左边变量       <span>返回原处</span></span><span>208</span><span>$a</span> = 3<span>;
</span><span>209</span><span>$b</span> = <span>$c</span> = 5<span>;
</span><span>210</span><span>211</span><span>212</span><span>//</span><span>==========================================
</span><span>213</span><span>//运算符的运算结果 
</span><span>214</span><span>//
</span><span>215</span><span>//大家想:既然是运算,运算完之后,必须得有一个结果   ---结果值 
</span><span>216</span><span>//3 + 2;  运算结果5
</span><span>217</span><span>//
</span><span>218</span><span>//1. 比较运算符                                               返回原处
</span><span>219</span><span>//注意 :比较去处符返回结果,,只有2个可能   true/false  
</span><span>220</span><span>//
</span><span>221</span><span>//以$a == $b  为例,,要么等,要么不等
</span><span>222</span><span>//要么是真,要么是假
</span><span>223</span><span>//2 ==3;运算结果是:false  
</span><span>224</span><span>225</span><span>//==========================================
</span><span>226</span><span>//2. 字符串运算符                                              返回原处
</span><span>227</span><span>//字符串运算符
</span><span>228</span><span>//$a.$b 
</span><span>229</span><span>//返回结果:拼接后的结果 
</span><span>230</span><span>231</span><span>//==========================================
</span><span>232</span><span>//赋值运算符,返回值是多少呢?</span><span>233</span><span>$a</span> = 3<span>;
</span><span>234</span><span>if</span>(<span>$a</span> = 33<span>){
</span><span>235</span><span>echo</span> 'aa'<span>;
</span><span>236</span> }<span>else</span><span>{
</span><span>237</span><span>echo</span> 'bb'<span>;
</span><span>238</span><span>}
</span><span>239</span><span>//</span><span>赋值运算长符的运算结果,就是 =右边的值 </span><span>240</span><span>$a</span> = <span>$b</span> = <span>$c</span> =88
<span>241</span><span>//</span><span>过程是这样的, $b = 88先进行,赋值后返回88
</span><span>242</span><span>//返回的88,再赋给$c 
</span><span>243</span><span>//同理 ……
</span><span>244</span><span>//最终$a也赋值为88
</span><span>245</span><span>//
</span><span>246</span><span>//
</span><span>247</span><span>//两个难点;
</span><span>248</span><span>//1. 比较运算时,有一个类型转换的问题
</span><span>249</span><span>//2. 凡运算,必有返回值 
</span><span>250</span><span>//比较运算返回布尔值
</span><span>251</span><span>//
</span><span>252</span><span>//赋值运算,返回赋的值本身 
</span><span>253</span><span>254</span><span>//==========================================
</span><span>255</span><span>//6. 逻辑运算符                                       返回原处
</span><span>256</span><span>//
</span><span>257</span><span>//命题:
</span><span>258</span><span>//真命题,假命题
</span><span>259</span><span>//
</span><span>260</span><span>//逻辑运算符:逻辑或 ||  or 
</span><span>261</span><span>//逻辑与   &&  and </span><span>262</span><span>263</span><span>$house</span> = 180<span>;
</span><span>264</span><span>$car</span> = 'BYD'<span>;
</span><span>265</span><span>266</span><span>//</span><span>$res = $house && $car == 'BMW'如果这样写,谁先给结合 ,牵涉到运算优先级的问题
</span><span>267</span><span>//我们推荐,在多个量进行去处的时候,需要先在一块的单元,用()包起来。如:$res = $house && ($car == 'BMW')
</span><span>268</span><span>//这样可以 使程序易读,易理解 ,不容易产生歧义 </span><span>269</span><span>if</span>(<span>$res</span> = <span>$house</span> && <span>$car</span> == 'BMW'<span>){
</span><span>270</span><span>echo</span> '还是可以嫁'<span>;
</span><span>271</span> }<span>else</span><span>{
</span><span>272</span><span>echo</span> '再等两年'<span>;
</span><span>273</span><span>}
</span><span>274</span><span>275</span><span>逻辑运算的优先级
</span><span>276</span> && 的优先级 比 ||<span>高
</span><span>277</span><span>但是在开发中,不推荐写在一起,应用()包起来
</span><span>278</span><span>$a</span> = 3<span>;
</span><span>279</span><span>$b</span> = 5<span>;
</span><span>280</span><span>if</span>((<span>$a</span>==8)||(<span>$b</span>=9<span>)){
</span><span>281</span><span>$a</span>++<span>;
</span><span>282</span><span>$b</span>++<span>;
</span><span>283</span><span>}
</span><span>284</span><span>echo</span><span>$a</span>,'<br>',<span>$b</span><span>;
</span><span>285</span><span>286</span><span>//</span><span>==========================================
</span><span>287</span><span>288</span><span>//逻辑反
</span><span>289</span><span>//一个命题如果是真命题,则其反命题,是假命题
</span><span>290</span><span>//用!表示 </span><span>291</span><span>292</span><span>//==========================================
</span><span>293</span><span>//异或运算  xor                                         返回原处
</span><span>294</span><span>//异或是指,两个条件必须‘一个真’,‘一个假’,才为真</span><span>295</span><span>296</span><span>$li</span> = '男'<span>;
</span><span>297</span><span>$mei</span> = '女'<span>;
</span><span>298</span><span>//</span><span>这个异或表达的是,必须一个为男,</span><span>299</span><span>if</span>(<span>$li</span> == '男') xor (<span>$mei</span> == '男'<span>){
</span><span>300</span><span>echo</span> '可以结婚'<span>;
</span><span>301</span> }<span>else</span><span>{
</span><span>302</span><span>echo</span> '法律禁止,请去北欧'<span>;
</span><span>303</span><span>}
</span><span>304</span><span>305</span><span>306</span><span>//</span><span>==========================================
</span><span>307</span><span>//三元运算符                                              返回原处
</span><span>308</span><span>//
</span><span>309</span><span>//条件?返回值1:返回值2
</span><span>310</span><span>//如果条件为真,则返回值1,否则返回值2
</span><span>311</span><span>//
</span><span>312</span><span>//
</span><span>313</span><span>//比如,在网站中判断一个人有没有登陆
</span><span>314</span><span>//如果登陆,用户名不为空,则显示其用户名
</span><span>315</span><span>//如果没登陆,用户名空,则显示游客。。
</span><span>316</span><span>317</span><span>//$user = 'zhangsan';
</span><span>318</span><span>//if(!=''){
</span><span>319</span><span>//    echo $user;
</span><span>320</span><span>//}else{
</span><span>321</span><span>//    echo '游客';
</span><span>322</span><span>//}
</span><span>323</span><span>//
</span><span>324</span><span>//$user = 'zhangsan';
</span><span>325</span><span>//echo $user?$user:'游客';
</span><span>326</span><span>327</span><span>//程序如果没有三元运算符,可以用什么代替?
</span><span>328</span><span>//
</span><span>329</span><span>//可以用if/else代替
</span><span>330</span><span>//三元中是一个简化的写法 
</span><span>331</span><span>//而且效率没有 if/esle高
</span><span>332</span><span>333</span><span>334</span><span>//==========================================
</span><span>335</span><span>//递增,递减运算符                                          返回原处
</span><span>336</span><span>//
</span><span>337</span><span>//就是语言中没有递增,递减,也不会影响我们写程序。
</span><span>338</span><span>//也是一些场景的简化写法 
</span><span>339</span><span>340</span><span>//分析,$b + 1是一个算术运算,算出的结果是3,这个3是一个‘算出结果’,和$b没有关系
</span><span>341</span><span>//这个算出结果,值给了$a
</span><span>342</span><span>//因此,$a为3,$b为2</span><span>343</span><span>$b</span> = 2<span>;
</span><span>344</span><span>$a</span> = <span>$b</span> +1<span>;
</span><span>345</span><span>echo</span><span>$a</span>,'~',<span>$b</span>,'<br>'<span>;
</span><span>346</span><span>347</span><span>//</span><span>$b++,这是一个递增运算,去处则必有去处结果,
</span><span>348</span><span>//问题:  $b++的运算结果是什么?
</span><span>349</span><span>//答:$b++,运算结果是$b本身
</span><span>350</span><span>//然后再把$b的值+1
</span><span>351</span><span>//就相当于return $b; $b = $b +1;
</span><span>352</span><span>//$b =1;
</span><span>353</span><span>//$a = $b++;
</span><span>354</span><span>//echo $a,'<br>',$b;
</span><span>355</span><span>//
</span><span>356</span><span>//
</span><span>357</span><span>//==========================================</span><span>358</span><span>/*</span><span>位运算符,说白了,就是,二进制运算符                              返回原处
</span><span>359</span><span>//人有10个指头,用10进位,便于计算
</span><span>360</span><span>//
</span><span>361</span><span>//玛雅人,不穿鞋,连脚趾也算上,20进位
</span><span>362</span><span>//进位是多少没关系,关键是方便我们我们使用
</span><span>363</span><span>// 
</span><span>364</span><span>//对于计算机来说,所有的位,都 只有两种状态 0  1
</span><span>365</span><span>//因为百进位上 ,1代表100
</span><span>366</span><span>//10进位上,1代表10
</span><span>367</span><span>//个位,1代表1
</span><span>368</span><span>//
</span><span>369</span><span>//我们知道计算机里的信息,都是以0,1来存储的
</span><span>370</span><span>//一个可以存储0/1单元,称为位bit
</span><span>371</span><span>//
</span><span>372</span><span>//8个位组成一个字节,Byte
</span><span>373</span><span>//字节是计算机里最基本的单位。
</span><span>374</span><span>375</span><span>376</span><span>12 = 8 + 4;
</span><span>377</span><span>   =1*2^3+1*2^2+0*2^1+0*2^0
</span><span>378</span><span>   =1100
</span><span>379</span><span>5=101
</span><span>380</span><span>但是计算机里,最基本的单位就是字节,有8个位,只用了3个怎么办?
</span><span>381</span><span>答:把用的3个或4个位,写在字节的右边
</span><span>382</span><span>左边的这些位,用0补上。
</span><span>383</span><span>因此,计算机里:
</span><span>384</span><span>12=0000 1100
</span><span>385</span><span>5 =0000 0101
</span><span>386</span><span>387</span><span>388</span><span>12 & 5 = 0000 0100 = 4
</span><span>389</span><span>12 | 5 = 0000 1101 = 13
</span><span>390</span><span>12 ^ 5 = 0000 1001 =9
</span><span>391</span><span>按位运算符
</span><span>392</span><span>&按位且  ,把2个二进制值齐,把1看成true ,把0看成false,然后每个位上都做 并且 的运算。
</span><span>393</span><span>|按位或 , 把2个二进制值齐,把1看成true ,把0看成false,然后每个位上都做  或者  的运算。
</span><span>394</span><span>395</span><span>^按位异或    把2个二进制值齐,把1看成true ,把0看成false,然后每个位都不一样时做 异或  的运算。
</span><span>396</span><span>397</span><span>398</span><span>提醒大家:计算机是按位算,我们操作,往往直接10进制
</span><span>399</span><span>*/</span><span>400</span><span>//</span><span>==========================================</span><span>401</span><span>402</span><span>$a</span> = 12<span>;
</span><span>403</span><span>$a</span> = 0000 1100
<span>404</span><span>//</span><span>按位求反</span><span>405</span><span>$a</span> = 1111 0011
<span>406</span><span>echo</span> ~12
<span>407</span><span>//</span><span>计算机里表示负数,是用补码的规则来计算的
</span><span>408</span><span>//具体这样来计算负数,假设有8位
</span><span>409</span><span>//最高位是1,得出符号是负
</span><span>410</span><span>//
</span><span>411</span><span>//开始补码规则 
</span><span>412</span><span>//计算除符号位之外,其他7位的绝对值
</span><span>413</span><span>//(1)111 0011 =115
</span><span>414</span><span>//然后把   绝对值-2^7 = 115 -128 = -13
</span><span>415</span><span>//
</span><span>416</span><span>//因此:
</span><span>417</span><span>//1000 0001 ---不要想当然是 -1
</span><span>418</span><span>//而是  1-2^7 = -127</span><span>419</span> 1111 1111 ---  127-128 = -1
<span>420</span> 1000 0000 ---  0-128 =-128
<span>421</span><span>422</span> 0000 0000 --- 0
<span>423</span> 0111 1111 --- 127
<span>424</span><span>425</span><span>//</span><span>为什么有人问:-128    +127
</span><span>426</span><span>//
</span><span>427</span><span>//直观来理解
</span><span>428</span><span>//-128 ---  0   0 ---  127
</span><span>429</span><span>//位运算,掌握更好,掌握的不熟悉也不要纠结
</span><span>430</span><span>431</span><span>432</span><span>//==========================================
</span><span>433</span><span>//移位运算                                          返回原处</span><span>434</span><span>435</span> 5 = 0000 0101<span>;
</span><span>436</span> 5 //<span>左移
</span><span>437</span><span>//相当于乘2的效果</span><span>438</span>   0000 1010; <span>//</span><span>得到的结果是10  </span><span>439</span> 5 >> 1; <span>//</span><span>右移</span><span>440</span> 0000 0010
<span>441</span><span>//</span><span>相当于除2的效果
</span><span>442</span><span>443</span><span>444</span><span>//==========================================
</span><span>445</span><span>//之前学习了‘量’---变量,常量 
</span><span>446</span><span>//量与量之间,可以运算,运算符
</span><span>447</span><span>//
</span><span>448</span><span>//如果只有量与量与的运算,那么我们学的知识只够做一个计算器
</span><span>449</span><span>450</span><span>//==========================================
</span><span>451</span><span>//想写程序:还得有控制结构
</span><span>452</span><span>//
</span><span>453</span><span>//控制结构,就是条件与逻辑,控制代码的执行顺序
</span><span>454</span><span>//
</span><span>455</span><span>//程序员 是用php  --- 培训 --- 计算机 
</span><span>456</span><span>//如果 --- if
</span><span>457</span><span>//否则如果 ---else if
</span><span>458</span><span>//否则 ---- else   </span><span>459</span><span>$gender</span> = <span>$_GET</span>['gender'<span>];
</span><span>460</span><span>if</span><span>(){
</span><span>461</span> }<span>else</span><span>{
</span><span>462</span><span>463</span><span>}
</span><span>464</span><span>//</span><span>==========================================
</span><span>465</span><span>466</span><span>//有几种控制结构 
</span><span>467</span><span>//答 :  1. 顺序   、  2. 分支  、 3. 循环 
</span><span>468</span><span>//这个3种控制结构 
</span><span>469</span><span>//所有语言都这样                                 返回原处
</span><span>470</span><span>471</span><span>//==========================================
</span><span>472</span><span>// 1. 顺序
</span><span>473</span><span>// 顺序结构就是语句按照你书写的顺序,从前到后顺序执行。
</span><span>474</span><span>// 最容易理解 </span><span>475</span><span>echo</span> 'a:8点了','<br>'<span>;
</span><span>476</span><span>echo</span> 'b:来YY:88354001','<br>'<span>;
</span><span>477</span><span>echo</span> 'c:我听懂了','<br>'<span>;
</span><span>478</span><span>echo</span> 'd:我开始写博客了','<br>'<span>;
</span><span>479</span><span>480</span><span>//</span><span>==========================================
</span><span>481</span><span>// 2. 分支                                                        <span>返回原处</span></span><span>482</span><span>//顾名思义:有分支 ,就像一条大路,走了3岔口,该选择一个走下去了。
</span><span>483</span><span>//最简单的,单路分支 
</span><span>484</span><span>485</span><span>//单路分支 </span><span>486</span><span>$age</span> = 18<span>;
</span><span>487</span><span>if</span>(<span>$age</span> >= 18<span> ){
</span><span>488</span><span>echo</span> '你已成年'<span>;
</span><span>489</span> }<span>//</span><span>这个if中的代码,要么执行,要么不执行
</span><span>490</span><span>//括号中的表达式,如果为真,则执行,否则,不执行</span><span>491</span> -------------------------------------------
<span>492</span><span>//</span><span>双路分支  if /else </span><span>493</span><span>$year</span> = 2012<span>;
</span><span>494</span><span>$birth</span> = 1987<span>;
</span><span>495</span><span>$age</span> = <span>$year</span> - <span>$birth</span><span>496</span><span>if</span>(<span>$age</span> > 28<span>){
</span><span>497</span><span>echo</span> '你是中老年'<span>;
</span><span>498</span> }<span>else</span><span>{
</span><span>499</span><span>echo</span> '你是有为青年'<span>;
</span><span>500</span><span>}
</span><span>501</span><span>//</span><span>if为真,则代码段1执行
</span><span>502</span><span>//if为假,则代码段2执行
</span><span>503</span><span>//if else 中的两个代码,有且只有一个执行。
</span><span>504</span><span>//-------------------------------------------
</span><span>505</span><span>//多路分支 </span><span>506</span><span>$age</span> = 34<span>;
</span><span>507</span><span>if</span>( <span>$age</span>  ){
<span>508</span><span>echo</span> '中年'<span>;
</span><span>509</span> } <span>else</span><span>if</span>(<span>$age</span> > 16 && <span>$age</span> ){
<span>510</span><span>echo</span> '老年'<span>;
</span><span>511</span> }<span>else</span><span>if</span>( <span>$age</span> >28 && <span>$age</span> ){
<span>512</span><span>echo</span> '其他'<span>;
</span><span>513</span><span>}
</span><span>514</span> -------------------------------------------
<span>515</span><span>//</span><span>switch 分支语句                                 <span>返回原处</span></span><span>516</span><span>517</span><span>$day</span> = 2<span>;
</span><span>518</span><span>if</span>(<span>$day</span> == 1<span>){
</span><span>519</span><span>echo</span>  '星期一'<span>;
</span><span>520</span> }<span>else</span><span>if</span>(<span>$day</span> == 2<span>){
</span><span>521</span><span>echo</span> '星期二'<span>;
</span><span>522</span> }<span>else</span><span>if</span>(<span>$day</span> == 3<span>){
</span><span>523</span><span>echo</span> '星期三'<span>;
</span><span>524</span> }<span>else</span><span>if</span>(<span>$day</span> == 4<span>){
</span><span>525</span><span>echo</span> '星期四'<span>;
</span><span>526</span> }<span>else</span><span>if</span>(<span>$day</span> == 5<span>){
</span><span>527</span><span>echo</span> '星期五'<span>;
</span><span>528</span> }<span>else</span><span>if</span>(<span>$day</span> == 6<span>){
</span><span>529</span><span>echo</span> '星期六'<span>;
</span><span>530</span> }<span>else</span><span>if</span>(<span>$day</span> == 7<span>){
</span><span>531</span><span>echo</span> '星期日'<span>;
</span><span>532</span> }<span>else</span><span>{
</span><span>533</span><span>echo</span> '不存在'<span>;
</span><span>534</span><span>}
</span><span>535</span><span>//</span><span>对于一个变量的值,有多种可能性,用if / if else 来比较,当然是可以的。
</span><span>536</span><span>//但是,有一种,更简单的分支结构来替换他</span><span>537</span><span>538</span><span>539</span><span>$day</span> =2<span>;
</span><span>540</span><span>switch</span>(<span>$day</span><span>){
</span><span>541</span><span>case</span> 1:
<span>542</span><span>echo</span> '星期一'<span>;
</span><span>543</span><span>break</span><span>;
</span><span>544</span><span>case</span> 2:
<span>545</span><span>echo</span> '星期二'<span>;
</span><span>546</span><span>break</span><span>;
</span><span>547</span><span>case</span> 3:
<span>548</span><span>echo</span> '星期三'<span>;
</span><span>549</span><span>break</span><span>;
</span><span>550</span><span>case</span> 4:
<span>551</span><span>echo</span> '星期四'<span>;
</span><span>552</span><span>break</span><span>;
</span><span>553</span><span>case</span> 5:
<span>554</span><span>echo</span> '星期五'<span>;
</span><span>555</span><span>break</span><span>;
</span><span>556</span><span>case</span> 6:
<span>557</span><span>echo</span> '星期六'<span>;
</span><span>558</span><span>break</span><span>;
</span><span>559</span><span>case</span> 7:
<span>560</span><span>echo</span> '星期日'<span>;
</span><span>561</span><span>break</span><span>;
</span><span>562</span><span>default</span>:
<span>563</span><span>echo</span> '其他'<span>;
</span><span>564</span> }<span>//</span><span>从上到下,判断$day和case指定的值,是否相等,如果相等,就执行以后的语句,否则就不执行。
</span><span>565</span><span>//因此,一定要用break退出 
</span><span>566</span><span>//如果没用switch case的话我们可以用什么替换?
</span><span>567</span><span>//
</span><span>568</span><span>//答:if / else if else 来替换  ,
</span><span>569</span><span>//
</span><span>570</span><span>//问:switch case ,与,if/esle if/else 相比,各适合什么 样的场合?
</span><span>571</span><span>//switch case 只能判断某变量是否与指定的值相等
</span><span>572</span><span>//
</span><span>573</span><span>//比如:值  1,2,3,4……
</span><span>574</span><span>//但是不能用来判断范围
</span><span>575</span><span>//
</span><span>576</span><span>//而if/ else if /else 可用来判断 范围,甚至更复杂的判断表达式,也可以
</span><span>577</span><span>//而case ,却只能提供一个准备的值,和变量做相等的比较。
</span><span>578</span><span>//如果 需要和几个确认的值比较是否相等,适合用switch\
</span><span>579</span><span>580</span><span>//==========================================
</span><span>581</span><span>582</span><span>//循环结构                                               
</span><span>583</span><span>//编程语言,一般都有for ,   while , do /while 循环
</span><span>584</span><span>//
</span><span>585</span><span>//先看while                                               <span><span>返回原处</span></span></span><span>586</span><span>//while(表达式){
</span><span>587</span><span>//    语句; 
</span><span>588</span><span>//}
</span><span>589</span><span>//判断表达式是否为真,如果为真,则执行语句
</span><span>590</span><span>//执行完语句之后,再回到while 开始处,判断表达式是否为真
</span><span>591</span><span>//如果为真……
</span><span>592</span><span>//…………
</span><span>593</span><span>//……
</span><span>594</span><span>//如果假,本次循环结束 </span><span>595</span><span>596</span><span>$hard</span> = 100<span>;
</span><span>597</span><span>$hit</span> = -1<span>;
</span><span>598</span><span>599</span><span>while</span>(<span>$hard</span> >= 30<span>){
</span><span>600</span><span>$hard</span> = <span>$hard</span> + <span>$hit</span><span>;
</span><span>601</span><span>if</span>(<span>$hard</span> >=30<span>){
</span><span>602</span><span>echo</span> '不服','<br>'<span>;
</span><span>603</span>     }<span>else</span><span>{
</span><span>604</span><span>echo</span> '服了','<br>'<span>;
</span><span>605</span><span>    }
</span><span>606</span><span>}
</span><span>607</span><span>echo</span><span>$hard</span>,'服了吧'<span>;
</span><span>608</span> -------------------------------------------
<span>609</span><span>while</span><span>(){
</span><span>610</span> }<span>//</span><span>  也有一次都不执行的,第一次条件就不成立,直接退出。</span><span>611</span><span>$hard</span> = 20<span>;
</span><span>612</span><span>$hit</span> = -1<span>;
</span><span>613</span><span>614</span><span>while</span>(<span>$hard</span> >= 30<span>){
</span><span>615</span><span>$hard</span> = <span>$hard</span> + <span>$hit</span><span>;
</span><span>616</span><span>if</span>(<span>$hard</span> >=30<span>){
</span><span>617</span><span>echo</span> '不服','<br>'<span>;
</span><span>618</span>     }<span>else</span><span>{
</span><span>619</span><span>echo</span> '服了','<br>'<span>;
</span><span>620</span><span>    }
</span><span>621</span><span>}
</span><span>622</span><span>623</span><span>//</span><span>-------------------------------------------
</span><span>624</span><span>//
</span><span>625</span><span>//如果碰到下面的情况</span><span>626</span><span>while</span>(<span>true</span><span>){
</span><span>627</span><span>echo</span> 'haha<br>'<span>;
</span><span>628</span> }<span>//</span><span>死循环</span><span>629</span> -------------------------------------------
<span>630</span><span>do</span><span>{                                                <span><span><span>返回原处</span></span></span></span><span>631</span><span>632</span> }<span>while</span><span>();  
</span><span>633</span><span>//</span><span>do ,即执行,上来先执行一下语句,然后再去判断
</span><span>634</span><span>//所有,do/while 到至少执行一次</span><span>635</span><span>636</span><span>$hard</span> = 20<span>;
</span><span>637</span><span>$hit</span> = -1<span>;
</span><span>638</span><span>do</span><span>{
</span><span>639</span><span>$hard</span> = <span>$hard</span> + <span>$hit</span><span>;
</span><span>640</span><span>if</span>(<span>$hard</span> >=30<span>){
</span><span>641</span><span>echo</span> '不服','<br>'<span>;
</span><span>642</span>     }<span>else</span><span>{
</span><span>643</span><span>echo</span> '服了','<br>'<span>;
</span><span>644</span><span>    }
</span><span>645</span> }<span>while</span>(<span>$hard</span>>=30<span>);
</span><span>646</span><span>647</span><span>//</span><span>==========================================
</span><span>648</span><span>//for(语句1;语句2;语句3){                               <span><span><span>返回原处</span></span></span></span><span>649</span><span>//    代码段; 
</span><span>650</span><span>//}
</span><span>651</span><span>//语句1,先执行 且只执行1次
</span><span>652</span><span>//然后 语句2判断,如为真,则执行 代码段; ,然后执行  语句3;
</span><span>653</span><span>//
</span><span>654</span><span>//再语句2判断……循环
</span><span>655</span><span>//直到语句2为假,for结束
</span><span>656</span><span>//
</span><span>657</span><span>//-------------------------------------------------------
</span><span>658</span><span>//打印1到100的整数,如果是3的倍数就打印Fizz,如果是5的倍数就打印Buzz
</span><span>659</span><span>//如果是3和5的倍数就打印abcd</span><span>660</span><span>$i</span> = 0<span>;
</span><span>661</span><span>while</span>(++<span>$i</span>){
<span>662</span><span>if</span>(<span>$i</span>%15 == 0<span>){ 
</span><span>663</span><span>echo</span><span>$i</span>,'----abcd----即是3的倍数,又是5的倍数','<br>'<span>; 
</span><span>664</span>     }<span>else</span><span>if</span>(<span>$i</span>%3 == 0<span>){
</span><span>665</span><span>echo</span><span>$i</span>,'----FiZZ----3的倍数','<br>'<span>;
</span><span>666</span>     }<span>else</span><span>if</span>(<span>$i</span>%5 == 0<span>){
</span><span>667</span><span>echo</span><span>$i</span>,'----BuZZ----5的倍数','<br>'<span>;
</span><span>668</span>     }<span>else</span><span>{
</span><span>669</span><span>echo</span><span>$i</span>,'<br>'<span>;
</span><span>670</span><span>    }
</span><span>671</span><span>}
</span><span>672</span> -------------------------------------------------------
<span>673</span><span>674</span><span>//</span><span>公鸡 5元1只,小鸡1元3只,母鸡 3元1只
</span><span>675</span><span>//100元买回100只,问公鸡,母鸡,小鸡各多少只?</span><span>676</span><span>for</span> (<span>$g</span> = 1;<span>$g</span>$g++<span>){
</span><span>677</span><span>for</span>(<span>$m</span> = 1;<span>$m</span>$m++<span>){
</span><span>678</span><span>$x</span> = 100-<span>$g</span>-<span>$m</span><span>;
</span><span>679</span><span>if</span>(5*<span>$g</span>+3*<span>$m</span>+<span>$x</span>/3 == 100<span>){
</span><span>680</span><span>echo</span> '公鸡',<span>$g</span>,'只,','母鸡',<span>$m</span>,'只,','小鸡,',<span>$x</span>,'只,<br>'<span>;
</span><span>681</span><span>        }
</span><span>682</span><span>    }
</span><span>683</span><span>}
</span><span>684</span><span>//</span><span>==========================================
</span><span>685</span><span>//
</span><span>686</span><span>//break 有破坏的意思,直接破坏整个循环语句,不再往下执行了。</span><span>687</span><span>688</span><span>for</span>(<span>$i</span> = 1;<span>$i</span> $i++<span>){
</span><span>689</span><span>if</span>(<span>$i</span> == 6<span>){
</span><span>690</span><span>break</span><span>;
</span><span>691</span>     }<span>else</span><span>{
</span><span>692</span><span>echo</span><span>$i</span>,'<br>'<span>;
</span><span>693</span><span>    }
</span><span>694</span><span>}
</span><span>695</span> --------------------------------------------------------
<span>696</span><span>//</span><span>continue  有继续的意思,跳出本次循环,继续执行下一次循环</span><span>697</span><span>for</span>(<span>$i</span> = 1;<span>$i</span> $i++<span>){
</span><span>698</span><span>if</span>(<span>$i</span> == 6<span>){
</span><span>699</span><span>continue</span><span>;
</span><span>700</span>     }<span>else</span><span>{
</span><span>701</span><span>echo</span><span>$i</span>,'<br>'<span>;
</span><span>702</span><span>    }
</span><span>703</span><span>}
</span><span>704</span><span>705</span> --------------------------------------------------------
<span>706</span><span>//</span><span>过收费站,手上有钱100,000元,当钱>50000时,交手上钱数的5%元,当钱数<span>707</span><span>for</span>(<span>$i</span>=0,<span>$money</span> = 100000;<span>$money</span> >= 5000;){ <span>//</span><span>看此处,第2个‘;’号后竟然是空//的</span><span>708</span><span>if</span>(<span>$money</span>>50000<span>){
</span><span>709</span><span>$money</span> *= 0.95<span>;
</span><span>710</span>  }<span>else</span><span> {
</span><span>711</span><span>$money</span> -=  5000<span>;
</span><span>712</span><span>713</span><span>    }
</span><span>714</span><span>$i</span>+=1<span>;
</span><span>715</span><span>echo</span> '第', <span>$i</span>,'过桥后','剩下的钱数为',<span>$money</span>,'<br>'<span>;
</span><span>716</span><span>}
</span><span>717</span><span>718</span><span>719</span><span>//</span><span>如下</span><span>720</span><span>for</span><span>(){
</span><span>721</span><span>echo</span>  'haha'<span>;
</span><span>722</span><span>//</span><span>别运行,死循环</span><span>723</span><span>}
</span><span>724</span><span>725</span><span>726</span> */<br>相关的视频连接:<br>1. 位运算符 ,移位运算<br>2.三大控制结构之顺序和分支</span>
Salin selepas log masuk

 php中的运算符、控制结构

以上就介绍了Java运算符 php中的运算符、控制结构,包括了Java运算符方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan