Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)归海一刀published in(发表于) 2014/2/10 6:47:39 PHP学习宝典-第七章_[PHP教程]
if (TRUE)
print(“This will always print”);
else
print(“This will never print”);
上面的范例与下面的确叙述的是相同的:
if(FALSE)
three = 3;
four = 4;
my_pi = 3.14159;
If ((three = =three) and
(four ===four)and
(three !=four)and
(three< four) and
(three<=four) and
(four>=there) and
(three<=three) and
(my_pi > three) and
(my_pi <=four))
print(“My faith in mathenmatics is restored! ”);
print (“Sure you typed that right?”):
if ((“Marx”<“Mary”)and
(“Mary”<“Marzipan”))
{
print(“Between Marx and Marzipan in the ”);
print(“dictionary, there was Mary .”);
}
strinhg_1 = “00008”;
string _2 = “007”;
string_3 = “00008-OK”;
If(string_2 Print(“string_2 is less than string_1 ”); If(string_3< string_2) Print(“string_3 is less than string_2”); If (string_1 < string_3) Print(“string_1 is less than string_3”); 输出结论为: 007 is less than 00008 //数字比较 00008-OK is less than 007 //字串比对 00008 is less than 00008-OK//字串比对产生矛盾 如果可以的话,PHP会把字串参数化为数字,当比较的两侧都能够这样处理时,就会按数字比较,而不是按字母来比较。PHP4设计人员把它当作一种特性,而不是错误。要我们的观点来看,如果在比较时可能会被转化为数字的字串,最好是还是使用strcmp()函式是比较好的作法(请参照第十章)。 三元运算子 一种非常有用的结构是三元条件运算子,它扮演了Boolean运算子和「真值」分支结构之间的角色。它有作用是带三个运算式,使用第一个运算式的值来决定另外两个运算式中的哪一个才是所需的,求值后传回它的值,该运算子的语法如下: test-expression ?yes-expression:no-expression 如果test-expression为真,这个运算式的值是yes-expression的结果,否则,就是no-expression的结果。 举例来看,下面的运算把max_num指定为first_num或second_num,这取决于两个参数哪一个比较大: max_num = first_num > second_num ? first_num:second_num; 正如我们可以看出的,上面的叙述等于: if (first_num >second_num ) max_num = first_num ; else max_num = second_num; 上述的例子若使用三元运算子则会更为简洁。 分支结构 分支的两种结构主要结构有if和switch.if是最常用的主要分支选择,通常也是所有人先学习的条件结构。在某些情况下switch也算是一种不错选取择,例如当基于单个值需要多个可能的分支时,或者当一系列if叙述非常麻烦时,非常适合使用switch。 if – else叙述 if的语法是: if(test) statement-1 或者,还可以带有可选取择的else分支: if(test) statement-1 else statement-2 当处理了if叙述后,test运算会被评算求值,结果被解释为一个Boolean值。如果test不为真并且没有else子句,则接著执行if结构后面的下一条叙述. 请注意,语法中的「statement」可以是分号结尾的单一叙述,也可以是大括弧围住的叙述区块,或者是另一个条件结构(它本身可看成单一叙述)。条件可相互巢状嵌套任意深度。另外,Boolean运算式可以是真正的Boolean值(即TRUE、FALSE或Boolean运算子或函式的结果),也可以是能够解释为布林值的其它任何型别的值。 关于布林值的完整资讯,请参考第六章。简单地说,数字「0」、字串「“0”」和空字串「“”」为非真(false),其它所有值为真(true)。 下面的例子是输出显示两数之间的绝对差,其中示范了使用条件的巢状嵌套,也提及把测试结果解译为Boolean值: if (first - second) if (first > second) { difference = first - second; print(“The difference is difference”); } else { difference = second - first; print(“The difference is difference”); } else print(“There is no difference”); 这段程式码依靠数字「0」解释为非真值,如果difference是「0」,则测试失败,输出显示「no difference」讯息,如果有差别,则执行一步的测试(这个例子是故意制作的,因为直接用像「first != second」这样的写法就与前述例子相同,且更容易理解)。 附加的else 此刻,以前使用过Pascal 的程式设计师可能很警觉到else部分有点怪,也就是说,else子句怎么知道它属于哪个if呢?其实规则很简单,几乎与Pascal外的其它所有语言中的情况相同。每个else与最接近的来匹配,当然首先要遵循大括弧限制住的范围。如果要确保if叙述维持独立、不需要与else匹配,那可以把它括在一对大弧中,如下所示: if(num % 2 = =0)// num为偶数? { if (num > 2) print(“num is not prime”); else print(“num is odd”); 如果num是大于2的偶数,则程式码将印出「num is not prime」,如果num为奇数,则印出「num is odd」,如果num恰好为2,则什么也不显示。如果省略了大括弧,则else会附加到内层的if,因此如果num等2,程式码也会错误地印出「num is odd」,如果num的确是奇数,程式码则什么也不列印。 在本章的例子中,我们常使用取余数运算子(%),该运算子在第十二章中讲解。对于这些例子,读者只需要知道「x % y」为「0」的意思是「x能被y整除」。 Elseif 进行层叠型式的测试是很常见的,如下面的巢状嵌套if叙述所示: if (day == 5) print(“Five golden rings”); else if (day ==4) print(“Four calling birds”); else if (day ==3) print(“Three French hens”); else if (day ==2) print(“Tow turtledoves”; else if (day ==1) print(“A partridge in a pear tree”); 这种型式很常见,有一个特殊的elseif结构来处理它。我们可以把前面的例子重写为: if (day == 5) print(“Five golden rings”); elseif(day == 4) print(“Four calling birds”); elseif(day == 3) print(“Three French hens”); elseif(day == 2) print(“Two turtledoves”); elseif(day ==1) print(“A partridge in a pear tree”); if、elseif……结构允许只在彼一个分支测试面成功时循序进行测试。从理论上说,这和前面的例子(一个结构带五个分支,而不是巢状嵌套的五个两头分支结构)在语法上是不同的,但行为是相同的。自己认为哪种好就用哪个吧。 分支和HTML模式 从前面几章中你已学过可以根据想要自由使用PHP标记在HTML模式和PHP模式间来回进行切换,无论如何都有是相当便利的。如果需要在页面中包括大区块的HTML文字,并且没有动态程式码或插入的变数,则切换回HTML模式并直接用文字来包含它,这样就比用print或echo发送它更为简单和有效。 不太明显的是,这个策略即使在本身条件结构内部也有效。也就是说,可以使用PHP决定传送什么HTML,然后透过临时切换回HTML模式来「传送」这些HTML。 举例来说,下面的程式码使用print叙述建构了一个完整的HTML网页(我们假设了一个female()的Boolean函式来测试它)。
Print(“string_2 is less than string_1 ”);
If(string_3< string_2)
Print(“string_3 is less than string_2”);
If (string_1 < string_3)
Print(“string_1 is less than string_3”);
if (first - second)
if (first > second)
difference = first - second;
print(“The difference is difference”);
difference = second - first;
print(“There is no difference”);
if(num % 2 = =0)// num为偶数?
if (num > 2)
print(“num is not prime”);
print(“num is odd”);
if (day == 5)
print(“Five golden rings”);
if (day ==4)
print(“Four calling birds”);
if (day ==3)
print(“Three French hens”);
if (day ==2)
print(“Tow turtledoves”;
if (day ==1)
print(“A partridge in a pear tree”);
elseif(day == 4)
elseif(day == 3)
elseif(day == 2)
print(“Two turtledoves”);
elseif(day ==1)
If(female())
Print(“TITLE>The women-only site”);
print(“
print(“This site has been specially constructed”);
print(“for women only. No men allowed here!”);
Else
Print(“TITLE>The men-only site”);
print(“for men only. No women allowed here!”);
?>
我们可以不用所有这些print语法,而且两部分支内部都使用HTML程式码,例如:
This site has been specially constructed
For women only. No men allowed here!
for men only. No women allowed here!
switch( expression)
case value -1:
statement-2
…
[break;]
case value-2:
statement-3
statement-4
[default:
default-statement]
switch(day)
case 5:
break;
case4:
case 3:
case 2:
print(“Tow turtledoves”);
default:
这个英文歌词的例子会在2-5于之中输出显示相对应的该行,而在其它日期则列印「A partridge in a pear tree」。 Switch 让人困扰的地方是,一个匹配的case后的所有case都将会执行,直到有break叙述来停止执行。在「partridge」的例子中,break叙述确保了一次只看到歌词中的一行。如果去掉break叙述,则会看到几行都被显示出来。 回圈 恭喜!你刚刚跨过了从script编制到「真正的程式设计」之间的界限了。到目前为止,我们看到的分支结构还算有用,但使用它们完成的计算还是有限的。另一方面,任何带有评算测试和无界限回圈的语言都能够比任何其它语言完成更好的功能,在电脑科学理论中,这一点是很确定的。在PHP中你也许并不需要编写一个C语言编译器,不过请记住这里没有什么天生的语言限制会阻止你这样做。
出处:南方Linux
赞