PHP - PHP file manipulation and command execution function -PHP

First, the function.

  • definition:
  • By custom function code is encapsulated into a group, so that the code can be reused, so that the logic program structure more clearly. The same function used multiple times, using a function easy to operate.
  • Naming rules:
  • Naming function can not be repeated and the existing function names and function names can only consist of letters, numbers, underscores, but the function name can not start with a number.
  • Use function () function is created:
  • Example: function name () {echo "John Doe";}
  •         call function:
  •                             name();

1, function parameters:

  • Information may be passed through the parameter is the parameter list to the list of the function expression (as a comma character). Is similar to a variable parameter, you can realize the function of calculating the parameters by passing parameters.
  • Example:
  •          <?php
  •                           function square($a){
  •                           echo $ a * $ a;
  •                }
  •                           square(2);
  •                ?>

2, the return value of the function:

  • Return value by using the optional return statement, return may comprise any type of arrays and objects. return return statement will immediately terminate the function, and the control back to the line of code calls the function. If omitted, the return, the return value is NULL.
  • Do not write the entire function call return, the return value after dispatching function runs.
  • Example:
  •        <?php
  •                        function square($a){
  •                        return $a*$a;
  •                        }
  •                         square(2);
  •     ?>

3, built-in functions.

PHP language comes with a lot of functions. Such as:

  • isset()
  • Whether the variable exists.
  • strlen()
  • 计算字符串的长度。
  • var_dump()
  • 输出变量类型和具体内容。

  • mysql_connect()

  • 链接数据库。
  • function_exists()

  • 判断函数是否存在。

二、字符串函数。

  • eval()   
  • 将字符串当作命令执行
  • strlen()  mb_strlen() 
  • 长度计算,  区别:mb_strlen可以计算中文字符串长度
  • addslashes()
  • 将预定义的字符添加反斜杠转义
  • bin2hex()
  • 将2进制字符串转换为16进制
  • hex2bin()
  • 将16进制字符串转换为2进制
  • chop()
  • 移除字符串右侧的空白字符或者其他字符
  • 参数1:在哪个字符串 
  • 参数2:指定移除的内容(不指定默认移除空白字符)
  • chr()
  • 将指定的ascii值转换成字符
  • htmlentities()
  • 将字符串转换为html实体
  • implode()
  • 返回一个由数组元素组合成的字符串  与explode()相反作用
  • 参数1:数组 
  • 参数2:数组分隔符(一般不指定,不指定默认空格)
  • explode()
  • 把字符串以分隔符打散为数组。
  • 参数1:在哪里分割 
  • 参数2:分割的字符串
  • str_split()
  • 把字符串分割到数组中  php4\5\7均支持 ,但是无法按照指定的字符来做分割,split函数可以按照指定的字符来分割,与explode函数用法类似,split函数仅仅可以在php4/5中使用,PHP7废除了这个用法。
  • md5()
  • 计算字符串的md5值
  • md5_file()
  • 计算文件的md5值
  • crypt()
  • 对字符串单项加密,不可逆转,类似于做md5值
  • convert_uuencode()
  • uu编码
  • convert_uudecode()
  • uu解码
  • urlencode() urldecode()
  • url编码/解码
  • 此函数便于将字符串编码并将其用于 URL 的请求部分
  • base_64_encode()   base_64_decode()       
  • base64编码、解码
  • str_replace()
  • 替换字符串中的一部分(大小写敏感)       str_ireplace() 大小写不敏感
  • 参数1:要替换谁 
  • 参数2:.替换成
  • 参数3:.替换哪个字符串
  • strcmp
  • 比较两个字符串(大小写敏感)
  • 参数1,2分别写两个字符串
  • strcasecmp()
  • 比较两个字符串(大小写不敏感)
  • 参数1,2分别写两个字符串
  • strstr()
  • 查找字符串在另一个字符串中第一次出现的位置(大小写敏感)并返回该字符串开始的后面内容
  • 参数1:在哪个字符串查  
  • 参数2:查谁
  • strpos()
  • 查找字符串在另一个字符串中第一次出现的位置(大小写敏感)并返回该字符串开始的后面内容,同strstr
  • 参数1:在哪个字符串查  
  • 参数2:查谁
  • strripos()
  • 从右向左查找字符串在另一个字符串中第一次出现的位置(大小写敏感)并返回该字符串开始的后面内容
  • 参数1:在哪个字符串查  
  • 参数2:查谁
  • substr()
  • 返回字符串中的一部分
  • 参数1:从哪个字符串 
  • 参数2:第几位起开始返回  
  • 参数3:返回的长度
  • 对于字符串替换操作,用得比较多的,会加上一个正则的操作。
  • preg_match() 与preg_match_all()

三、常用数组函数。

  • count()
  • 获取数组的长度    
  • sizeof()
  • 统计数组下标的个数
  • each() 
  • 返回当前数组的键名和键值,并将内部指针向前移动
  • list()
  • 用于在一次操作中给一组变量赋值
  •   例:   
  •         $a=[1,23,1];
  •         list($b,$c,$d)=$a;
  • sort()、asort()和 ksort() 
  • 数组元素正向排序        
  • rsort()、arsort()和 krsort()
  • 数组元素反向排序
  • array_count_values()
  • 统计数组内下标值的个数
  • current():
  • 每个数组都有一个内部指针指向他的当前单元,初始指向插入到数组中的第一个元素
  • array_pad():
  • 数组首尾选择性追加值,+尾部追加,-首部增加
  • 参数1:哪个数组 
  • 参数2:追加后的长度 
  • 参数3:追加的内容
  • unset() 
  • 用于销毁指定的变量
  • array_fill()  
  • 用键值填充数组
  •     参数1:从哪个索引值开始 
  •     参数2:填充的数量 
  •     参数3:填充内容
  • array_combine() 
  • 通过合并两个数组来创建一个新数组,其中的一个数组元素为键值,另一个数组元素为数值
  •     参数1,2分别为两个数组(1是键值,2 是数值)
  • array_splice()
  • 删除数组成员并替换
  • 例:
  •    array_splice($a,1,2,array("p","p"));     删除数组$a第二个数起的的两个数,并替换为p
  • array_unique()
  • 删除数组中的重复值
  • array_flip()
  • 交换数组的键值和值
  • array_search()
  • 搜索数值,并返回键名
  •     参数1:数值 
  •     参数2:哪个数组

四、其他常用函数。

  • isset()
  •  判断是否有值

 

发布了36 篇原创文章 · 获赞 130 · 访问量 2073

Guess you like

Origin blog.csdn.net/cldimd/article/details/104918185