Summary of knowledge about constants in PHP

墨辰丷
Release: 2023-03-27 18:04:01
Original
997 people have browsed it

This article mainly introduces relevant information about constant knowledge in PHP. Friends who need it can refer to it

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. The constant name does not require a dollar sign at the beginning ($)

  3. Scope does not affect access to constants

  4. Constant values ​​can only be strings or numbers

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 it must be flexible and convenient to use.

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 entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHPHow to use curl to simulate post uploading and receiving files_php tips

PHPAchieving the effect of imitating Baidu library and Docin online document (word, excel, ppt to flash)_php skills

php mailer class calls the remote SMTP server to send mail implementation method_php skills

The above is the detailed content of Summary of knowledge about 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!