💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
1.DIRECTORY\_SEPARATOR php目录分隔符:\ 在windows我们习惯性的使用“\\”作为文件分隔符,但是在linux上系统不认识这个标识,于是就要引入这个php内置变量了:DIRECTORY_SEPARATOR 1.5**PHP_EOL**为换行符。 2.# PHP常量PHP_SAPI与函数php_sapi_name()简介,PHP运行环境检测 (1)PHP_SAPI 用来判断是使用命令行还是浏览器执行的,如果 PHP_SAPI==’cli’ 表示是在命令行下执行 php判断解析php服务是由那种服务器软件,是采用那种协议 直接输出即可 代码如下: <?php echo PHP_SAPI; ?> 我的测试环境是nginx+fastcgi 输出结果为: cgi-fcgi 如果是apache 运行环境,输出结果为 apache2handler 如果是命令行的形式执行,结果为 cli 这就是PHP_SAPI (2)php_sapi_name() 是用来检测PHP运行环境的函数 例如,CLI 的 PHP 下这个字符串会是 “cli”,Apache 下可能会有几个不同的值,取决于具体使用的 SAPI。 以下列出了可能的值: aolserver、apache、 apache2filter、apache2handler、 caudium、cgi (直到 PHP 5.3), cgi-fcgi、cli、 continuity、embed、 isapi、litespeed、 milter、nsapi、 phttpd、pi3web、roxen、 thttpd、tux 和 webjames。 3. die()停止程序运行,输出内容 exit是停止程序运行,不输出内容 return是返回值 return:执行到该处退出,如果是在函数中,表示退出函数,如果是在脚本中,表示脚本停止执行 die是遇到错误才停止 exit是直接停止,并且不运行后续代码,exit()可以显示内容,例如exit('dsdsdsdf')。 exit(0):正常运行程序并退出程序; exit(1):非正常运行导致退出程序; 4 dirname() 函数返回路径中的目录部分 D:\\phpstudy\\PHPTutorial\\WWW\tp5\public 再一次:D:\\phpstudy\\PHPTutorial\\WWW\tp5 再一次:D:\\phpstudy\\PHPTutorial\\WWW 5. realpath() 函数返回绝对路径。 该函数删除所有符号连接(比如 '/./', '/../' 以及多余的 '/'),返回绝对路径名。 6.spl_autoload_register() ``` <? class test { public static function loadprint( $class ) { $file = $class . '.class.php'; if (is_file($file)) { require_once($file); } } } spl_autoload_register( array('test','loadprint') ); //另一种写法:spl_autoload_register( "test::loadprint" ); $obj = new PRINTIT(); $obj->doPrint();?> PRINTIT::doPrint() 同样会加载 PRINTIT.php文件 用法: require_once('A.php'); \A::regist(); //注册自动加载 echo \User::test();//自动加载User.php文件 ``` 7. 'think\\Loader::autoload' 表示:'think\Loader::autoload' \\转义字符,表示'\' 8.property_exists 主要作用是判断类或对象中的属性是否存在,存在是为true,不存在是false bool interface_exists (string $interface_name [, bool $autoload = true ]) 判断接口是否存在 bool class_exists (string $class_name [, bool $autoload = true ]) 判断类是否存在 bool method_exists (mixed $object , string $method_name) 判断指定类或者对象中是否含有指定的成员函数 bool property_exists (mixed $class , string $property) 判断指定类或者对象中是否含有指定的成员变量。 function_exists()检测函数是否存在 is_callable )检测函数是否匿名函数 **function\_exists 比较简单点就是判断函数有没有被定义 而method\_exists 是判断类内的方法存不存在  is\_callable 检测参数是否为合法的可调用结构** ``` class Obj{ public $is_public; protected $is_protected; private $is_private; public static $is_public_static; const is_const=3; } $obj = new Obj(); var_dump(property_exists('Obj','is_public')); //true; var_dump(property_exists($obj,'is_public')); //true; var_dump(property_exists($obj,'is_protected')); //true; var_dump(property_exists($obj,'is_private')); //true; var_dump(property_exists($obj,'is_public1')); //false; var_dump(property_exists($obj,'is_public_static')); //true; var_dump(property_exists('Obj','is_const')); //false; ``` 9. ``` [0=>'sasas'] 另一种写法:(array)'sasas' ``` 10 ``` var_dump(facade\App::class); 输出结果为: "app\index\controller\facade\App" ‘app\index\controller\’为所在文件的命名空间 ``` 11. strtr() 函数 ``` 把字符串中的字符 "ia" 替换为 "eo": <?php echo strtr("Hilla Warld","ia","eo"); ?> ``` 12. ``` $str='adafadsfadsff'; echo $str[0]; 输出结果:a; ``` 13. microtime() 函数返回当前 Unix 时间戳的微秒数。 ucwords() 函数把字符串中每个单词的首字符转换为大写 ucfirst() 函数把字符串中的首字符转换为大写 lcfirst() - 把字符串中的首字符转换为小写 strtolower() 函数把字符串转换为小写 strtoupper() 函数把字符串转换为大写 strpos() 函数查找字符串在另一字符串中第一次出现的位置。 strrpos() 函数查找字符串在另一字符串中最后一次出现的位置。 查找 "php" 在字符串中最后一次出现的位置: echo strrpos("You love php, I love php too!","php"); stristr() 函数搜索字符串在另一字符串中的第一次出现,并返回字符串的剩余部分 strstr() 函数搜索字符串在另一字符串中的第一次出现并,返回字符串的剩余部分,对大小写敏感 trim() 函数移除字符串两侧的空白字符或其他预定义字符。 ltrim() - 移除字符串左侧的空白字符或其他预定义字符 rtrim() - 移除字符串右侧的空白字符或其他预定义字符 realpath() 函数返回绝对路径,该函数删除所有符号连接(比如 '/./', '/../' 以及多余的 '/'),返回绝对路径名。若失败,则返回 false。比如说文件不存在的话。 scandir() 函数以数组形式返回指定目录中的文件和目录 strtr() 函数转换字符串中特定的字符。strtr("Hilla Warld","ia","eo"); str_replace() 函数以其他字符替换字符串中的一些字符(区分大小写) str_replace("world","Shanghai","Hello world!");把字符串 "Hello world!" 中的字符 "world" 替换为 "Shanghai"; 14 memory_get_usage()函数返回内存使用量,memory_get_peak_usage()函数返回内存使用峰值,getrusage()返回CUP使用情况。但有一点请注意,在这些函数需要在Linux上运行 15. $declaredClass = get_declared_classes(); 获取当前加载的所有类 $composerClass = array_pop($declaredClass); 删除数组中的最后一个元素,返回值是最后一个元素 16. pathinfo(path,options)函数以数组的形式返回文件路径的信息。 * PATHINFO_DIRNAME - 只返回 dirname * PATHINFO_BASENAME - 只返回 basename * PATHINFO_EXTENSION - 只返回 extension * PATHINFO_FILENAME-只返回 FILENAME ``` <?php print_r(pathinfo("/testweb/test.txt")); ?> ~~~ Array ( [dirname] => /testweb [basename] => test.txt [extension] => txt [filename] => test ) ~~~ ``` 17. key() 函数返回数组内部指针当前指向元素的键名 reset() 函数将内部指针指向数组中的第一个元素,并输出。 18.# call_user_func_array函数详解 call_user_func函数详解 把第一个参数作为回调函数(**callback**)调用,把参数数组作(**param_arr**)为回调函数的的参数传入 ``` 代码如下: (1)普通使用:            function a($b, $c) {                   echo $b;                  echo $c;             }            call_user_func_array('a', array("111", "222"));            //输出 111 222 (2)调用类内部的方法:          Class ClassA {                 function bc($b, $c) {                    $bc = $b + $c;                    echo $bc;                   }              }            call_user_func_array(array('ClassA','bc'), array("111", "222"));            //输出  333  (3)支持引用传递:           function a(&$b) {                $b++;            }            $c = 1;            call_user_func_array('a', array(&$c));            echo $c;  //输出 2  ``` 19. **func\_get\_args():返回一个包含函数参数列表的数组。** **func\_get\_arg():返回指定的参数值。** **func\_num\_args():返回调用函数的传入参数个数,类型是整型** ``` public function test(){ $res= $this->hello(1,2,3,4); var_dump($res); } public function hello($a,$b,$c,$d){ $num=func_num_args(); $num2=func_get_arg(2); $num3=func_get_args(); return $num3; } 打印结果分别是:4,3,array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) } ``` 20# __CLASS__、get_class()与get_called_class()都是获取当前类名,完整类名,包含命名空间 区别: ![](https://img.kancloud.cn/ca/15/ca15d20e46f40089260122011b6cf14b_672x458.png) 21 php 5.3 后新增了__call与__callStatic魔法方法。 __call当要调用的方法不存在或权限不足时,会自动调用__call 方法。 __callStatic当调用的静态方法不存在或权限不足时,会自动调用__callStatic方法。 ``` public function __call($func, $arguments){ } public function __callStatic($func, $arguments){ } ``` 22.错误方面的函数 ~~~ // 关闭错误报告 error_reporting(0); // 报告 runtime 错误 error_reporting(E_ERROR | E_WARNING | E_PARSE); // 报告所有错误 error_reporting(E_ALL); // 等同 error_reporting(E_ALL); ini_set("error_reporting", E_ALL); // 报告 E_NOTICE 之外的所有错误 error_reporting(E_ALL & ~E_NOTICE); ~~~ ``` 通过 set\_error\_handler() 函数设置用户自定义的错误处理程序,然后触发错误(通过 trigger\_error()): set\_error\_handler("customError") 不仅可以接受**函数**,还可以接受**类的方法(公开的静态方法 及 公开的非静态方法 都可以)**,但需要以**数组形式**传递,数组的第一值为“类名”,第二个参数为“方法名”, ``` ``` set\_exception\_handler() 函数设置用户定义的异常处理函数。 set_exception_handler([__CLASS__, 'appException']); ``` ``` 当PHP程序执行完成后,自动执行register\_shutdown\_function函数,该函数需要一个参数,用来指定由谁处理这些后续的工作。其中,**程序执行完成**,分为以下几种情况: 第一种:php代码执行过程中发生错误 第二种:php代码顺利执行成功 第三种:php代码运行超时 第四种:页面被用户强制停止 register_shutdown_function([__CLASS__, 'appShutdown']); ``` 23. get_object_var($object) 返回类中所有的非静态方法和非静态属性 24.array_change_key_case() 函数将数组的所有的键都转换为大写字母或小写字母。 * CASE_LOWER - 默认值。将数组的键转换为小写字母。 * CASE_UPPER - 将数组的键转换为大写字母 25uniqid(prefix,more_entropy) uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID。 prefix 可选。为 ID 规定前缀。如果两个脚本恰好在相同的微秒生成 ID,该参数很有用。 more_entropy 可选。规定位于返回值末尾的更多的熵。