PHP基础语法
<!--表单--> <form> <p>First name:<input type="text" name="fname"/></p> <p>Last name:<input type="text" name="lname"/></p> <h3>单击同意</h3><br/> <input type="radio" name="radiobutton" value="同意"/>同意<br/> <input type="radio" name="radiobutton" value="不同意"/>不同意<br/> <h3>酒店级别</h3> <select name="hotelclass"> <option selected>不限</option> <option>五星级/豪华</option> <option>四星级/高档</option> <option>三星级/舒适</option> <option>二星级以下/经济</option> </select> <p><input type="submit"/></p> </form> <?php define('name','PHP');//常量定义 $a="小小\n"; $b=18;//变量 echo "Hello World\n"; echo name; echo __DIR__; echo nl2br("\n"); if($b>100){ echo "$b大于100"; }else if($b<100){ echo nl2br($a); } echo '$b小于100'; echo nl2br("\n"); $myarray=array('a'=>'apple','b'=>'banana','c'=>'carrot');//数组 foreach($myarray as $key=>$value){ echo "$key=$value,"; } echo nl2br("\n"); //函数(具有全局属性) function fun1($a){ //function定义函数,fun1是函数名, //当形参写成&$a就会有不同的结果,相当于传址并不是传值 $a++; } $b=1; fun1($b); echo "$b"; //参数的默认值 //function my($a,$b=true,$c='test',$d=null){}当调用此函数时,可以不必传入有默认值的参数 echo nl2br("\n"); $c=1; function fun(){ global $c;//全局变量,用global获取全局变量,当注释掉这句的时候,运行就会报错 $c++; } echo $c;//输出1 fun(); echo $c;//输出2 echo nl2br("\n"); function funn(){ static $a=0;//静态变量 echo $a; $a++; } funn();//输出0 funn();//输出1 funn();//输出2 //表单 if(isset($_GET['fname'])){ echo "First name:${_GET['fname']}<br/>"; } if(isset($_GET['lname'])){ echo "Last name:${_GET['lname']}<br/>"; } if(isset($_GET['radiobutton'])){ echo "你的选择时:${_GET['radiobutton']}<br/>"; } if(isset($_GET['hotelclass'])){ echo "你的酒店选择是:${_GET['hotelclass']}<br/>"; } //字符串 $str1="Hello"; $str2="World"; $str3=$str1 .$str2;//连接上面两个字符 $str4=$str1 . " " . $str2; echo "$str3<br/>";//输出HelloWorld echo $str4; //字符串中的函数 echo nl2br("\n"); echo strlen('中文');//输出6.中文占3个位置按照utf-8格式 echo nl2br("\n"); echo strpos('abc123','1');//输出3 echo nl2br("\n"); echo substr('abcdef',1,3);//bcd echo nl2br("\n"); print_r(explode(',','a,b,c,d'));//以数组模式输出 echo nl2br("\n"); echo implode(',',['a','b','c','d']);//输出:a,b,c,d echo nl2br("\n"); //数组 //一维数组 $food=array("饼干","巧克力","蛋糕");//定义了一个数组 $food=["饼干","巧克力","蛋糕"];//等于上面那个语句 $food[0];//这样调用数组元素 //二维数组 $fo=array( array("饼干",4,6), array("巧克力",7,12), array("蛋糕",5,8) ); $fo[0][0];//这样调用数组元素 echo count($food);//统计数组个数 echo nl2br("\n"); echo count($fo);//统计数组个数 echo nl2br("\n"); foreach($food as $value){//遍历数组 echo "$value,"; } echo nl2br("\n"); $price=array(12,8,44,55,100); sort($price);//数组排序 foreach($price as $value){ echo "$value,"; } echo nl2br("\n"); $a=["old1","old2"]; array_push($a,"push1","push2");//数组后面添加元素 foreach($a as $value){ echo "$value,"; } echo nl2br("\n"); $a=["old1","old2"]; array_unshift($a,"push1","push2");//数组前面添加元素 foreach($a as $value){ echo "$value,"; } echo nl2br("\n"); $b=["小小1号","小小2号","小小3号"]; array_pop($b);//删除数组后面的元素 foreach($b as $value){ echo "$value,"; } echo nl2br("\n"); $b=["小小1号","小小2号","小小3号"]; array_shift($b);//删除数组前面的元素 foreach($b as $value){ echo "$value,"; } ?>
微信扫码关注
更新实时通知