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

How to use define to define constants in PHP

autoload
Release: 2023-03-09 11:20:02
Original
2465 people have browsed it

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   )
Copy after login
  • $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; 
?>
Copy after login
输出:
php.cn
Warning: Use of undefined constant Ok - assumed &#39;Ok&#39; (this will throw an Error in a future version of PHP)
Copy after login

b.Not size sensitive Write

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

c. Allowed to be array

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template