PHP constants, define () and defined () function

  • What is constant?

Constants can be understood as an amount (e.g., pi) a constant value; or the constant value is defined, may not be changed at any elsewhere in the script. PHP constants in the divided custom constants and systems constants.

You may be used defeine () function to define the constant

Explanation

define ( string $name , mixed $value [, bool $case_insensitive = false ] ) : bool

parameter

name  constant name.

value  value of the constant; in PHP 5, value must be scalar (integer, float, string, boolean , NULL) in PHP 7 also allows a value of the array.

Warning
constants can also be defined as the resource type , but this is not recommended, as there may be unpredictable behavior.

case_insensitive If set to TRUE, this constant is not case sensitive. The default is case-sensitive. For example, CONSTANT and Constant represent different values.

Note:

Case-insensitive manner constants stored in lowercase.

Return Values
Returns TRUE on success or FALSE on failure.

Example 1

<?php
define("CONSTANT", "Hello world.");
echo CONSTANT; // 输出 "Hello world."
echo Constant; // 输出 "Constant" 并导致 Notice 类似警告warning

define("GREETING", "Hello you.", true);//true则对大小写不敏感
echo GREETING; // 输出 "Hello you."
echo Greeting; // 输出 "Hello you."

//  PHP 7 起就可以运行了
define('ANIMALS', array(
    'dog',
    'cat',
    'bird'
));
echo ANIMALS[1]; // 输出 "cat"

?>

Example 2

<?php
$p = "PII";
define("PI",3.14);//定义的常量名必须为字符串型
define($p,3.14);
echo PI;
echo "<br />";
echo PII;
?>
  • Constant role:

  • The main effect is a constant defined to avoid duplication, tampering with variable values. In the time of our development team, or a large amount of code when, for some amount does not change after the first definition, if we use variable, unknowingly, using the same variable name, variable value will be replaced, which will lead to the wrong server to perform the task.

  • In addition, constant use can improve the maintainability of the code. If for some reason, the value of the constant need to change, we only need to modify one place. For example, in doing calculations, first we take the circumference was 3.14, so many calculations we used 3.14 calculated, when required calculation accuracy is improved, pi need to take 3.142, we have to change all the code used 3.14, if the code amount relatively long time, only a heavy workload, it may also be missing.

The code to show how small the role of constants:

<?php
define("PI",3.14);
$r=3;
echo "面积为:".(PI*$r*$r)."<br />";
echo "周长为:".(2*PI*$r)."<br />";
?>

operation result:
Here Insert Picture Description

  • System constants

  • PHP system constants are already defined constants, we can directly use it, a common system constants are:

(1) FILE : PHP file name. It can help us to get the physical location of the current file server.

(2) LINE : Number of files row PHP. It can tell us, the first few lines in the current code.

(3) PHP_VERSION: The current version number of the parser. It tells us the current version number of PHP parser, we can know in advance whether our PHP code can be resolved to the PHP parser.

(4) PHP_OS: PHP execution of the current version of the operating system name. It can tell us the name of the operating system used by the server, we can optimize our code based on the operating system.

For the code chestnuts deepen the impression of a Kazakhstan:

<?php
echo __FILE__;
//php程序文件名。它可以帮助我们获取当前文件在服务器的物理位置
echo "<br />";
echo __LINE__;
//PHP程序文件行数。它可以告诉我们,当前代码在第几行。
echo "<br />";
echo PHP_VERSION;
//当前解析器的版本号。它可以告诉我们当前PHP解析器的版本号
//我们可以提前知道我们的PHP代码是否可被该PHP解析器解析。
echo "<br />";
echo PHP_OS;
//执行当前PHP版本的操作系统名称。它可以告诉我们服务器所用的操作系统名称,
//我们可以根据该操作系统优化我们的代码。
echo "<br />";
?>

Output
Here Insert Picture Description

  • How constant value

  • Defines constants, then you should use constant, then how to obtain a constant value it?

  • Gets a constant value of values in two ways.
    1. The first is to use direct access to the name of the constant value;
    2. constant () function. It directly effects the constant use of the name of the output is the same, but different functions can be dynamically output constant.

  • constant () function

Role: return to a constant value

Explanation

constant ( string $name ) : mixed

  • Returns the value of the constant by name.

  • When you do not know the name of the constant, but need to get the value of the constant, constant () is very useful. That is, constant name stored in a variable, or by the function returns the constant name.

  • This function is also applicable class constants.

parameter

  • name constant name.

return value

  • Returns the value of the constant. If the constant is not defined NULL is returned.

Errors / exceptions

  • If the constant is not defined, it will generate a E_WARNING level error.

For chestnuts

<?php

define("MAXSIZE", 100);

echo MAXSIZE;
echo "<br/>";
echo constant("MAXSIZE"); // same thing as the previous line
echo "<br/>";

interface bar {
    const test = 'foobar!';
}

class foo {
    const test = 'foobar!';
}

$const = 'test';

var_dump(constant('bar::'. $const)); // string(7) "foobar!"
echo "<br/>";
var_dump(constant('foo::'. $const)); // string(7) "foobar!"

?>

operation result
Here Insert Picture Description

  • How to determine whether the constants are defined

  • After repeated if the constant is defined, PHP parser will issue a "Constant XXX already defined" warning, reminding us of the constant has been defined. So, in the case of team development, or a large amount of code, how to determine whether a constant is defined it?

  • defined () function can help us determine whether a constant is defined, its syntax is:

bool defined(string constants_name)

  • It only parameter constant_name, refers to obtain constant name, if there is the return Boolean true, otherwise returns boolean false; (Note: bool represents the function returns a value of type Boolean type)

For chestnuts

<?php 
define("PI1",3.14);
$p = "PI1";
$is1=defined($p);
$is2=defined("PI1");
$is3=defined("PI2");
var_dump($is1);
var_dump($is2);
var_dump($is3);
?>

operation result
Here Insert Picture Description

Published 17 original articles · won praise 1 · views 893

Guess you like

Origin blog.csdn.net/weixin_43914604/article/details/97268831