PHP语法的基础 a定义变量常量,b定义函数,c流程控制,d循环,e逻辑运算
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
</body>
</html>>
<?php
/**
* Created by PhpStorm.
* User: haily
* Date: 2016/4/11
* Time: 23:12
*/
//1.定义变量,常量
echo '1.定义变量,常量';
echo '<br>';
echo 'hello world,';
echo '<br>';
$c = 1515;
$b = 10;
$c = 10;
const FIR = 100;
echo $c + $b;
echo '<br>';
echo FIR;
define('VARR', 300);
echo VARR;
//2.书写函数
echo '<br>2.书写函数';
echo '<br>';
function hello()
{
echo '这是一个函数 byp hp';
}
hello();
$fun = 'hello';
$fun();
echo '<br>';
function hello1($name)
{
echo 'hello' . $name;
echo '<br>';
}
hello1('张三');
echo '<br>';
hello1('to 李四');
echo '<br>';
function hello2($a, $b)
{
echo '1------a=' . $a . ', b=' . $b . '<br>';
echo "2--------a=$a, b=$b <br>";
}
hello2(5, 6);
function add($a, $b)
{
return $a + $b;
}
echo add(10, 25) . '<br>';
echo 'hello';
//3.流程控制
echo '<br>3.流程控制' . " if else 和 switch 循环 for while 都 while ". '<br>';
function getlevel($score)
{
// if($score>90){
// return "优秀 <br>";
// }
// elseif($score>80){
// return '良好 <BR>';
//
// } elseif($score>=60){
// return '及格<br>';
//
// } elseif($score>45){
// return '一般';
//
// } else{
// return '最差的家伙';
//
// }
// switch ($score / 10) {
// case 10:
// case 9:
// return '优秀<br>';
// case 8:
// return '良好<br>';
// case 6:
// return '一般<br>';
//
// default :
// return "太差了" ;
// }
$result = '差';
switch (intval($score / 10)) {
case 10:
case 9:
$result = '优秀';
break;
case 8:
$result = '良好';
break;
case 6:
$result = '一般';
break;
default :
$result = 'default';
break;
}
echo $result;
}
echo getlevel(100);
echo getlevel(88);
echo getlevel(80);
echo getlevel(60);
echo getlevel(2);
// <pre name="code" class="php">4.循环
echo "<br> 4.循环<br>";for ($i = 0; $i < 10; $i++) { echo "hello $i <br>";}$j = 0;while ($j < 10) { echo "while $j <br>";// if($j==5){// break;// }// if($j==5){// continue;// }// echo 'come here $j '; $j++;}
// <pre name="code" class="php">5.逻辑运算 或 || 且 && 非 !
echo "<br>5.逻辑运算 或 || 且 && 非 ! <br>";function traceNum(){ for ($i = 0; $i <= 20; $i++) {// if($i%2==0 || $i%3==0){ echo '<br> '.$i;// } if ($i % 2 != 0) { echo '<br>' . $i; } }}traceNum();?>