常量在php中是一個非常重新的數據類型了,下面我來給初學者詳細介紹PHP常量一些用法,有需要了解的同學可進入參考.
PHP 常量
define() 函數用于定義常量.一個常量一旦被定義,就不能再改變或者取消定義.
定義常量的實例代碼如下:
- <?php
- 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)判斷函數是否存在
常量和變量相比,不同點:
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定義的常量不能重新賦值,如果在程序中試圖改變它的值將會出現錯誤
實例代碼如下:
- <?php
- class MyClass {
- const CONSTANT = 'CONSTANT value' ; //使用const聲明一個常量,并直接賦上初使值
- function showConstant() {
- echo self ::CONSTANT ."<br>" ;//使用self訪問常量,注意常量前不要加“$”
- }
- }
- echo MyClass:: CONSTANT . "<br>" ; //在類外部使用類名稱訪問常量,也不要加”$”
- $class = new MyClass();
- $class->showConstant();
- echo $class ::CONSTANT; // PHP 5.3.0之后
- ?>
關注細節:使用const定義的常量名稱前不需要使用“$“符號,且常量名稱通常都是大寫的.
試圖為const定義的常量賦值,將會出現錯誤.
實例代碼如下:
- <?php
- 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 (除了數組和其他對象類型)、
實例代碼如下:
- <?php
- 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).
實例代碼如下:
- <?php
- #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()這兩個方法--這說明他們都是只讀而且是靜態的(被所有該類的對象共享).
新聞熱點
疑難解答