Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP Variable Scope (ขอบเขตตัวแปร)


Introduction (บทนำ)

ในการเขียนโปรแกรม scope หมายถึงขอบเขตที่ตัวแปรสามารถเข้าถึงได้ โดยทั่วไป PHP script แบบง่าย (ไม่มี constructs เช่น loop, function ฯลฯ) มี single scope กล่าวคือ ตัวแปรพร้อมใช้งานตลอดโปรแกรมตั้งแต่จุดที่นิยาม

ตัวแปรใน main script ยังพร้อมใช้งานใน script อื่นที่ include หรือ require ด้วย ในตัวอย่างต่อไป test.php ถูก include ใน main script

นี่คือ main script

$var=100; 
include "test.php"; 
?>

ไฟล์ test.script ที่ include มีดังนี้ −

echo "value of of \$var in testscript.php : " . $var; 
?>

เมื่อ run main script จะได้ผลลัพธ์ดังนี้

value of of $var in testscript.php : 100

อย่างไรก็ตาม เมื่อ script มี user-defined function ตัวแปรภายในจะมี local scope ดังนั้น ตัวแปรที่นิยามใน function ไม่สามารถเข้าถึงได้จากภายนอก ตัวแปรที่นิยามนอก function (ด้านบน) มี global scope

ตัวอย่าง

<?php 
$var=100; //global variable 
function myfunction(){ 
    $var1="Hello"; //local variable 
    echo "var=$var var1=$var1" . "\n"; 
} 
myfunction(); 
echo "var=$var var1=$var1" . "\n"; 
?>

ผลลัพธ์

จะได้ผลลัพธ์ดังนี้ −

var= var1=Hello 
var=100 var1= 
PHP Notice: Undefined variable: var in line 5 
PHP Notice: Undefined variable: var1 in line 8

สังเกตว่า global variable ไม่พร้อมใช้งานใน local scope ของ function อัตโนมัติ ตัวแปรใน function ก็เข้าถึงไม่ได้จากภายนอก

global keyword

การเข้าถึง global variable ภายใน local scope ต้องเปิดใช้งานอย่างชัดเจนด้วย global keyword โค้ด PHP มีดังนี้ −

ตัวอย่าง

<?php 
$a=10; 
$b=20; 
echo "before function call a = $a b = $b" . "\n"; 
function myfunction(){ 
    global $a, $b; 
    $c=($a+$b)/2; 
    echo "inside function a = $a b = $b c = $c" . "\n"; 
    $a=$a+10; 
} 
myfunction(); 
echo "after function a = $a b = $b c = $c"; 
?>

ผลลัพธ์

จะได้ผลลัพธ์ดังนี้ −

before function call a = 10 b = 20 
inside function a = 10 b = 20 c = 15 
PHP Notice: Undefined variable: c in line 13 
after function a = 20 b = 20 c =

Global variable ตอนนี้สามารถประมวลผลใน function ได้ อีกทั้ง การเปลี่ยนแปลง global variable ใน function จะสะท้อนใน global namespace

$GLOBALS array

PHP เก็บ global variables ทั้งหมดใน associative array ชื่อ $GLOBALS ชื่อและค่าของตัวแปรเป็น key-value pair

ใน PHP script ต่อไป ใช้ $GLOBALS array เพื่อเข้าถึง global variables −

ตัวอย่าง

<?php 
$a=10; 
$b=20; 
echo "before function call a = $a b = $b" . "\n"; 
function myfunction(){ 
    $c=($GLOBALS['a']+$GLOBALS['b'])/2; 
    echo "c = $c" . "\n"; 
    $GLOBALS['a']+=10; 
} 
myfunction(); 
echo "after function a = $a b = $b c = $c"; 
?>

ผลลัพธ์

จะได้ผลลัพธ์ดังนี้ −

before function call a = 10 b = 20 
c = 15 
PHP Notice: Undefined variable: c line 12 
Notice: Undefined variable: c in line 12 
after function a = 20 b = 20 c =

static variable

ตัวแปรที่นิยามด้วย static keyword จะไม่ถูก initialize ใหม่ในทุกครั้งที่เรียก function และเก็บค่าจากการเรียกครั้งก่อนไว้

ตัวอย่าง

<?php 
function myfunction(){ 
    static $x=0; 
    echo "x = $x" . "\n"; 
    $x++; 
} 
for ($i=1; $i<=3; $i++){ 
    echo "call to function :$i : "; 
    myfunction(); 
} 
?>

ผลลัพธ์

จะได้ผลลัพธ์ดังนี้

call to function :1 : x = 0 
call to function :2 : x = 1 
call to function :3 : x = 2