PHP入门笔记 语言基础 数据类型 1 2 3 4 <?php $name ="..." ; echo "xxx" .$name ."xxx" ; ?>
类型转换 1 2 3 4 5 <?php $num ='3.1415926r*r' ; echo (int )num; $result =settype ($num ,'integer' ) ?>
类型检测 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php $boo ="043112345678" if (is_numberic ($boo )){ echo "xxx" ; }else { echo "xxx" ; } if (is_null ($boo )){ echo "xxx" ; }else { echo "xxx" ; } ?>
定义常量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?php define ("MESSAGE" ,"xxx" ); echo MESSAGE; ?>
指针(引用赋值) 1 2 3 4 5 6 7 8 <?php $i ="book" ; $j =&$i ; $i ="bccd" ; echo $j ; echo "<br>" ; echo $i ; ?>
预定义变量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?php $_SERVER ['SERVER_ADDR' ] $_SERVER ['SERVER_NAME' ] $_SERVER ['REQUEST_METHOD' ] $_SERVER ['REMOTE_ADDR' ] $_SERVER ['REMOTE_HOST' ] $_SERVER ['REMOTE_PORT' ] $_SERVER ['SCRIPT_FILENAME' ] $_SERVER ['SERVER_PORT' ] $_SERVER ['SERVER_SIGNATURE' ] $_SERVER ['DOCUMENT_ROOT' ] $_COOKIE $_SESSION $_POST $_GET $GLOBALS ?>
字符串 1 2 3 4 5 <?php $i ="xxx" ; echo "$i " ; echo '$i' ?>
函数 1 2 3 4 5 6 7 <?php function example ($num ) { echo "$num *$num =" .$num *$num ; return $num *$num ; } $sum =example (10 ); ?>
流程控制 条件控制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?php $num =rand (1 ,20 ); if ($num %2 ==0 ){ echo "..." ; }else { echo "..." ; } ?> <?php $num =rand (1 ,20 ); if ($num %2 ==0 ){ echo "..." ; }elseif ($num %3 ==0 ){ echo "..." ; }else { echo "..." ; } ?>
日期 1 2 3 4 5 <?php date_default_timezone_set ("Asia/Shanghai" ); $month =date ("n" ); $today =date ("j" ); ?>
switch 1 2 3 4 5 6 7 8 9 10 <?php $type =isset ($_GET ['type' ])?$_GET ['type' ]:'' ; switch ($type ){ case 'qq' : echo "..." ; break ; default : echo "..." ; } ?>
for 1 2 3 4 5 6 7 <?php $sum =1 ; for ($i =1 ;$i <=100 ;$i ++){ $sum *=$i ; } echo "xxx" .$sum ; ?>
while 1 2 3 4 5 6 7 8 9 10 11 <?php $num =1 ; $str ="xxx" ; while ($num <=10 ){ if ($num %2 ==0 ){ $str .=$num ."" ; } $num ++; } echo $str ; ?>
do…while 1 2 3 4 5 6 <?php $num =1 ; do { echo "xxx" ; }while ($num !=1 ); ?>
break continue 略。
字符串 定界符 1 2 3 4 5 6 <?php $i ="aaaa" ; echo <<<EOT 这行内容可被输出$i EOT ;?>
trim 1 2 3 4 5 6 <?php $keyword =" (:@_@xxx@_@:) " ; $keyword =trim ($keyword ); $keyword =ltrim ($keyword ,"(:@_@" ); $keyword =rtrim ($keyword ,"@_@:)" ); ?>
strlen 一个汉字在GBK/GB2312中占2个字节,在UTF-8/unicode中占3(或2~4)个字节。
mb_strlen
的第二个参数如果未设定则默认为内部编码,可由mb_insternal_encoding
得到。
使用mb_strlen()
时应确保在php.ini中extension=php_mbstring.dll
存在且没有被注释掉。
1 2 3 4 5 <?php $str ="啊啊啊啊xxx" ; echo "xxx" .strlen ($str ); echo "xxx" .mb_strlen ($str ,'UTF-8' ); ?>
substr 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php $str ="xxxxxxxxxxxxxxx" ; echo substr ($str ,0 ); echo "<br>" ; echo substr ($str ,4 ,14 ); echo "<br>" ; echo substr ($str ,-4 ,4 ); echo "<br>" ; echo substr ($str ,0 ,-4 ); echo "<br>" ; $str ="啊啊啊啊啊啊啊" ; echo substr ($str ,0 ,15 ,"UTF-8" ); ?>
strstr 1 2 3 4 5 6 7 <?php $string ="啊啊啊八八八八" ; echo strstr ($string ,"八" ); echo strstr ($string ,"八" ,true ); ?>
strpos 1 2 3 4 5 <?php $string ="xxxx" ; echo strpos ($string ,"xx" ,0 ); ?>
str_replace 1 2 3 4 5 <?php echo str_replace ($str2 ,$str1 ,$str ,$count ); echo "<br>" ; echo $count ; ?>
sub_replace 1 2 3 <?php echo substr_replace ($username1_phone ,$replace ,3 ,4 ); ?>
explode 1 2 3 4 5 6 <?php $array =explode (' ' ,$string ); for ($i =0 ;$i <3 ;$i ++){ echo trim ($array [$i ],'@' )."<br>" ; } ?>
implode 1 2 3 <?php $string =implode ("@" ,$str_arr ); ?>
正则表达式 基本元素 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 行定位符 ^tm 以tm开头 tm$ 以tm结尾 元字符 . 除换行符外任意字符 \w 字母、数字、_、汉字 \s 任意空白符 \d 数字 \b 单词开始或结束 ^ 字符串开始 $ 字符串结束 例如:\bmr\w*\b 限定符 ? 一次或零次 + 一次或多次 * 零次或多次 {n} n次 {n,} 最少n次 {n,m} 最少n次,最多m次 字符类、排除字符 [^a-zA-Z] 选择字符、转义字符、分组
preg_match 1 2 3 4 5 6 7 8 <?php if (preg_match ('/1[34578]\d{9}$/' ,$mobile )){ }else { } ?>
数组 数组声明 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php $array =array ("asp" ,"php" ,"jsp" ); $array =["asp" ,"php" ,"jsp" ]; print_r ($array ) echo $array [1 ]; $array =array ("1" =>"a" ,"2" =>"b" ,"3" =>"c" ,"4" =>"d" ); $array =array ("1" =>"a" ,"b" ,"c" ,"d" ); echo $array [1 ]; $newarray =array ("first" =>1 ,"second" =>2 ,"third" =>3 ); echo $newarray ["second" ]; $newarray ["third" ]=8 ; ?>
高维数组 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?php $str =array ( "xxx" =>array ( "xxx" =>array ('xxx' ,'xxx' ), "xxx" =>array ('xxx' ) ), "xxx" =>array ( "m" =>"xxx" , "n" =>"xxx" ), "xxx" =>array ( "xxx" , 8 =>"xxx" , "xxx" ) ); print_r ($str ); ?>
foreach 1 2 3 4 5 6 7 <?php $url =array ('PHP官网' =>'www.PHP.net' , 'xxx' =>'xxx' ); foreach ($url as $key =>$link ){ echo $key .":" .$link .'<br>' ; } ?>
count 1 2 3 4 <?php echo count ($array ); echo count ($array ,COUNT_RECURSIVE); ?>
array_search 1 2 3 4 5 6 7 8 <?php $key =array_search ($book_name ,$books ); if ($key ){ echo $price [$key ]; }else { echo "未知" ; } ?>
array_pop 1 2 3 4 5 6 <?php $arr =array ("ASP" ,"Java" ,"Java Web" ,"PHP" ,"VB" ); $array =array_pop ($arr ); echo "$array <br/>" ; print_r ($arr ); ?>
array_push 1 2 3 4 5 <?php $arr =array ("ASP" ,"Java" ,"Java Web" ,"PHP" ,"VB" ); array_push ($array_push ,"xxx" ,"xxx" ); print_r ($array_push ); ?>
array_unique 1 2 3 4 <?php $result =array_unique ($array ); print_r ($result ); ?>
sort 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <?php function arraySortByKey ($array =array ( ),$key ="" ,$asc =true ) { $result =array (); foreach ($array as $k =>$v ){ $values [$k ]=isset ($v [$key ])?$v [$key ]:"" ; } unset ($v ); $asc :asort ($values ):arsort ($values ); foreach ($values as $k =>$v ){ $result [$k ]=$array [$k ]; } return $result ; } $data =array ( array ('post_id' =>1 ,'title' =>'xxx' ,'reply_num' =>582 ), ); $new_array =arraySortByKey ($data ,'reply_num' ,false ); echo "<pre>" ; print_r ($new_array ); sort () rsort () asort () arsort () ksort () krsort () natsort () natcasesort () ?>
array 1 2 3 4 5 6 7 8 9 10 <?php $result =array_intersect ($brand ,$color ); array_sum () array_merge () array_diff () array_diff_assoc () array_intersect () array_intersect_assoc () ?>
面向对象 基础 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 <?php interface Mpurview { function playBasketball ( ) ; } interface Mpopedom { function showMe ( ) ; } class SportObject implements Mpurview ,Mpopedom { const BOOK_TYPE ='xxx' ; public $name ,$height ; const NAME ='aaa' ; private $aaa ='xxx' ; protected $bbb ='xxx' ; static $ccc =0 ; public function __construct ($name ,$height ) { $this ->name=$name ; $this ->height=$height ; echo SportObject ::NAME ."<br>" ; } public function playBasketball ($name ,$height ) { $this ->name=$name ; $this ->height=$height ; return "xxx" ; } abstract function showMe ( ) ; function __destruct ( ) { } } final class WeightLifting extends SportObject { public $weight ; const NAME ='bbb' ; function __construct ($name ,$weight ) { $this ->weight=$weight ; $this ->name=$name ; echo self ::NAME ."<br>" ; echo $this ->bbb; } function showMe ( ) { if ($this ->weight<85 ){ return $this ->name."xxx" ."<br>" ; }else { return "xxx" ; } } } $sport =new SportObject (); echo $sport ->playBasketball ('xxx' ,'xxx' ); echo SportObject ::BOOK_TYPE ; $weightlifting =new WeightLifting () if ($weightlifting instanceof SportObject) echo "weightlifting属于SportObject类" ; echo SportObject ::$aaa ; ?>
魔术方法 __set、__get 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <?php class Student { private $a ; private $b =0 ; public $c ; public $d =0 ; public function __get ($name ) { return 123 ; } public function __set ($name ,$value ) { echo "this is a set function" ; } } $s =new Student (); var_dump ($s ->a); var_dump ($s ->b); var_dump ($s ->c); var_dump ($s ->d); var_dump ($s ->e); $s ->a=3 ; $s ->c=3 ; $s ->f=3 ; ?>
__call 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php class SportObject { public function myDream ( ) { } public function __call ($method ,$parameter ) { echo $method ; print_r ($parameter ); } } $exam =new SportObject (); $exam ->myDream (); $exam ->mDream ('how' ,'what' ,'why' ); ?>
__toString 1 2 3 4 5 6 7 8 9 10 <?php class SportObject { private $type ='DIY' ; public function __toString ( ) { return $this ->type; } } $myComputer =new SportObject (); echo $myComputer ; ?>
__autoload 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?php require ('A.php' ); $a =new A (); function __autoload ($class_name ) { $class_path =$class_name .'.php' ; if (file_exitsts ($class_path )){ include_once ($class_path ); }else { echo "xxx" ; } } $myBook =new StudyObject (); ?>
Web交互 POST表单提交 1 <form method ="POST" action ="*.php" >
PHP操作MySQL 基础操作 1 2 3 4 5 6 7 8 9 <?php $host ="localhost" ; $userName ="root" ; $password ="root" ; $dbName ="database9" ; $link =mysqli_connect ($host ,$userName ,$password ,$dbName ) or die ("连接失败" .mysqli_error ()); $result =mysqli_query ($link ,"insert into tb_member values('mrsoft','123','mrsoft@mrsoft.com)'" ); ?>
数据读取 1 2 3 4 5 6 <?php $link =mysqli_connect ("localhost" ,"root" ,"root" ,"database9" ) or die (mysqli_error ()); mysqli_query ($link ,"set names utf8" ); $result =mysqli_query ($link ,"select * from books" ); include_once ("list.html" ); ?>
list.html:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 <!DOCTYPE html > <html lang ="en" class ="is-centered is-bold" > <head > <meta charset ="UTF-8" > <title > 标题</title > <link href ="css/bootstrap.css" rel ="stylesheet" > <script src ="js/jquery.min.js" > </script > </head > <body > <div class ="container" > <div class ="col-sm-offset-2 col-sm-8" > <div class ="panel panel-default" > <div class ="panel-heading" > 图书列表</div > </div > <div class ="panel-body" > <table class ="table table-striped task-table" > <thead > <tr > <th > ID</th > <th > 图书名称</th > <th > 分类</th > <th > 价格</th > <th > 出版日期</th > <th > 操作</th > </tr > </thead > <tbody > <?php while($rows=mysqli_fetch_array($result,MYSQLI_ASSOC)){ ?> <tr > <td class ="table-text" > <?php echo $rows['id']?> </td > <tb class ="tabel-text" > <?php echo $rows['name'] ?> </tb > <tb class ="table-text" > <?php echo $rows['category'] ?> </tb > <tb class ="table-text" > <?php echo $rows['price'] ?> </tb > <tb > <?php echo $rows['publish_time']?> </tb > <td > <a href ='editBook.php?id=<?php echo $rows[' id ']?> '> <button class ="btn btn-info edit" > 编辑</button > </a > <a href ='deleeteBook.php?id=<?php echo $rows[' id ']?> '> <button class ="btn btn-dangeer delete" > 删除</button > </a > </td > </tr > <?php } ?> </tbody > </table > </div > </div > </div > </body > </html >
Cookie与Session Cookie设置 1 2 3 4 5 6 7 8 9 10 <?php date_default_timezone_set ('PRC' ); if (!isset ($_COOKIE ["visittime" ])){ setcookie ("visittime" ,date ("Y-m-d H:i:s" )); }else { setcookie ("visittime" ,date ("Y-m-d H:i:s" ),time ()+60 ); echo $_COOKIE ["visittime" ]; } setcookie ("xxx" ,"" ,time ()-1 ); ?>
Session设置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?php $path ='./tmp/' ; session_save_path ($path ); session_cache_limiter ('private' ); $cache_limiter =session_cache_limiter (); session_cache_expire (10 ); $cache_expire =session_cache_expire (); session_start (); $_SESSION ['username' ]='xxx' ; if (!empty ($_SESSION ['username' ])){ $userName =$_SESSION ['username' ]; } unset ($_SESSION ['username' ]); $_SESSION =array (); session_destroy (); ?>
本章暂时搁置。