探讨php define()函数及defined()函数使用详解_php技巧

The define() function defines a constant.
define()函数的作用是:定义一个常量。

Constants are much like variables, except for the following differences:
常量[constant]与变量[variable]有很多相似的地方,因此,很容易混淆;下面,我们列举一下常量[constant]与变量[variable]之间的不同点:
    A constant's value cannot be changed after it is set
    一个常量值在指定之后就不可以更改;
    Constant names do not need a leading dollar sign ($)
    设置常量时,不需要在前面加上“$”符号;
    Constants can be accessed regardless of scope
    常量可以被所有范围的域访问;
    Constant values can only be strings and numbers
    常量的值只能是“字符串[string]”和“数字[number]”;

Syntax
语法
define(name,value,case_insensitive)

Parameter
参数
Description
描述
name Required. Specifies the name of the constant
必要参数。指定常量的名称
value Required. Specifies the value of the constant
必要参数。指定常量的值
case_insensitive Optional. Specifies whether the constant name should be case-insensitive. If set to TRUE, the constant will be case-insensitive. Default is FALSE (case-sensitive)
可选参数。指定常量的名称是否是不区分大小写的[case-insensitive]。如果设置为True,则不区分字母大小写;如果设置为False,则区分字母大小写。默认值是:False

Example 1
案例1
Define a case-sensitive constant:
指定一个常量(区分大小写):

复制代码 代码如下:

<?phpdefine("GREETING","Hello you! How are you today?");echo constant("GREETING");?>

The output of the code above will be:
上述代码将输出下面的结果:

复制代码 代码如下:

Hello you! How are you today?

Example 2
案例2
Define a case-insensitive constant:
指定一个常量(不区分大小写):

复制代码 代码如下:

<?phpdefine("GREETING","Hello you! How are you today?",TRUE);echo constant("greeting");?>

The output of the code above will be:
上述代码将输出下面的结果:

复制代码 代码如下:

Hello you! How are you today?

The defined() function checks whether a constant exists.
defined()函数的作用是:检查一个常量是否存在。

Returns TRUE if the constant exists, or FALSE otherwise.
如果该常量存在,则返回True;如果不存在,则返回False。
Syntax
语法

复制代码 代码如下:

defined(name)

Parameter
参数
Description
描述
name Required. Specifies the name of the constant to check
必要参数。指定常量对象的名称

Example
案例

复制代码 代码如下:

<?phpdefine("GREETING","Hello you! How are you today?");echo defined("GREETING");?>

The output of the code above will be:
上述代码将输出下面的结果:
1

时间: 2024-08-04 14:28:13

探讨php define()函数及defined()函数使用详解_php技巧的相关文章

PHP中filter函数校验数据的方法详解_php技巧

介绍PHP中filter函数校验数据的方法详解,PHP过滤器包含两种类型:Validation用来验证验证项是否合法.Sanitization用来格式化被验证的项目,因此它可能会修改验证项的值,将不合法的字符删除. input_filters_list() 用来列出当前系统所支持的所有过滤器. 复制代码 代码如下: <?php foreach(filter_list() as $id => $filter) {     echo $filter.' '.filter_id($filter).&

php禁用函数设置及查看方法详解_php技巧

本文实例讲述了php禁用函数设置及查看方法.分享给大家供大家参考,具体如下: 打开PHP.INI,找到这行: disable_functions = 在后面那里加上要禁用的函数,如禁用多个函数,要用半角逗号 , 分开 给个例子: 复制代码 代码如下: disable_functions = passthru,exec,system,popen,chroot,scandir,chgrp,chown,escapesh ellcmd,escapeshellarg,shell_exec,proc_ope

php使用glob函数遍历文件和目录详解_php技巧

php glob()函数返回匹配指定模式的文件名或目录.因此我们可以使用glob函数来查找文件,也可以实现目录的遍历. 函数说明:array glob ( string $pattern [, int $flags ] ) 功能:寻找与模式匹配的文件路径,返回包含匹配文件(目录)的数组(注:被检查的文件必须是服务器系统的,不能用于远程文件) 参数说明:第一个参数:匹配模式:第二个可选参数: GLOB_MARK - 在每个返回的项目中加一个斜线 GLOB_NOSORT - 按照文件在目录中出现的原

PHP 函数call_user_func和call_user_func_array用法详解_php技巧

call_user_func函数是当需要动态调用函数时,才使用的,这个函数有两种用法:第一种是调用孤独的函数: 复制代码 代码如下: <?phpfunction funa($b,$c){    echo $b;    echo $c;}call_user_func('funa', "111","222");call_user_func('funa', "333","444");//显示 111 222 333 444//

php array_slice函数的使用以及参数详解_php技巧

复制代码 代码如下: <?php /* 用手册上的例子 */ $input = array ("a", "b", "c", "d", "e"); $output = array_slice ($input, 2); // returns "c", "d", and "e", $output = array_slice ($input, 2,

探讨PHP中this,self,parent的区别详解_php技巧

{一}PHP中this,self,parent的区别之一this篇面向对象编程(OOP,Object OrientedProgramming)现已经成为编程人员的一项基本技能.利用OOP的思想进行PHP的高级编程,对于提高PHP编程能力和规划web开发构架都是很有意义的.PHP5经过重写后,对OOP的支持额有了很大的飞跃,成为了具备了大部分面向对象语言的特性的语言,比PHP4有了很多的面向对象的特性.这里我主要谈的是this,self,parent 三个关键字之间的区别.从字面上来理解,分别是指

JavaScript学习小结之被嫌弃的eval函数和with语句实例详解_javascript技巧

前面的话 eval和with经常被嫌弃,好像它们的存在就是错误.在CSS中,表格被嫌弃,在网页中只是用表格来展示数据,而不是做布局,都可能被斥为不规范,矫枉过正.那关于eval和with到底是什么情况呢?本文将详细介绍eval()函数和with语句 eval 定义 eval()是一个全局函数,javascript通过eval()来解释运行由javascript源代码组成的字符串 var result = eval('3+2'); console.log(result,typeof result)

PHP加密解密函数详解_php技巧

分享一个PHP加密解密的函数,此函数实现了对部分变量值的加密的功能.加密代码如下: /* *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function passport_encrypt($str,$key){ //加密函数 srand((double)microtime() * 1000000); $encrypt_key=md5(rand(0, 32000)); $ctr=0; $tmp=''; for($i=0;$i<strlen($str);$i++){

深入php define()函数以及defined()函数的用法详解_php技巧

The define() function defines a constant.define()函数的作用是:定义一个常量.Constants are much like variables, except for the following differences: 常量[constant]与变量[variable]有很多相似的地方,因此,很容易混淆:下面,我们列举一下常量[constant]与变量[variable]之间的不同点: •A constant's value cannot be