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

PHP เรียกฟังก์ชันจากสตริงที่เก็บในตัวแปร

เพื่อเรียกฟังก์ชันจากสตริงที่เก็บในตัวแปร ใช้ $func

Syntax ดังนี้

$func='anyFunctionName';

ตัวอย่าง

โค้ด PHP ดังนี้

<!DOCTYPE html> <html> <body> <?php 
function hello(){ 
    echo "Calling hello method."."
"; } function welcome(){ echo "Calling welcome method."."
"; } $func = 'hello'; $func(); ?> </body> </html>

ผลลัพธ์

นี่จะผลิตเอาต์พุตดังนี้

Calling hello method.