PHP 学习语法




	

It works!

namespace Library; "; //1.数据类型 $bool = true; $bool2 = false; $a = 3.14; $char1 = "双引号字符串"; $char2 ='单引号字符串'; //单引号和双引号在变量表示和字符的转义有区别 echo "$char1"."
"."$char2
"; echo "检测变量的类型使用函数"; $ais = NULL; echo "检查是否为空:".is_null($ais)."
"; echo "检查是否是浮点数:".is_float($a)."
"; // s_file() // is_array() // is_string(var) $ais = "string"; echo "检查是否是字符串:".is_float($ais)."
"; //在我这里条件为假的时候不打印信息,为真的时候打印0; if (!is_float($ais)) { echo "不是字符串
"; } //可以将变量的值作为一个变量的名称,这里竟然是可变变量 $txt = 'abc'; $$txt = "hello"; //等于 $abc = 'hello'; echo "验证可变变量:$abc = hello
"; //变量类型的转换。phpc存在自动类型的变量转换, $a = 0; $b = 0.0; $c =""; $d = "0"; $e = array();//数组类型 $f = NULL; //这个判断两个条件竟然都不执行 if ($a==false&&$b==false&&$c==false&&$d==false&&$e==false&&$f==false) { echo '都真的是false
'; } $b = $a + 1; var_dump($b);//dump能够打印出类型 $a = 1 + "-1.5e2"; echo "
字符和整数相加:$a
"; $a = true+1; echo "bool转换$a
"; //转换字符串类型 $a = ture ; $b =3 ; $c = 5.5; $d = $a. 'string'.$b.$c; var_dump($d); //强制类型转换 -- (类型)变量 $num = 10.00;//10.00 是字串10 var_dump((string)$num); $a;$b;$c;//逗号不行 $a=$b=$c =10; echo "$a$b$c
"; //流程语句 switch ($a) { case 1: echo "---1"; break; case 10: echo "====$a"; break; default: break; } for ($i=0; $i <10 ; $i++) { echo "$i."; } echo "
"; //替换语法,括号换成冒号--endfor for ($i=0; $i < 5; $i++): echo "$i"; endfor; //-----函数定义 function call($name) { echo "你好,我是$name
"; } call("王国栋"); function add($one,$two) { return $one + $two; } echo add(2,3); //echo add(2,3); $var = 100; function testone (){ global $var; echo "全局变量的值是:$var"; echo $GLOBALS['var']."
"; } testone(); //可变函数 $aliasTestOne = "testone"; $aliasTestOne(); //字符串相关的函数 $str = "apple,pear,banana,orange"; $arr = explode(",", $str); print_r($arr); $arrayName = array("apple","pear","bananer"); echo implode("&", $arrayName); //strcmp和==比较字符串一样,但是结果正好相反 //trim strlen 函数 //时间戳 echo "
"; //mktime 函数有许多参数 echo date("Y-m-d:H-i-s",mktime());//当前的时间,但是不准确 /* mktime加参数出错 */ echo "
"; echo date("Y-m-d:H-i-s",mktime(14,28,56,4,10,2013));//当前的时间,但是不准确 echo "

"; //设置时区 date_default_timezone_set("Asia/Shanghai"); echo date("Y-m-d H-i-s",time()); //输出明天时间点, echo "
".date("Y-m-d H-i-s",strtotime("+1 day"));; function microtime_float(){ list($usec,$sec) = explode(" ", microtime()); return ((float)$usec+(float)$sec); } $time_start = microtime_float(); usleep(1000); $time_end = microtime_float(); $time = $time_end - $time_start; echo "执行时间是 $time" ; function appleNumber($n) { if (1==$n) { return (1+1)*2; } else { return 2*(appleNumber($n-1)+1); } } echo "
桃子的个数是:".appleNumber(3); //数组 $arr = array('key' => "name",'key2'=> "name2");//打印数组 print_r($arr); var_dump($arr);//可以输出类型 $arridx[0]=123; $arridx[1]=124; $arridx[2]='hello'; echo "删除前:"; print_r($arridx); echo "删除后:"; unset($arridx[1]); print_r($arridx); $arrone = array('a' => "abc",'b' => 'bcd' ); $arrtwo = array('a' => "cde", 'd' =>'def'); $c = $arrone + $arrtwo; var_dump($c); echo "each函数的用法:-------
"; $shuzu = array('one' => 'ONE','two','Three'=>'3','four',5); print_r(each($shuzu)); print_r(each($shuzu)); echo "数组遍历\n"; foreach ($shuzu as $key => $value) { echo "$key"; echo "=>"; echo "$value"; echo "\n"; } //foreache 和list each遍历数组 $dict = array('os' => "linux",'server' =>'Apache','language'=>'Php','database'=>'MySql'); while (list($key,$value)=each($dict)) { echo "$key=>$value\n"; } $sort = array(9,8,10,2,4); function bubbleSort($array) { for ($i=0; $i $array[$j+1]) { $t = $array[$j]; $array[$j] = $array[$j+1]; $array[$j+1] = $t; } } } print_r($array); } bubbleSort($sort); $arrfunc = array(1,2,2,4,5); is_array($arrfunc); print_r(array_unique($arrfunc)); echo array_search('Apache', $dict); $array = array(1,2,3,11,'11'); print_r(array_keys($array)); print_r(array_keys($array,"11",false));//查找11不依赖类型 sort($array,SORT_NUMERIC); print_r($array); echo "
"; /** * */ class Person { public $name; public $age; function __construct($name,$age) { if ($name==NULL) { $name = "def"; } if ($age==NULL) { $age = 10; } $this->name = $name; $this->age = $age; } public function speak(){ echo "大家好,我叫".$this->name."今年".$this->age."岁\n";//this 不调用get方法 } public function __get($property_name) { echo "自动调用__get方法获取属性\n"; if (isset($this->$property_name)) { return ($this->$property_name); } else { return (NULL); } } public function __set($property_name,$value) { echo "自动调用set方法\n"; $this->$property_name = $value; } //析构函数 function __destruct() { echo "对象被销毁"; } } //--------------------------------这里需要看下,在定义construct方法后,这样不行 // $me = new Person(); // $me->name = "guodong"; // $me->age = 10; // $me->speak(); // echo "$me->name";// 这里会调用get方法 $me2 = new Person("xiaowang","25"); $me2->speak(); //echo Person::PI; // /** * */ class MathTool { const PI = 3.14; static $schoolName = "php学院";//对象共享 function show () { echo self::PI; echo MathTool::$schoolName;//---少了$ echo "\n"; //echo MathTool::PI; } //类方法 public static function classMethod() { echo "这相当于oc里面的类方法\n"; } } $stu1 = new MathTool(); $stu2 = new MathTool(); $stu2->show(); $stu1->show(); MathTool::classMethod(); /** * */ final class superMathTool extends MathTool //禁止其他类继承 { final function show() { echo "子类重写父类方法"; } } $math = new superMathTool(); $math->show(); abstract class Animail //抽象类,可以包含非抽象方法 { abstract function shout(); } /** * */ class Dog extends Animail { function shout() { echo "\n汪汪汪\n"; } } (new Dog())->shout(); //--接口。类似于OC, interface Animail2 //抽象类,可以包含非抽象方法,没有class关键字 // { function shout();//这里也没有abstract } class Dog2 implements Animail2 { function shout() { echo "\n汪汪汪\n"; } } (new Dog2())->shout(); //一个类可以实现多个接口,和OC一样 //错误处理 ini_set('display_errors', 1); $rand_num = mt_rand(0,1); if ($rand_num==0) { error_reporting(E_ALL); } else { error_reporting(E_ALLE_NOTICE); } echo $info; ?>

你可能感兴趣的:(iOS)