常量在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()这两个方法–这说明他们都是只读而且是静态的(被所有该类的对象共享).
波比源码 » Php入门教程之PHP常量使用方法详解
purchase levofloxacin generic order levofloxacin 500mg generic
purchase avodart online buy avodart generic buy zofran 4mg pill
buy spironolactone generic diflucan generic diflucan pills
fildena 50mg usa careprost medication robaxin 500mg pill
indomethacin buy online lamisil sale amoxicillin drug
cialis 5mg uk how much is cialis sildenafil next day delivery
buy anastrozole online Best prices on viagra sildenafil 100mg
original tadalafil 20mg rezeptfrei sicher kaufen tadalafil 20mg generika rezeptfrei kaufen sildenafil 50mg kaufen ohne rezept
oral alendronate 35mg panadol without prescription purchase famotidine generic
buy generic benicar 10mg buy divalproex without prescription diamox 250mg sale
oral prograf order labetalol 100mg for sale buy generic ursodiol
buy imuran 100mcg without prescription protonix online order over the counter viagra
cialis 10mg pills purchasing cialis on the internet viagra 100mg price
avlosulfon 100mg oral order nifedipine 30mg generic perindopril 8mg usa
buy modafinil 200mg for sale generic stromectol 3mg ivermectin cream uk
azithromycin 250mg for sale generic prednisolone 5mg cheap neurontin for sale
tadalafil brand name cheap viagra 50mg sildenafil on line
cialis 40mg pills clomipramine 25mg oral anafranil 50mg cheap
buy generic itraconazole itraconazole order online tindamax 500mg pill
buy generic norvasc 10mg viagra 200mg order tadalafil 20mg generic
purchase clozapine generic clozaril 100mg usa order decadron 0,5 mg without prescription
order metoprolol 100mg order lopressor 100mg for sale buy generic levitra 10mg
buy custom research paper real viagra sites cheap viagra
order clomiphene 100mg pill clomiphene 100mg brand casino online slots
buy generic priligy 60mg generic priligy levothyroxine without prescription
tadalafil online cialis 5mg uk us pharmacy viagra
cozaar pills buy losartan sale order topiramate 100mg for sale
free spins casino real money online blackjack play slots
order keflex 500mg without prescription erythromycin uk erythromycin cheap
sildenafil online order sildenafil mail order usa generic tadalafil 5mg
order ceftin generic robaxin pills methocarbamol cost
order trazodone 50mg sale oral suhagra cost sildenafil 50mg
buy essay now buy stromectol generic stromectol tablets
prednisone 20mg canada buy amoxil for sale order amoxil 250mg online
purchase fildena pill rhinocort over the counter order rhinocort for sale
buy tadacip 10mg online cheap tadalafil 20mg for sale indomethacin 75mg cheap
terbinafine online trimox 500mg brand amoxicillin 500mg drug
biaxin online buy order clonidine 0.1 mg meclizine order online
order tiotropium bromide 9 mcg online cheap minocycline 100mg for sale terazosin medication
pioglitazone 30mg over the counter brand actos 15mg order viagra 100mg online cheap
cialis tadalafil online casinos real money best online casinos that payout
real money spins online online roulette wheel real online casino
sugarhouse casino online online slots real money online blackjack for money
real online blackjack canadian pharmacy meds need essay written
doxycycline 100mg ca order albuterol generic clindamycin us
buy olmesartan 20mg sale buy generic olmesartan cheap divalproex 500mg
buy lanoxin 250mg without prescription buy digoxin 250mg buy molnupiravir pill
amoxil 500mg generic order stromectol 3mg generic stromectol 15 mg
order alendronate 70mg pills buy generic nitrofurantoin 100 mg buy motrin generic
buy indomethacin 75mg purchase indocin pills order cenforce pill
After all, what a great site and informative posts, I will upload inbound link – bookmark this web site? Regards, Reader. Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…
buy doxycycline pills buy doxycycline methylprednisolone 8mg pills
buy pepcid 40mg pill order famotidine 40mg sale order mirtazapine 30mg pills
tadalafil 20mg generic plavix 150mg drug trimox cheap
requip 2mg us requip 2mg over the counter labetalol 100 mg cost
minocycline pills order gabapentin 600mg without prescription order hytrin 1mg online cheap
glucophage 1000mg us order metformin 500mg generic nolvadex price
accutane online buy generic ampicillin brand ampicillin 500mg
ivermectin 15 mg order prednisone 20mg online prednisone 5mg uk
ventolin canada order albuterol without prescription augmentin sale
order provigil 100mg online buy zestril 10mg for sale order metoprolol 100mg generic
order doxycycline 100mg generic levitra online buy buy zovirax 800mg sale
zocor 20mg tablet order generic promethazine 25mg purchase sildalis sale
avlosulfon 100mg sale order mesalamine pills buy tenormin 100mg generic
uroxatral 10 mg pill buy uroxatral 10 mg diltiazem generic
viagra for men viagra 150 mg generic tadalafil 20mg