


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!
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 = '你最喜欢的唯美古诗词有哪些?'; function pome(){ echo $pome; } pome(); ?>
Demo results:
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 = '你最喜欢的唯美古诗词有哪些?'; function pome(){ echo $pome; } pome(); echo $pome; ?>
##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 = '树叶落到木地板上,说了一句,我喜欢你'; function demo(){ global $str; echo $str; } demo(); ?>
<?php /****** 全局变量*/ $str = '树叶落到木地板上,说了一句,我喜欢你'; function demo(){ global $str; echo $str; $str = '我也喜欢你'; } echo '这是第一个在函数外部输出:'. $str; echo '<hr/>'; echo '这是在函数内部输出'.demo(); echo '<hr/>'; echo '这是第二个在函数外部输出:'. $str; ?>The code demo is as follows:
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 functionAchieving 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!

C++局部变量和全局变量的区别:可见性:局部变量仅限于定义函数,而全局变量在整个程序中可见。内存分配:局部变量在栈上分配,而全局变量在全局数据区分配。作用域:局部变量在函数内,而全局变量在整个程序中。初始化:局部变量在函数调用时初始化,而全局变量在程序启动时初始化。重新创建:局部变量在每次函数调用时重新创建,而全局变量仅在程序启动时创建。

go语言没有静态全局变量,它使用了一种更为灵活的方式来处理全局变量的需求,全局变量通常是在包级别被声明,也就是在函数外部声明的变量,这些全局变量在整个包中均是可见的,可以在包中的任何函数中使用。

request的中文意思为“请求”,是php中的一个全局变量,是一个包含了“$_POST”、“$_GET”和“$_COOKIE”的数组。“$_REQUEST”变量可以获取POST或GET方式提交的数据、COOKIE信息。

随着JavaScript的流行,越来越多的网站和应用程序都依赖于JavaScript。然而,JavaScript中全局变量的使用可能存在安全问题。在此文中,我将介绍如何在JavaScript中实现全局变量的安全性。避免使用全局变量最好的方法是避免使用全局变量。在JavaScript中,所有变量都默认为全局变量,除非它们在函数中声明。因此,应尽可能使用局部变量

Golang是一种强类型编程语言,具有高效、简洁、并发等特点,因此逐渐受到了越来越多的开发者的青睐。而在Golang的开发中,函数的全局变量和局部变量往往会涉及到数据竞争的问题。本文将从实际编码的角度,对Golang函数中全局变量和局部变量的数据竞争问题进行分析。一、全局变量的数据竞争Golang全局变量在所有函数中均可以访问,因此如果不进行严谨的设计和编码

我们将了解在不初始化的情况下重新声明全局变量、通过初始化重新声明全局变量、重新声明全局变量并初始化两次时,C和C++的行为有何不同。另外,我们将使用局部变量重复上述组合。1.A)C程序:重新声明全局变量而不进行初始化#include<stdio.h>intvar;intvar;intmain(){ printf("Var=%d",var); return0;}输出Var=0B)C++程序:

是的,Go函数在Goroutine中默认情况下可以直接访问全局变量。原因:Goroutine继承创建它的Goroutine的内存空间,包括对全局变量的访问权限。

php全局变量有:1、$_SERVER,当前脚本运行的服务器和执行环境信息的超全局变量;2、$_GET,通过GET方法传递给当前脚本的变量的关联数组;3、$_POST,通过POST方法传递给当前脚本的变量的关联数组;4、$_SESSION,存储当前会话中用户相关信息;5、$_COOKIE,通过HTTP Cookie传递给当前脚本的变量的关联数组;6、$_FILES等等。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
