在PHP中,超全局變量包括$GLOBALS
、$_SERVER
、$_GET
、$_POST
、$_COOKIE
、$_FILES
和$_SESSION
。要測試這些變量,您可以創建一個簡單的PHP腳本,然后通過這些變量來存儲和檢索數據。以下是一些示例:
$GLOBALS
變量:<?php
$GLOBALS['globalVar'] = 'I am a global variable';
echo $GLOBALS['globalVar']; // 輸出 "I am a global variable"
?>
$_SERVER
變量:<?php
$_SERVER['serverVar'] = 'I am a server variable';
echo $_SERVER['serverVar']; // 輸出 "I am a server variable"
?>
$_GET
變量:<?php
$_GET['getVar'] = 'I am a get variable';
echo $_GET['getVar']; // 輸出 "I am a get variable" (當通過URL參數傳遞時)
?>
$_POST
變量:<?php
$_POST['postVar'] = 'I am a post variable';
echo $_POST['postVar']; // 輸出 "I am a post variable" (當通過表單提交時)
?>
$_COOKIE
變量:<?php
$_COOKIE['cookieVar'] = 'I am a cookie variable';
echo $_COOKIE['cookieVar']; // 輸出 "I am a cookie variable" (當通過cookie設置時)
?>
$_FILES
變量:<?php
$_FILES['fileVar']['name'] = 'I am a file variable';
echo $_FILES['fileVar']['name']; // 輸出 "I am a file variable" (當上傳文件時)
?>
$_SESSION
變量:<?php
session_start();
$_SESSION['sessionVar'] = 'I am a session variable';
echo $_SESSION['sessionVar']; // 輸出 "I am a session variable" (當啟動會話時)
?>
要測試這些超全局變量,您可以將這些示例代碼保存到一個PHP文件中,然后通過瀏覽器訪問該文件。您還可以使用PHP的內置函數,如var_dump()
或print_r()
,來查看變量的內容和結構。