参考《PHP5与MySQL5从入门到精通》.(卫喆, 陈争航). ';//phpinfo();echo date('Y-m-d');$func = true;var_dump($func);$int = 5;echo 'hello' . gettype($func) . ' ' . $int;if (is_array($int)) { echo 'is array';} else { echo 'is not a
'; //phpinfo(); echo date('Y-m-d'); $func = true; var_dump($func); $int = 5; echo 'hello' . gettype($func) . '
' . $int; if (is_array($int)) { echo 'is array'; } else { echo 'is not array'; } $i = 1.111112; $ii = 3.14; echo $i . ',' . gettype($i); echo '
break usage what do you like? '; echo '
\\print\'danyinh\'
'; echo "abc$i${ii}i
"; $str = <<'dkdk' EOF; echo "strange string output $str "; //serialize($str); $arr = ['a' => 'b', 'c', 'b', 'c' => 'd', 'c']; echo $arr['a'] . ''; var_dump($arr); echo $arr[0] . ''; class foo { function printLog() { echo "log print "; } } $f = new foo; $f->printLog(); $obj = (object)'hello sang'; var_dump($obj); $var_arr = get_object_vars($obj); foreach ($var_arr as $k => $v) { echo "$k:$v"; } echo '$obj class is :' . get_class($obj) . ''; $obj_from_arr = (object)$arr; var_dump($obj_from_arr); //---- 传值&传引用 ------// $var1 = 'abc'; $var2 = 12; echo "var1:$var1" . ",var2:$var2"; $var3 = $var1; echo "var3:$var3"; $var1 = 'ab'; echo ",by_value_var3:$var3"; $var3 = &$var1; echo ",by_ref_var3:$var3"; //---------- 预定义变量 ------ var_dump($GLOBALS); //---- 表单提交例子 --------- var_dump($_POST); ?> 'abc', '2' => 'def'); $arr2 = array('1' => '123', '3' => '456'); var_dump($arr1 + $arr2); //-------- instanceof --- class A { } class B { } $a = new A; echo $a instanceof A; //------- 反引号---------------------- $list = `ls -alh ./`; echo $list; //--- foreach遍历 foreach ($arr1 as $k => $v) { echo $k . ':' . $v . ''; } foreach ($arr1 as $v) { echo $v; } foreach ($arr1 as &$v) {//加上&修改数组原值 $v = '111'; } var_dump($arr1); //-------------- 范围解析符(静态调用) --------------------- class Test { function printMsg($msg) {//默认是public的 echo $msg; } } $msg = 'hello world'; Test::printMsg($msg); // ---------- 静态属性 静态方法 ----------------- class TestA { static $name = 'abc'; function showName() { echo self::$name; //方法内部调用 } } class TestB { var $name = 'abc';//必须带上修饰符比如 var, public,private,protected function showName() { echo $this->name; } } class TestC { const MAXSIZE = 12;//类里定义常量 static $mtd = 'ad'; var $mtd2 = 'abc'; static function showMtd() { echo 'static method:' + self::$mtd; } function showMtd2() { echo 'normal method:' + $this->mtd; } function getConst() { echo self::MAXSIZE; } } TestC::showMtd(); //TestC::showMtd2();//此时this会报错,无法找到.和java还是区别很大 $tc = new TestC(); echo TestC::MAXSIZE;//访问类中常量 // -------------- 类的继承 ----------------------- class Pa { var $a = 'you ok?'; function show() { echo 'from parent;'; } } class Ch extends Pa { function show() { echo 'from child;'; //也指明再执行父亲方法 parent::show(); } } $ch = new Ch; echo $ch->a; $p = new Pa; $c = new Ch; $p->show(); $c->show(); //------------ 构造函数 -------------- class T1 { function __construct() { echo 'abcdddd;'; } } class T2 extends T1 { function __construct() { //不带参数构造函数,基类自动执行 echo 'ddddddd;'; } } $t2 = new T2; class T3 { var $a; function __construct($a) { $this->a = $a; } } class T4 extends T3 { function __construct($a) { parent::__construct($a);//父子类构造函数带参,则必须显式调用父类构造函数! } } //------------------- 析构函数 -------- class T5 { var $a;// php4中用var,php5用public,但兼容var,参考http://www.kuitao8.com/20150427/3717.shtml function __destruct() { echo ou'do destruct;'; } } $t5 = new T5; unset($t5); // ------------------ 序列化&反序列化 --------------------- class T6{ public $a; public $b; function __sleep() { return(array('a'));//只序列化a } function __wakeup() { $this->b = uniqid();//初始化未序列化的b } } $t6 = new T6; $t6->a = 'sang;'; $t6s = serialize($t6); $t6us = unserialize($t6s); var_dump($t6us); // ------------------- 字符串操作 -------------------- $str1 = 'abcdefgHIGK '; echo $str1{0}; echo strlen($str1); echo strlen(trim($str1)); echo strtoupper($str1); echo strcmp($str1,$str); echo substr($str1,0,2);//ab echo substr_replace($str1,'11',0,2); echo substr_replace($str1,'11',-1,-2); echo strpos($str1,'e'); $str2 = 'a|b|c'; $str_arr = explode('|',$str2);//分割 var_dump($str_arr); $str3 = implode('@',$str_arr);//合并 echo $str3; $str4 = 'http://blog.csdn.net/spy19881201/article/details/25537225?a=1&b=2'; var_dump(parse_url($str4)); // ----------- 正则表达式 -------------- echo preg_match('/^a?b$/','ab'); echo preg_match('/^a?b$/','aab'); echo preg_match('/^\-{0,1}[0-9]{0,}\.{0,1}[0-9]{0,}$/','-1.2'); echo preg_match('/^\-?[0-9]*\.?[0-9]*$/','-1.2'); //--------------------- 数组 ------------------------- $arr1 = array('a','b');//索引数组 $arr2 = array('1'=>'a','2'=>'b');//关联数组 var_dump($arr1); var_dump($arr2); $arr3 = array('a','1'=>'b','0' =>'c');//注意,如果key为整形字符串,比如'0',会转成索引数组.数组后面的值覆盖前面的值 var_dump($arr3); echo $arr3['0']; $arr4['a'] = 'b';//arr4不存在,则自动创建一个!!! foreach ($arr2 as $item){ echo $item; } foreach ($arr2 as $k => $v){ echo $k.':'.$v; } phpinfo(); ?>