Scope of php constants

(*-*)浩
Release: 2023-02-23 19:40:01
Original
3315 people have browsed it

Every variable in PHP has a scope for it, which is the area within which the variable (and thus its value) can be accessed.

Scope of php constants

#For starters, the scope of variables is the page they reside on. Therefore, if you define $var, the rest of the page can access $var, but other pages generally cannot access it (unless special variables are used). (Recommended study: PHP Programming from Beginner to Master)

Because included files work as if they were part of the original (included) script, variables defined before the include() line Available to included files. Additionally, variables defined within the include file are available to the parent (include) script after the include() line.

All of this will become less obvious when using your own defined functions. These functions have their own scope, which means that variables used within a function cannot be used outside it, and variables defined outside a function cannot be used inside it. For this reason, variables inside a function can have the same name as variables outside it, but they will still be completely different variables with different values. For most junior programmers, this is a confusing concept.

To change the scope of variables within a function, you can use the global statement.

<?php
function function_name() {
    global $var;
}
 
$var = 20;
function_name(); // Function call.
?>
Copy after login

In this example, $var inside the function is now the same as $var outside the function. This means that the variable $var already has a value of 20, and if this value is changed inside the function, the value of $var outside will also change.

Another way to avoid variable scope is to use superglobal variables: $_GET, $_POST, $_REQUEST, etc. These variables are automatically accessible within your function (thus, they are superglobal variables). You can also add elements to the $GLOBALS array so that they can be used within functions.

In other words, it is best not to use global variables within functions. When designing functions, you should make them accept every value as a parameter as needed and return any value as needed. Relying on global variables within functions would make them more context-dependent and therefore less useful.

Once declared, constants will be globally visible, that is, they can be used inside and outside functions, but this is only limited to PHP scripts included in a page (including the PHP scripts we include through include and include_once) , but it cannot be used in other pages.

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