果然,PHP是最好的开发语言!!个人感觉基础语法和c语言没多少区别,不过估计后面语法细节上还是会有很大差别。
1变量与常量
<?php $a = 10; $b = 20; $c = $a+$b; echo $c; //定义变量 echo '<br/>'; const the_value = 20; echo the_value; //定义常量 ?>
2函数的定义与调用
<?php function say() { echo "hello<br/>"; } say(); function add($a,$b) { return $a+$b; } echo add(3,4); ?> 3if and switch等分支条件语句的使用 <<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>ifANDswitch</title> </head> <body> <?php function getLevel($score) { if($score>90) return "very good"; else if ($score>80) return "good"; else if($score>70) return "pass"; else return "failure"; } echo getLevel(93); echo"<br/>"; function switchLevel($score) { $a=floor($score/10); switch ($a) { case 10: case 9: return "very good"; case 8: return "good"; case 7: return "pass"; default: return "failure"; } } echo switchLevel(83); ?> </body> </html>4循环控制语句
<?php for($i=0;$i<10;$i++) { echo "$i<br/>"; } echo "<br/>"; //for循环 $i=0; while($i<10) { echo "$i<br/>"; $i++; } echo "<br/>"; //while循环 for($i=0;$i<10;$i++) { if($i==5) { break; } echo "$i<br/>"; } echo "<br/>"; //break跳出 for($i=0;$i<10;$i++) { if($i==5) { continue; } echo "$i<br/>"; } echo "<br/>"; //continue跳出 ?>5逻辑运算符 || && !
同C语言的使用一样。