PHP 基础
<h1 style="color:red;"> Start</h1> //1.注释//2.变量//3.可变变量 <?php //1.注释 /*PHP 脚本以 <?php 开始,以?>结束*/ //php单行注释;/* 多行注释*/ //2.变量 //变量以 $ 符号开始,后面跟着变量的名称,php为弱类型语言,不用定义变量的数据类型。 print "<br />"; echo $name; //变量提前被声明 $name = "jack"; echo $name,"is walk"; unset($name);//释放变量 echo $name,"is walking";//变量已被释放,此句报错 $a=1;$b=3;$c=2; $sum = $a+$b+$c; echo "<hr />"; echo "a + b + c =",$sum; echo "<h2 style='color:green'>i am in php<h2/>"; $names = "jack"; echo var_dump($names); //3.可变变量 $a1="hello"; $$a2="world";// echo $a1; echo $$world;//可变变量 ?> <hr / > /*4.作用域*//*5.参数作用域*/ <?php /*4.作用域*/ $Day = 30;//全局变量 $Month = 12; function foo () { global $Day;//如果要在一个函数中访问一个全局变量,需要使用 global 关键字。 $hours = 24;//局部变量。 echo "inside we have ", $Day," days."; echo "One day has ",$hours," hours."; echo "<br />"; echo $GLOBALS['Day'],"and",$GLOBALS['Month']; //通过$GLOBALS['全局变量']也可以在函数中访问全局变量。 } foo();//函数调用。 echo "<br />"; echo "outside we have {$Day} days."; echo "One day has {$hours} hours."; /*5.参数作用域*/ function fun($argss){ echo "Args is {$argss}."; echo __METHOD__; } fun("我的参数"); ?> <hr /> /*6.常量*///php自带常量 <?php /*6.常量*/ define("Years",2020); //define(name,values),定义常量,常量使用不能添加$ echo Years; echo "<br />"; //php自带常量 echo __FILE__;echo "<br />"; echo PHP_VERSION;echo "<br />"; echo PHP_OS;echo "<br />"; echo __CLASS__;echo "<br />"; //$_SERVER 是一个包含了诸如头信息(header)、路径(path)、以及脚本位置(script locations)等等信息的数组。 echo $_SERVER['PHP_SELF']; echo $_SERVER['SERVER_NAME']; echo $_SERVER['HTTP_USER_AGENT']; echo $_SERVER['SCRIPT_NAME']; //$_REQUEST 用于收集HTML表单提交的数据 $name = $_REQUEST['input_name']; //$_POST 收集由post提交的数据。 $username = $_POST['username']; // $_GET收集get提交的数据。 $args = $_GET['serach'] // http://www.xxx.com?search=php. ?> <hr /> /*7.php标识符*/ <?php echo <<<EOF <h1>我的第一个标题</h1> <p>我的第一个段落。</p> EOF; // 结束需要独立一行且前后不能空格 ?> <hr /> /*8.条件判断if-else*/ 9.switch语句 <?php echo "<br />"; $arg1=10; $arg2=20; $arg3="10"; if ($arg1 == $arg3) {//松散比较:使用两个等号 == 比较,只比较值,不比较类型。 echo "arg1 == arg3"; if ($arg1 != $arg2){ echo "<br />"; echo var_dump($arg1 != $arg2);//bool(true) } } elseif ($arg1 === $arg3){//严格比较:用三个等号 === 比较,除了比较值,也比较类型。 echo "arg1 === arg3"; } else { echo "WARNing"; } echo "<br />"; //9.switch语句 :有选择地执行若干代码块之一 $action = "walk"; switch ($action){ case "walk":echo "i am walking"; break; case "sleep":echo "i am sleeping"; break; case "fight":echo "i am fighting"; break; default: echo "i am do nothing"; //如果$action没有值,默认走这这行代码块。 } ?> <hr /> /*10.数组*///11.关联数组 //12.多维数组 //13.数组排序 <?php //array() 函数用于创建数组 $car = array("BWM","Audi","Benz"); echo $car[0],$car[1],$car[2]; echo count($car);//3;count()获取数组长度 echo "<br />"; //11.关联数组:使用您分配给数组的指定的键的数组 $ages = array("Mick"=>22,"Jack"=>30,"ruby"=>21); echo "Mick is {$ages['Mick']},and ruby is {$ages['ruby']}"; echo "<hr / >"; //12.多维数组 $menu = array ( array("鸡蛋炒饭",10,11), array("牛肉盖饭",20,21), array("宫保鸡丁",30,31) ); print("<pre>");// 格式化输出数组 print_r($menu); print("<pre />"); echo "i want to eat {$menu[1][0]},it's cost {$menu[1][1]}dollars. she wants to eat {$menu[3][0]}"; echo "<br />"; //13.数组排序 sort($car); print_r ($car); $num = array(5,8,2,9,1); sort($num);//升序 print_r ($num); rsort($num);//降序 print_r ($num); /*asort() - 根据数组的值,对数组进行升序排列 ksort() - 根据数组的键,对数组进行升序排列 */ ?> <hr /> /*14.循环语句*/ <?php //15.while 循环语句 $a = 1; while ($a <= 6){ echo "Num is {$a}"; $a++; }; echo "<br />"; //16.do..while 循环语句 $b = 1; do { echo "inside num is {$b}"; $b++; } while ($b <=6); echo "<br />"; //17.For循环语句 for ($i=0;$i<=6;$i++) { echo "{$i}man".PHP_EOL; } echo "<br />"; //18.foreach循环语句:遍历数组 $color = array("blue","red","green"); foreach ($color as $val) {//当前数组元素的值就会被赋值给 $value 变量 echo $val.PHP_EOL; } $x=array(1=>"Google", 2=>"Runoob", 3=>"Taobao"); foreach ($x as $key => $value)//当前数组元素的键与值就都会被赋值给 $key 和 $value 变量 { echo "key 为 " . $key . ",对应的 value 为 ". $value . PHP_EOL; } ?> <hr /> /*19.函数*/ <?php function fun1(){ echo "i am fun1"; } function fun2($args){ echo "i am fun2 and my args is {$args}"; } function fun3($ar1,$ar2){ $sums = $ar1+$ar2; echo "the sum is {$sums}"; return $sums; } //调用函数 fun1(); echo PHP_EOL; fun2(55); echo PHP_EOL; fun3(10,20); $res = fun3(10,20); echo "{$res}"; ?> <hr /> /*20.魔术常量*/ <?php echo "this is line ",__LINE__;ECHO PHP_EOL; echo "this is filepath ",__FILE__;ECHO PHP_EOL; echo "this is dirpwd ",__DIR__;ECHO PHP_EOL; echo "this is __FUNCTION__",__FUNCTION__;ECHO PHP_EOL; ?> <hr /> <span style="color:blue;">End</span>