Home >Backend Development >PHP Tutorial >A brief summary of commonly used magic constants and superglobal variables in PHP

A brief summary of commonly used magic constants and superglobal variables in PHP

不言
不言Original
2018-08-23 09:45:582281browse

This article brings you a brief summary of the commonly used magic constants and superglobal variables in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Commonly used magic constants

//获取当前文件所在的目录,如:D:\web
echo __DIR__;
//获取当前文件的绝对路径,目录+文件名,如:D:\web\index.php
echo __FILE__;
//当前运行脚本的类名称
echo __CLASS__;
//当前运行脚本的函数名称
echo __FUNCTION__;
//当前运行脚本的方法名称
echo __METHOD__;
/*注:非类方法体内,__FUNCTION__等同于__METHOD__;
     类方法体中__METHOD__会包含类名称,如:ClassName::MethodName*/

2. Commonly used super global variables

//$_COOKIE由HTTP传递给脚本的变量数组,记录在客户端
print_r($_COOKIE);
$_COOKIE['var_name'] = 'cookie';    //赋值
echo $_COOKIE['var_name'];        //取值
//$_SESSION存储在服务器端的变量数组,需要依赖$_COOKIE
session_start();//开启session,需写在在文件开头
print_r($_SESSION);
$_SESSION['var_name'] = 'session';    //赋值
echo $_SESSION['var_name'];        //取值
//$_GET获取URL携带的参数
echo $_GET['param'];
//$_POST获取HTTP使用post方式传递的参数
echo $_POST['param'];

Related recommendations:

PHP global variables and super global variables Difference analysis, the difference between php global variables

The difference between super global variables $GLOBALS and global in PHP              

The above is the detailed content of A brief summary of commonly used magic constants and superglobal variables in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn