Heim > php教程 > php手册 > Hauptteil

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

WBOY
Freigeben: 2016-05-25 16:48:26
Original
1314 Leute haben es durchsucht

常量在php中是一个非常重新的数据类型了,下面我来给初学者详细介绍PHP常量一些用法,有需要了解的同学可进入参考.

PHP 常量

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

定义常量的实例代码如下:

<?php 
    define("CONSTANT", "你好!"); 
    echo CONSTANT; 
?>
Nach dem Login kopieren

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

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

1.常量前面没有美元符号($)

2.常量只能用 define() 函数定义,而不能通过赋值语句

3.常量可以不用理会变量范围的规则而在任何地方定义和访问

4.常量一旦定义就不能被重新定义或者取消定义

5.常量的值只能是标量

PHP内置了大量预定义常量,具体的可以在网上搜PHP手册里面有具体的内容.

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

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

(1)判断常量是否存在

实例代码如下:

if(defined(&#39;MYCONSTANT&#39;)){  
    echo MYCONSTANT;   
}
Nach dem Login kopieren

(2)判断变量是否定义

实例代码如下:

if(isset($myvar)){  
    echo "存在变量$myvar.";    
}
Nach dem Login kopieren

(3)判断函数是否存在

实例代码如下:

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

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

实例代码如下:

$a=66; 
function t(){ echo $a; } 
t();//此时不能打印出来99,因为函数作用域影响,如果要打印出99,可以改为: 
define("A",66); 
function t(){ echo A; } 
t();
Nach dem Login kopieren

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

实例代码如下:

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

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

实例代码如下:

<?php  
class MyClass {  
    const CONSTANT = &#39;CONSTANT value&#39; ; //使用const声明一个常量,并直接赋上初使值  
    function showConstant() {                 
        echo  self ::CONSTANT ."<br>" ;//使用self访问常量,注意常量前不要加"$"  
    }  
}   
echo MyClass:: CONSTANT . "<br>" ; //在类外部使用类名称访问常量,也不要加"$"  
$class = new MyClass();                       
$class->showConstant();                        
echo $class ::CONSTANT;  // PHP 5.3.0之后  
?>
Nach dem Login kopieren

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

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

实例代码如下:

<?php  
class MyClass {  
    const CONSTANT = &#39;CONSTANT value&#39; ;    
    function setCONSTANT(){  
        self ::CONSTANT  = &#39;news CONSTANT&#39; ;//程序运行结果将会出错.  
    }        
}   
echo MyClass::CONSTANT ;
?>
Nach dem Login kopieren

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 (除了数组和其他对象类型)、

实例代码如下:

<?php 
    define(&#39;MIN_VALUE&#39;, &#39;0.0&#39;);   // RIGHT - Works OUTSIDE of a class definition. 
    define(&#39;MAX_VALUE&#39;, &#39;1.0&#39;);   // 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(&#39;MIN_VALUE&#39;, &#39;0.0&#39;);  WRONG - Works OUTSIDE of a class definition. 
      //define(&#39;MAX_VALUE&#39;, &#39;1.0&#39;);  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; 
      } 
    } 
?>
Nach dem Login kopieren
#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).
Nach dem Login kopieren

实例代码如下:

<?php 
    #Example 1: 
    $min = Constants::MIN_VALUE; 
    $max = Constants::MAX_VALUE; 
    #Example 2: 
    $min = Constants::getMinValue(); 
    $max = Constants::getMaxValue(); 
?>
Nach dem Login kopieren
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).
Nach dem Login kopieren

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


本文地址:

转载随意,但请附上文章地址:-)

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!