After studying PHP for so long, the basic knowledge always feels unreliable, especially the application of arrays and string functions. However, these basic knowledge are very important, so this article will share some common functions of PHP. I hope it can help you.
one. PHP basic syntax
Variables, constants
Strictly case-sensitive, but built-in structures or keywords do not matter (echo)
Naming: cannot use numbers, spaces, . to start with, but can have Chinese characters, eg: $variable="aa";
Variable variable: a=′aa′;
a="bb"; then
aa="bb";
Reference assignment: a="aa" ;
b=&a; then changechange The value of
a, b also changes to . Not the same as : SaveStorageSTRUCTURESTRUCTURE is 分开的,是使unset(a), $b is still there (distinguish C language) Variable type: int str array bool object float resource null
Global variables: Global variables themselves are static storage methods, and all global variables are static variables
_SESSION,
_COOKIE, _POST,
##_GET, REQUEST
,
_FILES,$_EVN Static variables:
static [See the picture below for details]
Static methods: Static methods do not require the class to be instantiated to be used directly use. Math::Max(
a
,
b); (the static method Max is directly called without instantiating the Math class) Constant:
# PHP_OS=”WINDOWS”; PHP_VERSION=”version”; Error
Magic constants: [All return physical paths. Even if included in the output, the output is the source code information, not the information of the currently included file, which is distinguished from $_SERVER]
__FILE__ Current文件名称
__CLASS__ 当前类名称
__FUNCTION__ 当前函数名称
__METHOD__ 当前方法名称
__LINE__ 当前行数名称

Summary: 
(Global) constants: (Default constants are global) stored in the (static) data segment
Variables
Global variables: stored in the static data segment
Local variables: stored in the stack
Static variables: (regardless of global/local) stored in static data Section
Type conversion
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 | 1.setType( $a );
2.b=(int)
a;
b= intval (
a);
3. is_int (b);
b是否为整形,返回bool值
类型转换:(int),(bool),(float),(),(string),(),( array ),(),(object);
intval (), floatval (); strval ()
判断类型:
is_bool /int/float/string/ array /object/resource/null
is_numberic();’
is_callable ();
运算符
算数运算符:+ - * / % ++ –
连接运算符: .
赋值运算符: =, +=,-=,*=,/=,%=,.=
比较运算符:>,<,==,===,!= <>,!==
逻辑运算符: and ,&& ; or ,|| ; not,! ; xor (逻辑异或,两边不同返回TRUE,相同返回FALSE)
位运算符 : & ;|;^(异或,不同返回1);~(非运算符,1.0取反);<<左移,右边空出的补0;>>右移左边空出的补0
其他运算符:
?: 三目运算符 举例:a=
bool?b:
c;
a=b;否则
a= $c
@ 忽略错误
=>数组下标用
->调用对象值用
·· 反引号为执行运算符??
instanceof 类型运算符 class ClassOne{} a= new ClassOne(); var_dump(
a instanceof ClassOne );
|
Copy after login
Process control
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 1. if (){} else {}
2. while (){};
3. do {} while ();$
4. for (a=1;
a<10; $a ++){}
5. switch ( $a ){
case 1: echo 1; break ;
case 2: echo 2; break ;
default : echo “this is defaut value”;
}
continue , break ; exit 区别:
continue 跳过当前循环,循环还在继续
break 跳出当前循环,循环终止
exit ; 终止当前脚本,这行代码后边的代码不执行了就
|
Copy after login
<br/>Function
Naming: Just follow the variable naming rules, and the function cannot be repeated Load,
Function: The function implements structured programming and improves the maintainability of the code
Global variables: Can be used in the entire script
Local variables: only used within the function body, automatically released after the function is executed
|—> are divided into static storage types and dynamic storage types; static $a is a static variable. After the function is executed, it will not be Release
Local variables within functions Program global variables: 1.global; 2.$GLOBAL[”]Use global array
Several types of functions
1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <strong>.引用参数的函数: function (&a)
a的操作,会对实参也造成影响 eg:sort()
2.默认参数的函数: function (a=0)
a=0
3.可变参数个数的函数: function (a,
b $c ,…){}
4.回调函数:fun= "one" ; function one(){} 当调用
fun()的时候,就是再调用one()函数,call_user_func_array()
5.递归函数: function test(){ test()}
|
Copy after login
二. Commonly used functions in PHP
Commonly used functions: echo()[Language structure]
print()//[Language structure][with return value], if The transmission failed and resulted in no output, it returned false
var_dump()
<br/> var_export()【With return value, translate a legal PHP code】
printf( )//Similar to the C language form printf(“my name is %s, age %d”,
n
ame,age);, print it out sprintf()//Similar to printf, but does not print. It returns formatted text, and the others are the same as printf
Array function:<br/>
1. Sorting class: [1. No return value, pass-by-value reference, The original array was modified directly】
Press V: sort, rsort, asort, arsort,
Press K: ksort, krsort
Press letters:
1 2 3 4 5 | natsort();
natcasesort();
当遇到字符完全一样,按照数字排 eg: FILE1,FILE2, 这两个字符相同,再按照数字1<2排,所以结果 FILE1,FILE2
回调:usort(arr, "strnatcmp" )</span>
|
Copy after login
Rules:
If there is no "k", the sorting is according to [value]. The sorting with "a" means that the KEY should be retained, and the sorting with "r" in reverse order
There is " k", sorted according to [KEY], those with "r" are sorted in reverse order
Those with "u" indicate that they will be thrown into the callback function for processing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 2.键值操作类:【都有返回值,没有在原来参数上修改】
1. array_values (
arr);</span><span style= "font-size:14px;font-family:'Microsoft YaHei', STXihei !important;" >
2. array_keys (
arr[, "str" ,true])
3. array_search (“is”,
arr[,true]</span><span style= "background-color:inherit;line-height:1.5;color:rgb(51,51,51);font-family:Monaco, Consolas, Courier, 'Lucida Console', monospace;" ><span style= "color:rgb(0,0,0);font-family:'Microsoft YaHei', STXihei !important;" >) </span><span style= "color:rgb(0,0,0);font-weight:normal;font-family:'Microsoft YaHei', STXihei !important;" >
)
4.in_array(“str”,arr);
arr中是否存在,【返回BOOL】
5. is_array ( $arr );
6. array_key_exists (key,
arr);
key,【返回BOOL】
7. array_flip ( $arr );
8. array_reverse ( $arr ,[true|false]);
9.array_column( array (),’name’[,name_two]) — 返回数组中指定的name列[可选参数,如果有返回name=>name_two的形式]【返回一维数组】
|
Copy after login
3. Number of elements And uniqueness
1.array_unique(
arr);//Remove the duplicate values in
arr, retain the first value, [return array, key Value retention】 2.array_count_values($arr)//Count the number of times the array value appears, [return the array, KEY is the value of the original array, VALUE is the number of counts]
3.count(arr[,1])/sizeof();//统计
arr的元素个数,参数”1”表示统计多维数组开启,默认0为关闭【返回统计个数】
4.回调函数
1 2 3 4 5 6 7 8 9 10 | 1. array_filter (arr, "function" );
arr放到函数 function 中处理,【返回判断为TRUE的数据组成新数组,键值保留】
2. array_walk (arr, "function" [, "data" ]);
arr放到 function (&v,
k, $data )中处理【返回值为bool】
3. array_map (“ function ”,arr,
arr2, $arr3 ,….);
4. array_reduce (arr,myfunction[,initial]):把一维数组
arr中的值依次传到自定义函数myfunction(v1,
v2)的v2上,v1为累加值类似于( .= ),[如果有initial,先把其当v1传进去]【返回字符串】
|
Copy after login
5.拆分,合并,分解,接合数组
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 | 1. array_slice (arr,1[,2]);
arr中,从第二个开始取[,返回俩个值]【返回新数组(对原数组无影响),键值保留】
2. array_splice (arr,1[,2, "aaa" , "bb" ]);
删除或替换,从arr第二个开始取,删除或替换2个值【返回值为新数组,拆掉原数组】
3. array_combine (arr1,
arr2);
arr2为VALUES结合形成新索引数组【返回索引数组】
4. array_merge (arr1,
arr2, $arr3 …);
array_merge 发现有key值相同的,取后者;
arr1+
arr2 发现有key值相同的,取前者,第二个重复的值丢弃
5. array_intersect (arr1,
arr2)
6. array_diff (arr1,
arr2)
7. array_chunk (arr,2)
arr按照【2个为一组】均等分割【返回一个二维数组】
6.数组的数据结构【2.无返回值,传值引用,就直接对原数组进行了修改】
1. array_shift ( $arr )
2. array_unshift ( $arr ,”one”,”two”)
3. array_pop ( $arr )
4. array_push ( $arr ,”aaa”);
6.current( $arr )
7.key( $arr )
8.next( $arr )
9.pre( $arr )
10. end ( $arr )
11.reset( $arr )
12.unset( $arr )
|
Copy after login
7.其他
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | 1. array_rand (arr,2);
arr数组当中的key【返回值为值或者数组】
2.shuffle(arr)
arr,顺序变乱【返回新数组,保留键值】
3. array_sum (arr);
arr的value的和【返回一个值】
4.range(0,10,2,)
5.http_build_query( $arr )
字符串函数
返回bool
1.isset(a)
a=NULL 或不存在,返回false,反之为true
2. empty (a)
a=NULL/”/ array ()/0/’0’/不存在 时 返回true,反之为false
返回值为自字符
1. substr (字符串,开始地方,[返回字符串的长度]);
2.substr_replace(str, "aaa" ,start[,length]);在
str上操作,从第start个开始,把【后边】的字符[全/或length个]替换
3.sub_count( $str ,”is”[,5,10]);
4. strstr ( $email ,”@”[,true])
5. strrchr ( $email ,”@”)
6. str_replace (被替换词,替换词,被搜索字符串,[统计替换次数 $num ]) 前两个参数也可为数组,两个数组元素个数相同
7. str_repeat (str,num);
str字符串 num次,
返回值为数字类的
1. strpos ( $str ,”@”);
2. strrpos ( $str ,”@”);
3. str_word_count ( $str [,0/1/2]);
4. strcmp (str1,
str2);
5. strnatcmp (str1,
str2);
strcasecmp (str1,
str2); strnatcasecmp
str2比较,【返回值:相等0,小于-1,大于1】
6. number_format(10000[,2]);
7. strlen (string);<span style= "color:rgb(69,69,69);font-family:arial, '宋体', sans-serif, tahoma, 'Microsoft YaHei';" >成功则返回字符串
string 的长度
8. mb_strlen(string,'utf8');
string长度,多字节的字符被计为 1。
文本处理类
1. strtoupper ( $str )
2. strtolower ()
3. strtotime (‘2015-10-10 10:10:10’);
4. str_pad ( $str ,length,[—,STR_PAD_BOTH]);
“---“,注意,length若小于str长度,不填补
5.trim( $str [,”a”,STR_PAD_BOTH])
6. floatval (“123.45aa”)
7.ucfirst()
8.ucword()
HTML类处理
1.htmlspecialchars( $str )
htmlspecialchars_decode( $str );
2.htmlentities( $str );,函数把【预定义字符】转换为【 HTML 实体】,&转换成&,有乱码问题,注意第二第三个参数,若编码不正确,会在实体化时把信息丢失
html_entity_decode( $str )
3. addslashes ( $html );
stripslashes ( $html );
4. strip_tags ( $html );
5. nl2br (str)
str中的换行/n前插入<br>,因为\n在源码可以换行,但是在浏览器窗口不行,有这个就可以
6.iconv( from_charset ; to_charset,str);
file_name = iconv( "gb2312" , "utf-8" , $file_name );
|
Copy after login
正则函数[原则,能用字符串函数解决不用正则,速度问题]<br/>
字符串的匹配查找
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | 1.preg_match(pattern,
subject,arr);</span>
2.preg_match_all(pattern,
subject,arr</span><span style= "background-color:inherit;font-weight:normal;" ><span style= "background-color:#FFA500;" >)</span>
)
3. strstr ( $str ,”@”[,true]);
4. strpos , strrpos , substr ( $str ,position)
字符串的替换
1.preg_replace(pattenr,
replace, $str );
在str中,把
parrern匹配的值替换成 $replcae 【返回值为处理后的字符串】
2. str_replace (str, "aaa" , "bbb" );
str中的aaa换成bbb
字符串的分割和链接
1.preg_split(pattern,
str);通过一个正则表达式分隔字符串【返回值为数组】
举例: $keywords = preg_split(“/[\s,]+/”, “hypertext language, programming”);
结果Array([0] => hypertext,[1] => language[2] => programming)
2. explode (“,”,str[,
limit_num]);
3.impolde(“+”,arr);
arr里的元素按照“+”链接成一个字符串
文件处理函数
[ $file =c:
基础函数
1. file_exists ( $file )
2. filesize ( $file )
3.is_readale( $file )
4. is_writeable ( $file )
5. is_executable ( $file )
6. filectime ( $file )
7. filemtime ( $file )
8. fileatime ( $file )
9.stat( $file )
目录的基本操作
1. basename ( $file )
2.dirname( $file )
3. pathinfo ( $file )
[“dirname”目录名] [“ basename ”文件名] [“extension”文件后缀]
目录复制,删除,统计大小使用的总结:
本质:依靠递归思想,对目录的的循环遍历,通过每一个文件的操作,得出结果
函数:
复制: copy (org,
to); mkdir ()
删除:unlink( $file );mrdir();
统计大小: filesize ( $file )
遍历目录
1.opendir(file)
dir_handle,无权限返false】
2.readdir( $dir_handle );
3. closedir ( $dir_handle )
4. rewinddir ( $dir_handle )
|
function
menu_list(
$root_path){
$num=0;//存储条数
$dir_handle=(
建立和删除目录Copy after login
1 2 3 | <em> 1. mkdir (“dir_name”);
2. rmdir (“dir_name”);
unlink(“file_name”)
|
Copy after login
1 2 3 4 5 | <em>
<br style= "background-color:inherit;" ></span><span style= "background-color:inherit;color:rgb(249,38,114);" > function </span><span style= "background-color:inherit;color:rgb(166,226,46);" >del_dir</span>(<span style= "background-color:inherit;color:rgb(253,151,31);" ><em> $root_path ){
if ((); exit ;</em>
|
Copy after login
1 2 3 4 5 6 7 8 9 | <em> $dir_handle <span style= "background-color:inherit;color:rgb(249,38,114);" >=</span><span style= "background-color:inherit;color:rgb(102,217,239);" ><em>opendir</em></span>(<span style= "background-color:inherit;color:rgb(253,151,31);" ><em> $root_path );
while ( $cur_name <span style= "background-color:inherit;color:rgb(249,38,114);" >=</span><span style= "background-color:inherit;color:rgb(102,217,239);" ><em>readdir</em></span>( $dir_handle )){
if ( $cur_name <span style= "background-color:inherit;color:rgb(249,38,114);" >==</span><span style= "background-color:inherit;color:rgb(230,219,116);" > "." </span><span style= "background-color:inherit;color:rgb(249,38,114);" >|| </span> $cur_name == ".." ) continue ;
$cur_path <span style= "background-color:inherit;color:rgb(249,38,114);" >=</span><span style= "background-color:inherit;color:rgb(253,151,31);" ><em> $root_path . "/" . $cur_name ;<br style= "background-color:inherit;" ><span style= "background-color:inherit;color:rgb(249,38,114);" > if </span>(<span style= "background-color:inherit;color:rgb(102,217,239);" ><em> is_file </em></span>( $cur_path )){
del_file( $cur_path );<br style= "background-color:inherit;" > }<span style= "background-color:inherit;color:rgb(249,38,114);" > else </span>{<br style= "background-color:inherit;" ><span style= "background-color:inherit;color:rgb(117,113,94);" >
(){
return ();
}</em>
|
Copy after login
统计目录大小【得自定义函数,不自带】
相关推荐:
180多个PHP常用函数总结
php常用函数总结表
php常用函数和函数库总结
The above is the detailed content of Share some common functions of php. For more information, please follow other related articles on the PHP Chinese website!