一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Php入门教程之PHP常量使用方法详解

时间:2013-07-31 编辑:简简单单 来源:一聚教程网

PHP 常量
define() 函数用于定义常量。一个常量一旦被定义,就不能再改变或者取消定义。

定义常量的例子:

 代码如下 复制代码

define("CONSTANT", "你好!");
echo CONSTANT;
?>

常量名和其它任何 PHP 标签遵循同样的命名规则。合法的常量名以字母或下划线开始,后面跟着任何字母,数字或下划线。

常量默认为大小写敏感,按照惯例常量标识符总是大写的,在脚本执行期间该值不能改变。

定义常量和定义变量的区别:

1.常量前面没有美元符号($)
2.常量只能用 define() 函数定义,而不能通过赋值语句
3.常量可以不用理会变量范围的规则而在任何地方定义和访问
4.常量一旦定义就不能被重新定义或者取消定义
5.常量的值只能是标量
PHP内置了大量预定义常量,具体的可以在网上搜PHP手册里面有具体的内容。


判断一个常量是否已经定义


如何判断一个php常量是否已经定义过了,突然之间还有点迷茫,晕,特意查了下手册,备案本次总结结果如下:

(1)判断常量是否存在

 代码如下 复制代码

 if(defined('MYCONSTANT')){ 

    echo MYCONSTANT;  

 }

(2)判断变量是否定义

 代码如下 复制代码

if(isset($myvar)){ 

    echo "存在变量$myvar.";  
3 }


(3)判断函数是否存在

 代码如下 复制代码

if(function_exists('imap_open')){
 echo "存在函数imag_open";
}else{
 echo "函数imag_open不存在";
}


常量和变量相比,不同点:

1:常量是全局有效的, 因此在页面内,函数内,类内部甚至数组内部都可以直接引用.
 

 代码如下 复制代码
   $a=66;
    function t(){ echo $a; }
    t();//此时不能打印出来99,因为函数作用域影响,如果要打印出99,可以改为:
    define(“A”,66);
    function t(){ echo A; }
    t();

2:常量一旦定义,就不可以重新定义,不可以清除.也不可以修改;

常量也可以动态的哦

 

 代码如下 复制代码

define("A","常量介绍");
  define("B","常量动态调用");
  $c=$_get['c'];//此处直接把b的值,并不会再b的值当成常量名再次解析
  echo constant($c);// constant(常量名)  ---> 返回常量的值


 

面向对象之const常量修饰符

中常用的常量修饰符const。

我们知道,在PHP中定义常量是通过define()函数来完成的,但在类中定义常量不能使用define(),而需要使用const修饰符。类中的常量使用const定义后,其访问方式和静态成员类似,都是通过类名或在成员方法中使用self访问,但在PHP 5.3.0之后也可以使用对象来访问。被const定义的常量不能重新赋值,如果在程序中试图改变它的值将会出现错误

 代码如下 复制代码


    class MyClass { 

          const CONSTANT = 'CONSTANT value' ; //使用const声明一个常量,并直接赋上初使值 

            function showConstant() {                

                   echo  self ::CONSTANT ."
" ;//使用self访问常量,注意常量前不要加“$” 

             } 

      } 
      echo MyClass:: CONSTANT . "
" ; //在类外部使用类名称访问常量,也不要加”$” 
      $class = new MyClass();                      

     $class->showConstant();                       

      echo $class ::CONSTANT;  // PHP 5.3.0之后 

 ?>

关注细节:使用const定义的常量名称前不需要使用“$“符号,且常量名称通常都是大写的。


试图为const定义的常量赋值,将会出现错误。

 代码如下 复制代码

     class MyClass { 

          const CONSTANT = 'CONSTANT value' ;   

             function setCONSTANT(){ 

               self ::CONSTANT  = 'news CONSTANT' ;//程序运行结果将会出错。 

      } 

                                                                                                                                                                                         

      } 
     echo MyClass:: CONSTANT ;           

                                                                                                                                                                            

?>


CONSTANTS and PHP Class Definitions

Using "define('MY_VAR', 'default value')" INSIDE a class definition does not work. You have to use the PHP keyword 'const' and initialize it with a scalar value -- boolean, int, float, or string (no array or other object types) -- right away.

不能在类里面使用"define('MY_VAR', 'default value')"来定义常量,你必须使用PHP的关键字 'const'去初始化一个标量--boolean, int, float, or string (除了数组和其他对象类型)、

 代码如下 复制代码

define('MIN_VALUE', '0.0');   // RIGHT - Works OUTSIDE of a class definition.
define('MAX_VALUE', '1.0');   // RIGHT - Works OUTSIDE of a class definition.

//const MIN_VALUE = 0.0;         WRONG - Works INSIDE of a class definition.
//const MAX_VALUE = 1.0;         WRONG - Works INSIDE of a class definition.

class Constants
{
  //define('MIN_VALUE', '0.0');  WRONG - Works OUTSIDE of a class definition.
  //define('MAX_VALUE', '1.0');  WRONG - Works OUTSIDE of a class definition.

  const MIN_VALUE = 0.0;      // RIGHT - Works INSIDE of a class definition.
  const MAX_VALUE = 1.0;      // RIGHT - Works INSIDE of a class definition.

  public static function getMinValue()
  {
    return self::MIN_VALUE;
  }

  public static function getMaxValue()
  {
    return self::MAX_VALUE;
  }
}

?>

#Example 1:
You can access these constants DIRECTLY like so:
 * type the class name exactly.
 * type two (2) colons.
 * type the const name exactly.

#Example 2:
Because our class definition provides two (2) static functions, you can also access them like so:
 * type the class name exactly.
 * type two (2) colons.
 * type the function name exactly (with the parentheses).

 代码如下 复制代码

#Example 1:
$min = Constants::MIN_VALUE;
$max = Constants::MAX_VALUE;

#Example 2:
$min = Constants::getMinValue();
$max = Constants::getMaxValue();

?>

 

Once class constants are declared AND initialized, they cannot be set to different values -- that is why there are no setMinValue() and setMaxValue() functions in the class definition -- which means they are READ-ONLY and STATIC (shared by all instances of the class).

当类常量被声明和初始化后,他们就不能被设置成其他值--这就是为什么他们在类定义时没有setMinValue()和setMaxValue()这两个方法--这说明他们都是只读而且是静态的(被所有该类的对象共享)。

热门栏目