Home >Backend Development >PHP Tutorial >How to write predefined variables in php
Predefined variables are special variables automatically created by the interpreter in PHP. They contain information about the script execution environment and cannot be written directly. Predefined variables in PHP include: constant variables (such as __LINE__, __FILE__), special variables (such as $this), environment variables (such as $_SERVER), and global variables (such as $GLOBALS). You can access predefined variables using standard PHP variable syntax.
Predefined variables in PHP
What are predefined variables?
Predefined variables are a special type of variables in PHP. They are automatically created by the PHP interpreter and contain information about the script execution environment.
How to write predefined variables
You cannot directly write or assign predefined variables. They are automatically generated by the PHP interpreter when the script is executed.
What are the predefined variables in PHP?
There are many predefined variables in PHP, including:
__LINE__
, __FILE__
and __FUNCTION__
, which contain information about the current state of the script. $this
, which points to the current object. $_SERVER
and $_GET
, which contain information about the server environment and client requests. $GLOBALS
, which contains an array of all global variables in the script. Accessing predefined variables
You can access predefined variables using standard PHP variable syntax. For example:
<code class="php">echo __LINE__; // 输出当前代码行的行号 echo __FILE__; // 输出当前脚本的文件名</code>
Example
The following example shows how to use some predefined variables:
<code class="php"><?php echo "你在第 {$_SERVER['REMOTE_ADDR']} 行访问了这个脚本。"; echo "<br>"; echo "脚本正在 {__FILE__} 中执行,第 {__LINE__} 行。"; ?></code>
Output:
<code>你在 127.0.0.1 行访问了这个脚本。 脚本正在 /var/www/example.php 中执行,第 12 行。</code>
The above is the detailed content of How to write predefined variables in php. For more information, please follow other related articles on the PHP Chinese website!