如题所述
ä¼æå¨ç¥ï¼å¨PHPä¸(PHP 4å以å)ï¼æ们å¯ä»¥ä½¿ç¨å½æ°define()æ¥å®ä¹å¸¸éï¼ä¾å¦ï¼
<?php
define('PI', 3.14159); //å®ä¹ä¸ä¸ªå为PIç常é
echo PI; //è¾åºï¼3.14159
?>
ä¸è¿ï¼å¨PHP 5.3.0ä¹åï¼é¤äºä½¿ç¨å½æ°define()ä¹å¤ï¼æ们è¿å¯ä»¥ä½¿ç¨PHPå ³é®åconstæ¥å®ä¹å¸¸éã
ä¾å¦ï¼
<?php
//以ä¸ä»£ç éå¨PHP 5.3.0åä¹åççæ¬ä¸è¿è¡
const PI = 3.14159; //使ç¨constå ³é®åå®ä¹ä¸ä¸ªå为PIç常é
echo PI; //è¾åºï¼3.14159
?>
è½ç¶ä¸è¿°ä¸¤ç§æ¹å¼åå¯ä»¥å®ä¹å¸¸éï¼ä½æ¯å®ä»¬ä¹é´æä»ä¹ä¸åä¹å¤å¢ãä¸é¢æ们æ¥ä¸ä¸è®²è§£PHPä¸define()å½æ°åconstå ³é®åå®ä¹å¸¸éçåºå«ï¼
1.çæ¬å·®å¼
é¦å ï¼æ¯«æ çé®çæ¯ï¼ä¸¤ç§å®ä¹å¸¸éçæ¹å¼ä¹é´åå¨çæ¬å·®å¼ï¼å½æ°define()å¨PHP4åPHP5ä¸åå¯ä½¿ç¨ï¼å ³é®åconståªè½å¨PHP 5.3.0åå ¶åççæ¬ä¸ä½¿ç¨ã
2.å®ä¹ä½ç½®çåºå«
ç±äºå½æ°define()å®ä¹ç常éæ¯å¨æ§è¡define()å½æ°æ¶å®ä¹çï¼å æ¤å¯ä»¥å¨å½æ°å ã循ç¯å ãifè¯å¥å çå½æ°è½å¤è¢«è°ç¨çä»»ä½å°æ¹ä½¿ç¨define()å½æ°å®ä¹å¸¸éãä¸define()ä¸åçæ¯ï¼ç±äºconstå ³é®åå®ä¹ç常éæ¯å¨ç¼è¯æ¶å®ä¹çï¼å æ¤constå ³é®åå®ä¹å¸¸éå¿ é¡»å¤äºæ顶端çä½ç¨åºåãè¿ä¹å°±æå³çä¸è½å¨å½æ°å ã循ç¯å 以åifè¯å¥ä¹å ç¨constæ¥å®ä¹å¸¸éã
<?php
//使ç¨constå ³é®åå®ä¹å¸¸éå¿ é¡»å¤äºæ顶端çä½ç¨åºå
//ä¹å°±æ¯å¯ä»¥å¨ç¼è¯æ¶ç´æ¥è§£æå®ä¹çå°æ¹
const DEMO = 'DEMO';
class Person{
const MAN = 'ç·';
const WOMAN = '女';
}
interface USB{
const VERSION_2 = '2.0';
const VERSION_3 = '3.0';
}
?>
3.对å¼ç表达å¼æ¯æçå·®å¼
è½ç¶å ³é®åconstådefine()å®ä¹ç常éå¼é½åªè½ä¸ºnullææ éæ°æ®(booleanï¼integerï¼floatåstringç±»å)以åresourceç±»å(ä¸æ¨èå®ä¹resourceç±»åç常éï¼å¦åå¯è½åºç°æ æ³é¢ç¥çç»æ)ãä¸è¿ï¼ç±äºå ³é®åconstå®ä¹å¸¸éæ¯å¨ç¼è¯æ¶å®ä¹çï¼å æ¤constå ³é®åå®ä¹ç常éå¼ç表达å¼ä¸ä¸æ¯æç®æ¯è¿ç®ç¬¦ãä½è¿ç®ç¬¦ãæ¯è¾è¿ç®ç¬¦çå¤ç§è¿ç®ç¬¦ï¼èè¿äºè¿ç®ç¬¦å¨define()å½æ°å®ä¹å¸¸éæ¶é½æ¯å¯ä»¥ç´æ¥ä½¿ç¨çã
<?php
define('DEFINE_VAR1', 1 << 1);
//const CONST_VAR1 = (1 << 1); //constä¸æ¯æä½è¿ç®ç¬¦ï¼PHPä¼æ¥è¯æ³é误
define('DEFINE_VAR2', 1 + 1);
//const CONST_VAR2 = 1 + 1 ; //constä¸æ¯æç®æ¯è¿ç®ç¬¦ï¼PHPä¼æ¥è¯æ³é误
define('DEFINE_VAR3', 1 == 1);
//const CONST_VAR3 = 1 == 1 ; //constä¸æ¯ææ¯è¾è¿ç®ç¬¦ï¼PHPä¼æ¥è¯æ³é误
$value = 3;
define('DEFINE_VAR4', $value);
//const CONST_VAR4 = $value ; //constä¸æ¯æåéå½¢å¼çå¼ï¼PHPä¼æ¥è¯æ³é误
define('DEFINE_VAR5', true || false);
//const CONST_VAR5 = true || false ; //constä¸æ¯æé»è¾è¿ç®ç¬¦ï¼PHPä¼æ¥è¯æ³é误
define('DEFINE_VAR6', 'Hello'.' World!');
//const CONST_VAR6 = 'Hello'.' World!' ; //constä¸æ¯æå符串è¿ç®ç¬¦ï¼PHPä¼æ¥è¯æ³é误
class User{
}$user = new User();define('DEFINE_VAR7', $user instanceof User);
//const CONST_VAR7 = $user instanceof User ; //constä¸æ¯æç±»åè¿ç®ç¬¦ï¼PHPä¼æ¥è¯æ³é误
?>
4.对å符大å°åææçæ¯æå·®å¼
é¤ä¸è¿°3个åºå«å¤ï¼è¿æä¸ä¸ªä¸å¤ªèµ·ç¼çåºå«ãå½æ°define()å¯ä»¥æ¥æ¶ç¬¬3个åæ°ï¼å¦æ该åæ°ä¸ºtrueï¼å表示常éåç大å°åä¸ææãè使ç¨constå ³é®åå®ä¹å¸¸éå´æ²¡ææä¾ç±»ä¼¼çåè½ã
<?php
//设置ç¼ç 为UTF-8ï¼ä»¥é¿å ä¸æä¹±ç
header('Content-Type:text/html;charset=utf-8');
//define()ç第3个åæ°ä¸ºtrueæ¶ï¼è¡¨ç¤ºå¤§å°åä¸ææ
define('SITE_NAME', 'CodePlayer', true);
echo SITE_NAME; //è¾åºï¼CodePlayer
echo site_name; //è¾åºï¼CodePlayer
echo SiTe_NamE; //è¾åºï¼CodePlayer
const DOMAIN_NAME = '365mini.com';
echo DOMAIN_NAME; //è¾åºï¼365mini.com
echo domain_name; //PHPæ示常éæªå®ä¹
echo DomaIN_nAMe; //PHPæ示常éæªå®ä¹
?>
<?php
define('PI', 3.14159); //å®ä¹ä¸ä¸ªå为PIç常é
echo PI; //è¾åºï¼3.14159
?>
ä¸è¿ï¼å¨PHP 5.3.0ä¹åï¼é¤äºä½¿ç¨å½æ°define()ä¹å¤ï¼æ们è¿å¯ä»¥ä½¿ç¨PHPå ³é®åconstæ¥å®ä¹å¸¸éã
ä¾å¦ï¼
<?php
//以ä¸ä»£ç éå¨PHP 5.3.0åä¹åççæ¬ä¸è¿è¡
const PI = 3.14159; //使ç¨constå ³é®åå®ä¹ä¸ä¸ªå为PIç常é
echo PI; //è¾åºï¼3.14159
?>
è½ç¶ä¸è¿°ä¸¤ç§æ¹å¼åå¯ä»¥å®ä¹å¸¸éï¼ä½æ¯å®ä»¬ä¹é´æä»ä¹ä¸åä¹å¤å¢ãä¸é¢æ们æ¥ä¸ä¸è®²è§£PHPä¸define()å½æ°åconstå ³é®åå®ä¹å¸¸éçåºå«ï¼
1.çæ¬å·®å¼
é¦å ï¼æ¯«æ çé®çæ¯ï¼ä¸¤ç§å®ä¹å¸¸éçæ¹å¼ä¹é´åå¨çæ¬å·®å¼ï¼å½æ°define()å¨PHP4åPHP5ä¸åå¯ä½¿ç¨ï¼å ³é®åconståªè½å¨PHP 5.3.0åå ¶åççæ¬ä¸ä½¿ç¨ã
2.å®ä¹ä½ç½®çåºå«
ç±äºå½æ°define()å®ä¹ç常éæ¯å¨æ§è¡define()å½æ°æ¶å®ä¹çï¼å æ¤å¯ä»¥å¨å½æ°å ã循ç¯å ãifè¯å¥å çå½æ°è½å¤è¢«è°ç¨çä»»ä½å°æ¹ä½¿ç¨define()å½æ°å®ä¹å¸¸éãä¸define()ä¸åçæ¯ï¼ç±äºconstå ³é®åå®ä¹ç常éæ¯å¨ç¼è¯æ¶å®ä¹çï¼å æ¤constå ³é®åå®ä¹å¸¸éå¿ é¡»å¤äºæ顶端çä½ç¨åºåãè¿ä¹å°±æå³çä¸è½å¨å½æ°å ã循ç¯å 以åifè¯å¥ä¹å ç¨constæ¥å®ä¹å¸¸éã
<?php
//使ç¨constå ³é®åå®ä¹å¸¸éå¿ é¡»å¤äºæ顶端çä½ç¨åºå
//ä¹å°±æ¯å¯ä»¥å¨ç¼è¯æ¶ç´æ¥è§£æå®ä¹çå°æ¹
const DEMO = 'DEMO';
class Person{
const MAN = 'ç·';
const WOMAN = '女';
}
interface USB{
const VERSION_2 = '2.0';
const VERSION_3 = '3.0';
}
?>
3.对å¼ç表达å¼æ¯æçå·®å¼
è½ç¶å ³é®åconstådefine()å®ä¹ç常éå¼é½åªè½ä¸ºnullææ éæ°æ®(booleanï¼integerï¼floatåstringç±»å)以åresourceç±»å(ä¸æ¨èå®ä¹resourceç±»åç常éï¼å¦åå¯è½åºç°æ æ³é¢ç¥çç»æ)ãä¸è¿ï¼ç±äºå ³é®åconstå®ä¹å¸¸éæ¯å¨ç¼è¯æ¶å®ä¹çï¼å æ¤constå ³é®åå®ä¹ç常éå¼ç表达å¼ä¸ä¸æ¯æç®æ¯è¿ç®ç¬¦ãä½è¿ç®ç¬¦ãæ¯è¾è¿ç®ç¬¦çå¤ç§è¿ç®ç¬¦ï¼èè¿äºè¿ç®ç¬¦å¨define()å½æ°å®ä¹å¸¸éæ¶é½æ¯å¯ä»¥ç´æ¥ä½¿ç¨çã
<?php
define('DEFINE_VAR1', 1 << 1);
//const CONST_VAR1 = (1 << 1); //constä¸æ¯æä½è¿ç®ç¬¦ï¼PHPä¼æ¥è¯æ³é误
define('DEFINE_VAR2', 1 + 1);
//const CONST_VAR2 = 1 + 1 ; //constä¸æ¯æç®æ¯è¿ç®ç¬¦ï¼PHPä¼æ¥è¯æ³é误
define('DEFINE_VAR3', 1 == 1);
//const CONST_VAR3 = 1 == 1 ; //constä¸æ¯ææ¯è¾è¿ç®ç¬¦ï¼PHPä¼æ¥è¯æ³é误
$value = 3;
define('DEFINE_VAR4', $value);
//const CONST_VAR4 = $value ; //constä¸æ¯æåéå½¢å¼çå¼ï¼PHPä¼æ¥è¯æ³é误
define('DEFINE_VAR5', true || false);
//const CONST_VAR5 = true || false ; //constä¸æ¯æé»è¾è¿ç®ç¬¦ï¼PHPä¼æ¥è¯æ³é误
define('DEFINE_VAR6', 'Hello'.' World!');
//const CONST_VAR6 = 'Hello'.' World!' ; //constä¸æ¯æå符串è¿ç®ç¬¦ï¼PHPä¼æ¥è¯æ³é误
class User{
}$user = new User();define('DEFINE_VAR7', $user instanceof User);
//const CONST_VAR7 = $user instanceof User ; //constä¸æ¯æç±»åè¿ç®ç¬¦ï¼PHPä¼æ¥è¯æ³é误
?>
4.对å符大å°åææçæ¯æå·®å¼
é¤ä¸è¿°3个åºå«å¤ï¼è¿æä¸ä¸ªä¸å¤ªèµ·ç¼çåºå«ãå½æ°define()å¯ä»¥æ¥æ¶ç¬¬3个åæ°ï¼å¦æ该åæ°ä¸ºtrueï¼å表示常éåç大å°åä¸ææãè使ç¨constå ³é®åå®ä¹å¸¸éå´æ²¡ææä¾ç±»ä¼¼çåè½ã
<?php
//设置ç¼ç 为UTF-8ï¼ä»¥é¿å ä¸æä¹±ç
header('Content-Type:text/html;charset=utf-8');
//define()ç第3个åæ°ä¸ºtrueæ¶ï¼è¡¨ç¤ºå¤§å°åä¸ææ
define('SITE_NAME', 'CodePlayer', true);
echo SITE_NAME; //è¾åºï¼CodePlayer
echo site_name; //è¾åºï¼CodePlayer
echo SiTe_NamE; //è¾åºï¼CodePlayer
const DOMAIN_NAME = '365mini.com';
echo DOMAIN_NAME; //è¾åºï¼365mini.com
echo domain_name; //PHPæ示常éæªå®ä¹
echo DomaIN_nAMe; //PHPæ示常éæªå®ä¹
?>
温馨提示:答案为网友推荐,仅供参考