How to define global variables in PHP? How to declare? what is the function? (Summarize)

慕斯
Release: 2023-03-10 11:00:02
Original
4092 people have browsed it

The previous article introduced you to "The little-known function variable scope (implementation steps)". This article continues to introduce to you how to define global variables? How to declare? What works? Let’s learn together!

How to define global variables in PHP? How to declare? what is the function? (Summarize)

Global variables:

  • Ordinary variables: Ordinary variables refer to variables declared outside the function, and their scope is the entire page part, but cannot be used inside the function;

  • Global variables: Variables that can be accessed and used both inside and outside the function are global variables;

<?php
     /****** 普通变量*/
     $pome = &#39;你最喜欢的唯美古诗词有哪些?&#39;;
     function pome(){
         echo $pome;
     }
     pome();
  
  
?>
Copy after login

Demo results:

How to define global variables in PHP? How to declare? what is the function? (Summarize)

Code analysis:

Define a function, pome() , and then can we use the function to define variables outside? According to the results of running our code, it can be seen that it does not work. The result still shows that it is undefined. That is to say, when we define a variable inside the function and then output (echo), the result will appear that we have not defined this variable, so we know that ordinary variables It refers to the variables declared outside the function. Its scope is part of the entire page and cannot be used inside the function;

The code is demonstrated as follows:

<?php
     /****** 普通变量*/
     $pome = &#39;你最喜欢的唯美古诗词有哪些?&#39;;
     function pome(){
         echo $pome;
     }
     pome();
     echo $pome;
     
  
   
?>
Copy after login

How to define global variables in PHP? How to declare? what is the function? (Summarize)

##Code analysis:

First define a variable outside the function, and then define a function function demo() inside the function. If we want to make a global variable, we need to declare it inside the function. A variable with the same name as the outside. After declaring the variable, we need to use a keyword called (global) to declare a variable with the same name as the outside. At this time, it is a global variable, and finally output (echo) this content. The demonstration code is as follows:

<?php
     /****** 全局变量*/
     $str = &#39;树叶落到木地板上,说了一句,我喜欢你&#39;;
     function demo(){
         global $str;
         echo $str;
     }
  demo();
   
?>
Copy after login

1How to define global variables in PHP? How to declare? what is the function? (Summarize)

What happens when we try to change the value of $str inside the function? The demo code is as follows:

<?php
     /****** 全局变量*/
     $str = &#39;树叶落到木地板上,说了一句,我喜欢你&#39;;
     function demo(){
         global $str;
         echo $str;
         $str = &#39;我也喜欢你&#39;;
     }
  echo &#39;这是第一个在函数外部输出:&#39;. $str;
  echo &#39;<hr/>&#39;;
  echo &#39;这是在函数内部输出&#39;.demo();
  echo &#39;<hr/>&#39;;
  echo &#39;这是第二个在函数外部输出:&#39;. $str;
?>
Copy after login
The code demo is as follows:

How to define global variables in PHP? How to declare? what is the function? (Summarize)

In summary:

Method 1 to implement global variables:

  • Have a variable inside the function

  • Also have a variable with the same name outside the function

  • Use the global keyword to modify the variable with the same name inside the function to achieve global variables

Note: global declares the global Assignment of variables is prohibited.

If a variable has been assigned a value inside the function, and then uses global to make a global declaration, the result is that the value of the variable becomes the value of the variable outside the function

Achieving globalization Variable method 2:

$GLOBALS

-Within the function, you can use $GL0BALS[variable name] to directly call the variable value outside the function. At this time, it can be used inside and outside the function. , another way of

global variables is implemented.

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to define global variables in PHP? How to declare? what is the function? (Summarize). 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