PHP中的常量

来源:互联网 发布:w3m linux 编辑:程序博客网 时间:2024/05/21 10:53
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

 

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 ""<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击