首页 > 开发 > PHP > 正文

PHP中的常量

2024-05-04 23:02:33
字体:
来源:转载
供稿:网友

 

php预先定义了几个常量,并提供了一种机制在运行时自己定义。常量和变量基本上是一样的,不同的是:常量必须用define函数定义,常量一旦定义好,就不能被重新定义了。

php
中预先定义好的常量:

__file__
这个默认常量是 php 程序文件名。若引用文件 (include require)则在引用文件内的该常量为引用文件名,而不是引用它的文件名。

__line__
这个默认常量是 php 程序行数。若引用文件 (include require)则在引用文件内的该常量为引用文件的行,而不是引用它的文件行。

php_version
这个内建常量是 php 程序的版本,如 3.0.8-dev

php_os
这个内建常量指执行 php 解析器的操作系统名称,如 linux

true
这个常量就是真值 (true)

false
这个常量就是伪值 (false)

e_error
这个常量指到最近的错误处。

e_warning
这个常量指到最近的警告处。

e_parse
本常式为解析语法有潜在问题处。

e_notice
这个常式为发生不寻常但不一定是错误处。例如存取一个不存在的变量。

这些 e_ 开头形式的常量,可以参考 error_reporting() 函数,有更多的相关说明。


可以用define函数定义更多的常量。

如,定义常量:

<?php
define("constant", "hello world.");
echo constant; // outputs "hello world."
?>

__file__ __line__ 的举例

php:
function report_error($file, $line, $message) {
echo "an error occured in $file on line $line: $message.";
}

report_error(__file__,__line__, "something went wrong!");
?>


我自己的写法:
<?
$file = __file__;
$line = __line__;
echo $file;
echo "<br><br>";
echo $line;
echo "<br><br>";
echo __file__;
echo "<br><br>";
echo (__line__);
?>

echo
常量用echo (); 不用echo ""

上一篇:PHP中的类-操作XML(1)

下一篇:PHP语句

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表