当前位置:Gxlcms > PHP教程 > 不常用但很实用的PHP预定义变量分析

不常用但很实用的PHP预定义变量分析

时间:2021-07-01 10:21:17 帮助过:117人阅读

1. $php_errormsg — 前一个错误信息

  1. <?php
  2. @strpos();
  3. echo $php_errormsg;
  4. ?>

2.$http_response_header — HTTP 响应头

  1. <?php
  2. function get_contents() {
  3. file_get_contents("http://example.com");
  4. var_dump($http_response_header);
  5. }
  6. get_contents();
  7. var_dump($http_response_header);
  8. ?>

3. $argc — 传递给脚本的参数数目

  1. <?php
  2. var_dump($argc);
  3. ?>
  4. 当使用这个命令执行: php script.php arg1 arg2 arg3

4. $argv — 传递给脚本的参数数组

  1. <?php
  2. var_dump($argv);
  3. ?>
  4. 当使用这个命令执行:php script.php arg1 arg2 arg3
  • __FILE__:返回所在路径文件名和文件名称
  • __DIR__:返回文件所在的完整目录
  • __LINE__:返回当前文件代码的行号
  • __CLASS__:返回当前类名
  • __FUNCTION__:返回当前方法名
  • __METHOD__:返回当前类名和方法名
  1. var_dump(__FILE__); //所在路径文件名和文件名称 E:\demo\blog_code\predefined\predefined.php
  2. var_dump(__DIR__); //所在完整目录 E:\demo\blog_code\predefined
  3. var_dump(__LINE__); //代码所在行号 4
  4. class testClass{
  5. function testMethod(){
  6. var_dump(__FUNCTION__); //返回当前方法名 testMethod
  7. var_dump(__CLASS__); //返回类名 testClass
  8. var_dump(__METHOD__); //类名加方法名 testClass::testMethod
  9. }
  10. }
  11. $a=new testClass();
  12. $a->testMethod();

人气教程排行