Home > Backend Development > PHP Tutorial > Summary of how to use php constants

Summary of how to use php constants

伊谢尔伦
Release: 2023-03-11 22:36:01
Original
1627 people have browsed it

This article mainly introduces the relevant information on the knowledge of constants in PHP. Friends in need can refer to the following

Constants in PHP are divided into custom constants and System constants

Characteristics of constants:

  1. After setting, the value of the constant cannot be changed

  2. Constant name The leading dollar sign ($) is not required

  3. Scope does not affect access to constants

  4. Constant values ​​can only be characters String or number

Get the value of a constant:

  • Use the constant name to get the value directly;

  • Use ==constant()== Function, it has the same effect as directly using the constant name to output, but the function can dynamically output different constants, so you need to use Flexible and convenient.

defined() function: Determine whether a constant has been defined

define("PI",3.14);

$is1 = defined('PI');
$is2 = defined('PI1');

var_dump($is1); // boolean true
var_dump($is2); // boolean false
Copy after login

1. Custom constant

In PHP, use the ==define()== function to define a constant

define("PI",3.14);
echo constant('PI');

define("GREETING","Hello world!",TRUE);
echo constant('greeting');
Copy after login

2. System constants

Common system constants are:

1. FILE: PHP program file name. It can help us get the physical location of the current file on the server.

2. LINE: The number of lines in the PHP program file. It can tell us which line the current code is on.

3. PHP_VERSION: The version number of the current parser. It can tell us the version number of the current PHP parser, and we can know in advance whether our PHP code can be parsed by the PHP parser.

4. PHP_OS: The name of the operating system that executes the current PHP version. It can tell us the name of the operating system used by the server and we can optimize our code according to that operating system.

echo FILE; // E:\Web\Project\_wamp\demo.php
echo LINE; // 3
echo PHP_VERSION; // 5.6.25
echo PHP_OS; // WINNT
Copy after login

The above is the detailed content of Summary of how to use php constants. 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