常量在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)判断函数是否存在
实例代码如下:
常量和变量相比,不同点:
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的Intl扩展是一个非常实用的工具,它提供了一系列国际化和本地化的功能。本文将介绍如何使用PHP的Intl扩展。一、安装Intl扩展在开始使用Intl扩展之前,需要安装该扩展。在Windows下,可以在php.ini文件中打开该扩展。在Linux下,可以通过命令行安装:Ubuntu/Debian:sudoapt-getinstallphp7.4-

CakePHP是一个开源的PHPMVC框架,它广泛用于Web应用程序的开发。CakePHP具有许多功能和工具,其中包括一个强大的数据库查询构造器,用于交互性能数据库。该查询构造器允许您使用面向对象的语法执行SQL查询,而不必编写繁琐的SQL语句。本文将介绍如何使用CakePHP中的数据库查询构造器。建立数据库连接在使用数据库查询构造器之前,您首先需要在Ca

随着网络技术的发展,PHP已经成为了Web开发的重要工具之一。而其中一款流行的PHP框架——CodeIgniter(以下简称CI)也得到了越来越多的关注和使用。今天,我们就来看看如何使用CI框架。一、安装CI框架首先,我们需要下载CI框架并安装。在CI的官网(https://codeigniter.com/)上下载最新版本的CI框架压缩包。下载完成后,解压缩

PHP是一种非常受欢迎的编程语言,它允许开发者创建各种各样的应用程序。但是,有时候在编写PHP代码时,我们需要处理和验证字符。这时候PHP的Ctype扩展就可以派上用场了。本文将就如何使用PHP的Ctype扩展展开介绍。什么是Ctype扩展?PHP的Ctype扩展是一个非常有用的工具,它提供了各种函数来验证字符串中的字符类型。这些函数包括isalnum、is

作为一种流行的前端框架,Vue能够提供开发者一个便捷高效的开发体验。其中,单文件组件是Vue的一个重要概念,使用它能够帮助开发者快速构建整洁、模块化的应用程序。在本文中,我们将介绍单文件组件是什么,以及如何在Vue中使用它们。一、单文件组件是什么?单文件组件(SingleFileComponent,简称SFC)是Vue中的一个重要概念,它

PHP是一种流行的服务器端脚本语言,它可以处理网页上的动态内容。PHP的geoip扩展可以让你在PHP中获取有关用户位置的信息。在本文中,我们将介绍如何使用PHP的geoip扩展。什么是PHP的GeoIP扩展?PHP的geoip扩展是一个免费的、开源的扩展,它允许你获取有关IP地址和位置信息的数据。该扩展可以与GeoIP数据库一起使用,这是一个由MaxMin

PHP的DOM扩展是一种基于文档对象模型(DOM)的PHP库,可以对XML文档进行创建、修改和查询操作。该扩展可以使PHP语言更加方便地处理XML文件,让开发者可以快速地实现对XML文件的数据分析和处理。本文将介绍如何使用PHP的DOM扩展。安装DOM扩展首先需要确保PHP已经安装了DOM扩展,如果没有安装需要先安装。在Linux系统中,可以使用以下命令来安

PHP是一种广泛使用的服务器端脚本语言,而CodeIgniter4(CI4)是一个流行的PHP框架,它提供了一种快速而优秀的方法来构建Web应用程序。在这篇文章中,我们将通过引导您了解如何使用CI4框架,来使您开始使用此框架来开发出众的Web应用程序。1.下载并安装CI4首先,您需要从官方网站(https://codeigniter.com/downloa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),