企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 第四课 常用函数 `>empty 检测变量是否为空,不支持语句 ``` <pre class="calibre14">``` <span class="token4">""</span> <span class="token3">0</span> <span class="token4">"0"</span> <span class="token1">array</span><span class="token2">(</span><span class="token2">)</span> NULL FALSE var $var 七类数据为空<span class="token2">,</span>返回TRUE <span class="token4">" "</span> 返回FALSE ``` ``` - "" 0 false array() null 五类都是NULL - 0 '' false null array 五类都是false; - false null true 和FALSE NULL TRUE 是不区分大小写 > isset检测变量是否存在 ``` <pre class="calibre14">``` $var <span class="token">=</span> <span class="token4">""</span><span class="token2">;</span> <span class="token1">isset</span><span class="token2">(</span>$var<span class="token2">)</span> 返回TRUE <span class="token1">unset</span><span class="token2">(</span>$var<span class="token2">)</span> 此时返回 FALSE <span class="token1">isset</span><span class="token2">(</span>NULL<span class="token2">)</span> 返回<span class="token3">false</span> ``` ``` 另一种是未定义 > ceil 进一法取整 ``` <pre class="calibre14">``` <span class="token1">ceil</span><span class="token2">(</span><span class="token3">4.01</span><span class="token2">)</span> <span class="token3">4.01</span><span class="token">-</span><span class="token3">5</span>返回<span class="token3">5</span> <span class="token1">ceil</span><span class="token2">(</span><span class="token">-</span><span class="token3">4</span><span class="token2">)</span><span class="token2">;</span> <span class="token">-</span><span class="token3">4</span>到<span class="token">-</span><span class="token3">4.99</span> 返回<span class="token">-</span><span class="token3">4</span> ``` ``` > 舍去法取整 ``` <pre class="calibre14">``` <span class="token1">floor</span><span class="token2">(</span><span class="token3">4.99</span><span class="token2">)</span> <span class="token3">4</span><span class="token">-</span><span class="token3">4.99</span> 返回<span class="token3">4</span> <span class="token1">floor</span><span class="token2">(</span><span class="token">-</span><span class="token3">4.01</span><span class="token2">)</span> <span class="token">-</span><span class="token3">4.01</span><span class="token">-</span><span class="token3">5</span> 返回<span class="token">-</span><span class="token3">5</span> ``` ``` > 四舍五入 ``` <pre class="calibre14">``` <span class="token1">round</span><span class="token2">(</span><span class="token3">3.1</span><span class="token2">)</span> 返回<span class="token3">3</span> <span class="token3">3.5</span>返回<span class="token3">4</span> <span class="token1">round</span><span class="token2">(</span><span class="token3">3.453</span><span class="token2">,</span><span class="token3">2</span><span class="token2">)</span> 返回<span class="token3">3.45</span> 保留两位小数 <span class="token1">round</span><span class="token2">(</span><span class="token3">12355</span><span class="token2">,</span><span class="token">-</span><span class="token3">2</span><span class="token2">)</span> 返回<span class="token3">12400</span> ``` ``` > intval转换成整数 intval('1.2') 返回1 使用 (int)1.2也可以 > mt\_rand随机数 mt\_rand() 0到最大数随机数 mt\_rand(1,20) 1-20之间随机数 > php版本 PHP\_VERSION > 类型转换 浮点型 floatval($str) > is\_array($str) 是否数组 ``` <pre class="calibre14">``` <span class="token1">define</span><span class="token2">(</span><span class="token4">"CONSTANT"</span><span class="token2">,</span> <span class="token4">"Hello world."</span><span class="token2">)</span> 常量定义 <span class="token1">defined</span><span class="token2">(</span><span class="token4">'IN_IA'</span><span class="token2">)</span> or <span class="token1">exit</span><span class="token2">(</span><span class="token4">'Access Denied'</span><span class="token2">)</span>是否定义常量 ``` ``` die() exit() 同名函数 ``` <pre class="calibre14">``` <span class="token1">highlight_file</span><span class="token2">(</span>$filename<span class="token2">)</span> 高亮 <span class="token1">highlight_string</span><span class="token2">(</span>$str<span class="token2">)</span><span class="token2">;</span> ``` ``` > 延迟秒数 > `sleep(10);` 数组打乱变量,变量合并数组 ``` <pre class="calibre14">``` $i <span class="token">=</span> <span class="token4">"999"</span><span class="token2">;</span> $arr <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span> <span class="token4">'a'</span><span class="token">=</span><span class="token">></span><span class="token4">'我爱你'</span><span class="token2">,</span> <span class="token4">'b'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token2">,</span> <span class="token4">'c'</span><span class="token">=</span><span class="token">></span><span class="token3">0</span><span class="token2">,</span> <span class="token4">'d'</span><span class="token">=</span><span class="token">></span><span class="token4">'美国'</span><span class="token2">,</span> <span class="token2">)</span><span class="token2">;</span> <span class="token1">extract</span><span class="token2">(</span>$arr<span class="token2">)</span><span class="token2">;</span> $arr2 <span class="token">=</span> <span class="token1">compact</span><span class="token2">(</span><span class="token4">'a'</span><span class="token2">,</span><span class="token4">'b'</span><span class="token2">,</span><span class="token4">'c'</span><span class="token2">,</span><span class="token4">'d'</span><span class="token2">,</span><span class="token4">'i'</span><span class="token2">)</span><span class="token2">;</span> ``` ``` 保留几位小数 ``` <pre class="calibre14">``` @<span class="token1">sprintf</span><span class="token2">(</span><span class="token4">"%.4f"</span><span class="token2">,</span><span class="token3">1.345677</span><span class="token2">)</span> ``` ``` 索引数组转换json `$arr = ["中国","日本"]; $arr = new ArrayObject($arr); $json = json_encode($arr,256);` 判断一个数组是否空值 ``` <pre class="calibre16">``` <span class="token1">empty</span><span class="token2">(</span><span class="token1">implode</span><span class="token2">(</span><span class="token4">""</span><span class="token2">,</span>$v<span class="token2">)</span><span class="token2">)</span> ``` ```