Home > Backend Development > PHP Problem > Are php constants global?

Are php constants global?

(*-*)浩
Release: 2023-02-25 22:58:01
Original
3513 people have browsed it

PHP Constants

Are php constants global?

Constant is an identifier (name) of a single value. This value cannot be changed in script.

Valid constant names begin with a character or an underscore (there is no $ sign in front of the constant name). (Recommended learning: PHP video tutorial)

Note: Unlike variables, constants are automatically global throughout the entire script.

Set PHP constants

To set constants, use the define() function - it uses three parameters:

The first parameter defines the constant The name

The second parameter defines the value of the constant

The optional third parameter specifies whether the constant name is case-insensitive. The default is false.

The following example creates a case-sensitive constant with the value "Welcome to php.cn!":

Example

<?php
define("GREETING", "Welcome to php.cn!");
echo GREETING;
?>
Copy after login

Constants are global

Constants are automatically global and can be used throughout the entire script.

The following example uses a constant inside a function even though it is defined outside the function:

Example

<?php
define("GREETING", "Welcome to php.cn!");

function myTest() {
    echo GREETING;
}
 
myTest();
?>
Copy after login

The above is the detailed content of Are php constants global?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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