Nick's Blog

PHP基础之常量(Constant)

常量 (constant):

  1. 唯一的值

  1. 命名方式多为大写

  1. 数据类型可以为int, float, boolean

 

举例:

define(‘DBNAME’, ‘901itcom’);

define(‘DBNAME’, ‘901it’);

echo DBNAME;

OUTPUT:

报错

define(‘DBNAME’, ‘901itcom’);

echo DBNAME;

OUTPUT:

901itcom

define (‘DB’, ‘901itcom’);

function fun(){

echo DB;

}

fun();

OUTPUT:

901itcom

 

define (‘URL’, ‘901ITCOM’);

if (defined(‘URL’)){

echo URL;

}else{

echo “you didn’t define constants”;

}

OUTPUT:

901ITCOM

 

  1. 系统衡量

echo M_PI;

echo “<br>”;

echo PHP_OS;

echo “<br>”;

echo PHP_VERSION;

OUTPUT:

3.1415926535898

WINNT

5.4.16

  1. 魔法衡量

echo __LINE__;

echo “<br>”;

echo __FILE__;

echo “<br>”;

function fun2(){

echo __function__;

echo “<br>”;

}

fun2();

OUTPUT:

7

C:wwwbasictest.php

fun2

class test{

function fun1(){

echo __CLASS__;

echo “<br>———-<br>”;

echo __METHOD__;

}

}

$ob1 = new test();

$ob1->fun1();

OUTPUT:

test

———-

test::fun1