หากต้องการเรียกใช้ฟังก์ชันจากสตริงที่เก็บไว้ในตัวแปร ให้ใช้ $func
ไวยากรณ์มีดังนี้
$func=’anyFunctionName’;
ตัวอย่าง
รหัส PHP มีดังต่อไปนี้
<!DOCTYPE html>
<html>
<body>
<?php
function hello(){
echo "Calling hello method."."<br>";
}
function welcome(){
echo "Calling welcome method."."<br>";
}
$func = 'hello';
$func();
?>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้
Calling hello method.