Home  >  Article  >  Backend Development  >  Are php constants global?

Are php constants global?

(*-*)浩
(*-*)浩Original
2019-10-10 09:30:093462browse

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;
?>

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();
?>

The above is the detailed content of Are php constants global?. 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
Previous article:How to query in phpNext article:How to query in php