賦值運算
$a++; 先賦值在加加
$a=0;
$b=$a++;
echo $b //顯示0
++$a 先加加載賦值
$a=0;
$b=++$a;
echo $b//顯示1
邏輯運算符
xor 異或 相同為false 不同為true
$a=true;
$b=false;
$res= ($a xor $b);
var_dump($res) //顯示bool(true)
比較運算符
<>不等于
==全等于(兩個比較內容里,類型也要比較);!==全不等;
Math函數
$num=mt_rand(1,10);//隨機1到100的隨機數
$num=mt_pow(x,y);// 返回x的Y的次方;
三元運算
$res= $num>5? "{$num}大于5": $num;
echo $res;
運算符優先級
循環語句
<?
switch(表達式){
case :
執行語句;
break;
dafault:
break;
};
$num=5;
while($num>5){
echo "{$num}大于5";
}; // 不顯示 先判斷 在執行;
do{
echo "{$num}大于5";
}while($num>5); //顯示5大于5 先執行在判斷
foreach 循環
$arr=array("姓名"=>"張燦","年齡"=>"18","身高"=>"178cm");
foreach( $arr as $k => $v ){
echo $v."---------".$arr[$k]."<br>";
echo $k.":".$v."<br>";
if($k=="姓名"){
$v="李四";
};
};
顯示
張燦---------張燦
姓名:張燦
18---------18
年齡:18
178cm---------178cm
身高:178cm
$num=0;
while($num<10){
$num++;
if($num==3){
continue; //跳出本次循環
}else if($num>4){
break; //阻止全部循環
}else{
echo $num;
};
}; //顯示 124
?>
函數
PHP中函數名不區分大小寫
function test(){
echo "我是test函數";
};
TEST();
函數參數默認值
function test($a="你好",$b="世界"){
echo $a.$b;
};
test(); // 顯示 你好世界 ;不傳參數時候使用默認值 ,傳參時候不適用默認值
test("hello","word");// 顯示 helloWord;
函數傳值和傳址
$c=5;
function test($a){ //這樣是傳值
$a=10;
};
test($c);
echo $c; //顯示5
$c=5;
gunction test(&$a){ //這樣是傳址
$a=10;
};
test($c);
echo $c; // 顯示 10
函數的作用域
$a=10;
$fn= function ($b){
echo $b; //函數里邊想得到$a 要傳進去 或者global 聲明全局變量 函數里邊的作用域拿不到外面的變量
};
$fn($a); // 不支持 多個函數名重載 和關鍵字; 匿名函數結尾要加;號
遞歸
function test($num){
$num--;
echo $num;
if($num>0){
test($num);
}else{
return; //可以阻止程序執行
};
};
test(5); // 顯示43210 ;
靜態變量
靜態變量 記住上一次的值 常駐內存 static 聲明靜態變量 把值保存在內存
<?
function test(){
static $num=5;
$num--;
echo $num;
if($num>0){
test();
}else{
unset($num); //unset()銷毀靜態變量
return;
};
};
test(); //顯示43210
?>
數組排序
注意:排序是按照按照ascll碼來排序的
1.數組的操作
unset($arr[0])刪除數組某個元素
print_r($arr); 打印數組
count($arr)取得數組大小
in_array(10,$arr); 檢查數組中包含某個值
1.根據值來排序
<?
值正序排列 sort()
$arr3[]="c";
$arr3[]="a";
$arr3[]="b";
$arr3[]="e";
print_r($arr3);
echo "<br>";
sort($arr3);
print_r($arr3);
顯示 Array ( [0] => c [1] => a [2] => b [3] => e )
Array ( [0] => a [1] => b [2] => c [3] => e )
值反序排列 rsort()
$arr3[]="c";
$arr3[]="a";
$arr3[]="b";
$arr3[]="e";
print_r($arr3);
echo "<br>";
rsort($arr3);
print_r($arr3);
顯示
Array ( [0] => c [1] => a [2] => b [3] => e )
Array ( [0] => e [1] => c [2] => b [3] => a )
?>
2.根據下標來排序
下標正序排序 ksort();
<?
$arr3["B"]="你好";
$arr3["A"]="荷花";
$arr3["a"]="hello";
$arr3["c"]="word";
print_r($arr3);
echo "<br>";
ksort($arr3);
print_r($arr3);
顯示
Array ( [B] => 你好 [A] => 荷花 [a] => hello [c] => word )
Array ( [A] => 荷花 [B] => 你好 [a] => hello [c] => word )
下標反序排列 krsort();
$arr3["B"]="你好";
$arr3["A"]="荷花";
$arr3["a"]="hello";
$arr3["c"]="word";
print_r($arr3);
echo "<br>";
krsort($arr3);
print_r($arr3);
顯示
Array ( [B] => 你好 [A] => 荷花 [a] => hello [c] => word )
Array ( [c] => word [a] => hello [B] => 你好 [A] => 荷花 )
?>
字符串
1.統計字符串的長度
<?
$a="你好";
$res= strlen($a);
echo $res; //顯示6
顯示多個字符串
echo "hello","wrod"; //顯示hellowrod
?>
2.字符串變大小寫
<?
變小寫 strtolower()
$str="fdadsadaasdASDADSASDADASDs";
$res=strtolower($str);
echo $res; //顯示 fdadsadaasdasdadsasdadasds
-------------------------------------------------------------------------
變大寫 strtoupper()
$str="fdadsadaasdASDADSASDADASDs";
$res=strtoupper($str);
echo $res; //顯示 FDADSADAASDASDADSASDADASDS
?>
3.查詢需要查詢內容第一次出現的位置
只針對字符串 一個空格占一個字符 區分大小寫 strpos()
$res= strpos("hello wordor","or" );
echo $res; 顯示7
函數中有 i 是否對大小寫敏感 不區分大小寫 stripos()
$res= stripos("hello wordor","OR" );
echo $res; 顯示 7
4.要替換的字符串 str_replace()
$str="asdweedcdf--dafsdds";
$res= str_replace("--","**",$str); $res= str_ireplace("--","**",$str) 帶i的只是不區分大小寫
echo $res; 顯示 asdweedcdf**dafsdds
5.字符串截取
substr()
strstr() 根據字符串來獲取前、后字符串
截取的時候,截取的長度小于字符串長度對亂碼,針對中文;
$str="你好";
第一個參數為對象
第二個參數從什么位置開始截取 第三個參數截取的位數
$res=substr($str,0,2);/ $res=substr($str,0,6);
echo $res;
顯示 亂碼 / 你好
$str="erqdasffffrefsdsasdvs";
$res= strstr($str,"ffff",false); //默認false 取后面的字符串 true取前面的字符串
stristr() 不區分大小寫
echo $res; 顯示ffffrefsdsasdvs
6.刪除系統預定義的字符串
$str=" dasdadsadas ";
echo "(".$str.")<br>";
$res= ltrim($str); // 去掉左邊的字符串 左邊的空格
$res= rtrim($str); // 去掉右邊的字符串
echo "(".$res.")";
$res= trim($str); // 去掉全部空格
7.字符串倒敘排列
strrev()
$str= "faadafdf";
$res= strrev($str);
echo $res;
顯示 fdfadaaf
8.將換行符轉化為HTML標簽
$str= "fdaa
asdasd
asda";
$res = nl2br($str); //轉化為<br>標簽
echo $res;
9.去掉字符串里的標簽
strip_tags()
$str="<h1>我是H1標簽</h1>";
echo $str."<br>";
$res=strip_tags($str);
echo $res;
顯示
<h1>我是H1標簽<h1>
<h6 >我是H1標簽<h6>
10.把預定義的字符轉化成HTML實體
用于評論區的留言
$str="<h1>我是H1標簽</h1>";
$res=htmlspecialchars($str); //把預定義的字符轉換為 HTML 實體。
echo $res;
顯示
<h1>我是H1標簽</h1>
預定義字符
預定義的字符是:
& (和號)成為 &
" (雙引號)成為 "
' (單引號)成為 '
< (小于)成為 <
(大于)成為 >
11.字符串的割接 和 拼接
explode()割接
implode()拼接
$str="adsdasdas-dfdssfadsf";
$arr= explode("-",$str); // 切割成數組 以 - 切割成數組
print_r($arr);
顯示
Array ( [0] => adsdasdas [1] => dfdssfadsf )
--------------------------------------------------------------------
$newstr=implode("*",$arr);// 將數組拼接成字符串 根據給定字符來拼接
echo $newstr;
顯示
adsdasdas*dfdssfadsf
數據轉化成JSON
json_encode() 編碼
json_decode() 解碼
解碼---將JSON數據解析成數組或者對象 第二個參數為true 轉化成數組 默認false 轉換成對象
關聯數組會轉換成對象 默認
索引數組會轉換成數組 默認
$arr=["張三","李四","王二","hehe","wrod"];
$arr=["name"=>"張三","age"=>"18"];
$str=json_encode($arr); //編碼
echo $str."<hr>";
顯示 {"name":"\u5f20\u4e09","age":"18"}
$obj=json_decode($str,true); //解碼
print_r($obj);
print_r($obj->name); //調用方法
顯示 Array ( [name] => 張三 [age] => 18 )