日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php globals_PHP $ GLOBALS(超级全局变量),带有示例

發布時間:2023/12/1 php 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php globals_PHP $ GLOBALS(超级全局变量),带有示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

php globals

PHP $全球 (PHP $GLOBALS)

PHP $GLOBALS is the only superglobal that does not begin with an underscore (_). It is an array that stores all the global scope variables.

PHP $ GLOBALS是唯一不以下劃線( _ )開頭的超全局變量。 它是一個存儲所有全局范圍變量的數組。

$GLOBALS in PHP is used to access all global variables (variables from global scope) i.e. the variable that can be accessed from any scope in a PHP script.

PHP中的$ GLOBALS用于訪問所有全局變量(來自全局范圍的變量),即可以從PHP腳本中的任何范圍訪問的變量。

Example of $GLOBALS in PHP

PHP中的$ GLOBALS的示例

We will see how to access a variable defined globally with the $GLOBALS superglobal?

我們將看到如何使用$ GLOBALS superglobal訪問全局定義的變量?

PHP代碼演示$ GLOBALS示例 (PHP code to demonstrate example of $GLOBALS)

<?php//global variable$name = 'my name is sam';//functionfunction sayName() {echo $GLOBALS['name'];}//calling the functionsayName(); ?>

Output

輸出量

my name is sam

By defining the $name variable it automatically is stored in the superglobal variable $GLOBALS array. This explains why we can access it in the sayName() function without defining it in the function.

通過定義$ name變量,它會自動存儲在超全局變量$ GLOBALS數組中。 這就解釋了為什么我們可以在sayName()函數中訪問它而無需在函數中定義它的原因。

PHP代碼通過使用$ GLOBALS訪問全局變量來查找兩個數字的和 (PHP code to find sum of two numbers by accessing global variables using $GLOBALS)

<?php//global variables$num1 = 36;$num2 = 24;//function to access global variablesfunction add2Numbers() {$GLOBALS['sum'] = $GLOBALS['num1'] + $GLOBALS['num2'];}//calling functionadd2Numbers();//printing sum using global variableecho $sum; ?>

Output

輸出量

60

In the code above, num1 and num2 are global variables so we are accessing them using $GLOBALS, and sum is a variable present within $GLOBALS, thus, it is accessible from outside the function also.

在上面的代碼中, num1和num2是全局變量,因此我們使用$ GLOBALS訪問它們,并且sum是$ GLOBALS中存在的變量,因此,也可以從函數外部對其進行訪問。

翻譯自: https://www.includehelp.com/php/$GLOBALS-super-global-variable-with-example.aspx

php globals

總結

以上是生活随笔為你收集整理的php globals_PHP $ GLOBALS(超级全局变量),带有示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。