本文主要和大家分享PHP基础函数实例详解,希望能帮助到大家。
1、 有关输出的函数:
l print():类似于echo输出提供的内容,返回1
l print_r() 一般用于打印数组,不打印类型
l var_dump():打印数据的值,还有类型
2、有关时间的函数
l date():显示日期时间
举例:date(“Y-m-dH:i:s”)
l time():时间戳(秒数)
l microtime():返回时间戳和微秒数
l strtotime():将日期时间的字符串解析成时间戳
3、数学相关函数:
l max($v1,$v2,$v3…) 求最大值
l min($v1,$v2,$v3…) 求最小值
l rand()随机数
l mt_rand()生成更好的随机数
l round(变量 [,N]) 保留n位小
l ceil() 向上取整
l floor() 向下取整
l pow(x,y) 求xY
l abs()绝对值
l sqrt():求平方根
4、有关函数的函数
l function_exists( ‘函数名’) 判断函数是否存在
l func_get_arg(下标) 获取指定下标对应的参数
l func_get_args():在自定义函数中获取所有的参数(返回值是一个数组)
l func_num_args():获取当前自定义函数的参数数量
5、字符串相关的函数
基本函数strlen():得到字符串的长度 mb_strlen($字符串变量名 ,字符编码)
l implode(连接方式,数组):连接成一个字符串
l explode(分割字符,目标字符串):变成数组
l str_split(字符串,字符长度):按照指定长度拆分字符串得到数组
l trim(字符串[,指定字符]): 默认是用来去除字符串两边的空格
l ltrim():去除左边的
l rtrim():去除右边的
l substr(字符串,起始位置从0开始[,长度])
l strstr(字符串,匹配字符): 从指定位置开始,截取到最后(可以用来去文件后缀名)
l strtolower():全部小写
l strtoupper():全部大写
l ucfirst():首字母大写
l strpos(字符串,匹配字符):判断字符在目标字符串中出现的位置(首次)
l strrpos(字符串,匹配字符)
l str_replace(匹配目标,替换的内容,字符串本身)
l printf/sprintf(输出字符串有占位符,顺序占位内容..)%d %s
l str_repeat():重复某个字符串n次
l str_shuffle():随机打乱字符串
6、数组相关的函数
数组排序的函数
l sort():按元素的值 升序排列(下标重排)
l rsort():按元素的值降序排列(下标重排)
l asort():按元素的值升序排列(下标保留)
l arsort():按元素的值降序排列(下标保留)
l ksort():数组按键名(下标) 升序排列,(下标保留)
l krsort():按键名降序排列,(下标保留)
l shuffle():随机打乱数组元素,数组下标会重排
l 排序:默认的是升序排列,有 r就是降序排列)
l 有a:就是保留索引关系
l 有k : 就是按照键名排序
指针
l reset() 重置指针,将数组指针回到首位
l end() 重置指针,将数组指针指到最后一个元素
l next() 指针下移,取得下一个元素的值
l prev() 指针上移,取得上一个元素的值
l current():获取当前指针对应的元素值 (不移动指针)
l key(): 获取当前指针对应的下标值(不移动指针)
数组其他函数
l count():统计数组中元素的数量
l array_push():往数组中加入一个元素(数组后面)
l array_pop():从数组中取出一个元素(数组后面)
l array_shift():从数组中取出一个元素(数组前面)
l array_unshift():从数组中加入一个元素(数组前面)
l array_reverse():数组元素反过来
l in_array():判断一个元素在数组中是否存在
l array_keys():获取一个数组的所有下标,返回一个索引数组
l array_values():获取一个数组的所有值,返回一个索引数组
l array_merge():合并一个或多个数组
array_merge($arr1,$arr2)
l array_splice():把数组中的一部分去掉并用其它值取代
<span style="color:#0000BB;">$input </span>
<span style="color:#007700;">= array(</span>
<span style="color:#DD0000;">"red"</span>
<span style="color:#007700;">, </span>
<span style="color:#DD0000;">"green"</span>
<span style="color:#007700;">, </span>
<span style="color:#DD0000;">"blue"</span>
<span style="color:#007700;">, </span>
<span style="color:#DD0000;">"yellow"</span>
<span style="color:#007700;">);</span>
<span style="color:#0000BB;">array_splice</span>
<span style="color:#007700;">(</span>
<span style="color:#0000BB;">$input</span>
<span style="color:#007700;">, </span>
<span style="color:#0000BB;">2</span>
<span style="color:#007700;">);</span>
<span style="color:#0000BB;">//$input is now array(“red”,”</span>
green<span style="color:#0000BB;">”);</span>
l array_chunk():将一个数组分割成多个
l shuffle():将数组打乱
l range():建立一个包含指定范围单元的数组
l is_file():判断给定文件名是否为一个正常的文件
l define():定义一个常量
l defined():检查某个名称的常量是否存在
l ini_set():为一个配置选项设置值 <span style="color:#0000BB;">ini_set</span>
<span style="color:#007700;">(</span>
<span style="color:#DD0000;">'display_errors'</span>
<span style="color:#007700;">, </span>
<span style="color:#DD0000;">'1'</span>
<span style="color:#007700;">);</span>
l basename():返回路径中的文件名部分
l is_array():检测变量是否是数组
l list():把数组中的值赋给一些变量
<span style="color:#0000BB;">$info </span>
<span style="color:#007700;">= array(</span>
<span style="color:#DD0000;">'coffee'</span>
<span style="color:#007700;">, </span>
<span style="color:#DD0000;">'brown'</span>
<span style="color:#007700;">, </span>
<span style="color:#DD0000;">'caffeine'</span>
<span style="color:#007700;">);</span>
<span style="color:#007700;">list(</span>
<span style="color:#0000BB;">$drink</span>
<span style="color:#007700;">, </span>
<span style="color:#0000BB;">$color</span>
<span style="color:#007700;">, </span>
<span style="color:#0000BB;">$power</span>
<span style="color:#007700;">) = </span>
<span style="color:#0000BB;">$info</span>
<span style="color:#007700;">;</span>
<span style="color:#007700;">echo </span>
<span style="color:#DD0000;">"</span>
<span style="color:#0000BB;">$drink</span>
<span style="color:#DD0000;"> is </span>
<span style="color:#0000BB;">$color</span>
<span style="color:#DD0000;"> and </span>
<span style="color:#0000BB;">$power</span>
<span style="color:#DD0000;"> makes it special.\n"</span>
<span style="color:#007700;">;</span>
l nl2br():在字符串所有新行之前插入 HTML 换行标记
l array_rand():从数组中随机取出一个或多个单元
l count():计算数组中的单元数目或对象中的属性个数
l reset():将数组的内部指针指向第一个单元
相关推荐:
以上是PHP基础函数实例详解的详细内容。更多信息请关注PHP中文网其他相关文章!

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。