Php入門教程之PHP常量使用方法詳解
來(lái)源:程序員人生 發(fā)布時(shí)間:2014-03-11 08:58:37 閱讀次數(shù):4040次
常量在php中是一個(gè)非常重新的數(shù)據(jù)類型了,下面我來(lái)給初學(xué)者詳細(xì)介紹PHP常量一些用法,有需要了解的同學(xué)可進(jìn)入?yún)⒖?
PHP 常量
define() 函數(shù)用于定義常量.一個(gè)常量一旦被定義,就不能再改變或者取消定義.
定義常量的實(shí)例代碼如下:
- <?php
- define("CONSTANT", "你好!");
- echo CONSTANT;
- ?>
常量名和其它任何 PHP 標(biāo)簽遵循同樣的命名規(guī)則.合法的常量名以字母或下劃線開始,后面跟著任何字母,數(shù)字或下劃線.
常量默認(rèn)為大小寫敏感,按照慣例常量標(biāo)識(shí)符總是大寫的,在腳本執(zhí)行期間該值不能改變.定義常量和定義變量的區(qū)別:
1.常量前面沒有美元符號(hào)($)
2.常量只能用 define() 函數(shù)定義,而不能通過(guò)賦值語(yǔ)句
3.常量可以不用理會(huì)變量范圍的規(guī)則而在任何地方定義和訪問(wèn)
4.常量一旦定義就不能被重新定義或者取消定義
5.常量的值只能是標(biāo)量
PHP內(nèi)置了大量預(yù)定義常量,具體的可以在網(wǎng)上搜PHP手冊(cè)里面有具體的內(nèi)容.
判斷一個(gè)常量是否已經(jīng)定義
如何判斷一個(gè)php常量是否已經(jīng)定義過(guò)了,突然之間還有點(diǎn)迷茫,暈,特意查了下手冊(cè),備案本次總結(jié)結(jié)果如下:
(1)判斷常量是否存在
實(shí)例代碼如下:
- if(defined('MYCONSTANT')){
- echo MYCONSTANT;
- }
(2)判斷變量是否定義
實(shí)例代碼如下:
- if(isset($myvar)){
- echo "存在變量$myvar.";
- 3 }
(3)判斷函數(shù)是否存在
實(shí)例代碼如下:
常量和變量相比,不同點(diǎn):
1:常量是全局有效的, 因此在頁(yè)面內(nèi),函數(shù)內(nèi),類內(nèi)部甚至數(shù)組內(nèi)部都可以直接引用.
實(shí)例代碼如下:
- $a=66;
- function t(){ echo $a; }
- t();
- define(“A”,66);
- function t(){ echo A; }
- t();
2:常量一旦定義,就不可以重新定義,不可以清除.也不可以修改;常量也可以動(dòng)態(tài)的哦
實(shí)例代碼如下:
- define("A","常量介紹");
- define("B","常量動(dòng)態(tài)調(diào)用");
- $c=$_get['c'];
- echo constant($c);
面向?qū)ο笾甤onst常量修飾符中常用的常量修飾符const.我們知道,在PHP中定義常量是通過(guò)define()函數(shù)來(lái)完成的,但在類中定義常量不能使用define(),而需要使用const修飾符.類中的常量使用const定義后,其訪問(wèn)方式和靜態(tài)成員類似,都是通過(guò)類名或在成員方法中使用self訪問(wèn),但在PHP 5.3.0之后也可以使用對(duì)象來(lái)訪問(wèn).被const定義的常量不能重新賦值,如果在程序中試圖改變它的值將會(huì)出現(xiàn)錯(cuò)誤
實(shí)例代碼如下:
- <?php
- class MyClass {
- const CONSTANT = 'CONSTANT value' ;
- function showConstant() {
- echo self ::CONSTANT ."<br>" ;
- }
- }
- echo MyClass:: CONSTANT . "<br>" ;
- $class = new MyClass();
- $class->showConstant();
- echo $class ::CONSTANT;
- ?>
關(guān)注細(xì)節(jié):使用const定義的常量名稱前不需要使用“$“符號(hào),且常量名稱通常都是大寫的.
試圖為const定義的常量賦值,將會(huì)出現(xiàn)錯(cuò)誤.
實(shí)例代碼如下:
- <?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')"來(lái)定義常量,你必須使用PHP的關(guān)鍵字 'const'去初始化一個(gè)標(biāo)量--boolean, int, float, or string (除了數(shù)組和其他對(duì)象類型)、
實(shí)例代碼如下:
- <?php
- define('MIN_VALUE', '0.0');
- define('MAX_VALUE', '1.0');
-
-
- class Constants
- {
-
-
- const MIN_VALUE = 0.0;
- const MAX_VALUE = 1.0;
- 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).
實(shí)例代碼如下:
- <?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).
當(dāng)類常量被聲明和初始化后,他們就不能被設(shè)置成其他值--這就是為什么他們?cè)陬惗x時(shí)沒有setMinValue()和setMaxValue()這兩個(gè)方法--這說(shuō)明他們都是只讀而且是靜態(tài)的(被所有該類的對(duì)象共享).
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)