Home >Backend Development >PHP Problem >How to use define to define constants in PHP

How to use define to define constants in PHP

autoload
autoloadOriginal
2021-04-15 11:13:552483browse

How to use define to define constants in PHP

In PHP, variable refers to the numerical value used in the program that can be changed. The opposite is constant, constant Once the value is defined, it cannot be changed anywhere else in the script.

Syntax:

define ( string $name   , mixed $value   , bool $case_insensitive = false   )
  • $name: Constant name.

  • $value: The value of a constant; in PHP 5, value must be a scalar(int, float, string, boolean, null) in PHP 7 A value of array is allowed.

  • $case_insensitive: If set to true, this constant is case-insensitive. The default is case-sensitive. PHP 7.3.0 The definition of case-insensitive constants has been abandoned.

  • Return value: Returns true on success, or false on failure.

Usage example:

a.Case sensitive

<?php
define("OK", "Hello world.");
echo OK; 
echo Ok; 
?>
输出:
php.cn
Warning: Use of undefined constant Ok - assumed &#39;Ok&#39; (this will throw an Error in a future version of PHP)

b.Not size sensitive Write

<?php
    define("OK", "php.cn", true);
    echo OK."<br>"; 
    echo Ok; 
?>
输出:
php.cn
php.cn

c. Allowed to be array

<?php
define(&#39;People&#39;, array(
    &#39;man&#39;,
    &#39;woman&#39;,
    &#39;strick&#39;
));
echo People[1];
?>
输出:woman

Recommended: 2021 PHP Interview Questions Summary (Collection)》《php video tutorial

The above is the detailed content of How to use define to define constants in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn