使用 URL 将参数传递给函数

Pass parameter to function using URL

这是我试图从 url

中调用的函数
 public function getParentId($childId)
 {
    $statment = $this->db->prepare("SELECT parent FROM `person` WHERE id = $childId");
    $statment->execute();
    $result = $statment->fetchAll();

    foreach($result as $output){

        return $output['parent'];
    }
    echo 'You call the function from url';

 }

我写了这段代码来访问 url

中的函数
 if(isset($_GET['action'])){
    include_once('Family.php');
    $object = new Family;

    switch($_GET['action']){
        case 'getId':
        $object->getParentId($childId);
        break;

        default;
        echo 'There is no function with that name';
        break;
    }
}

然后我从 Url

传递参数

test.local/Family.php?action=getId&childId=4

输出你从url

调用函数

但他没有 return$output['parent'] 为什么?

我想你是想写第二段代码 像这样:

if(isset($_GET['action'])){
    include_once('Family.php');
    $object = new Family;

    switch($_GET['action']){
        case 'getId':
        $object->getParentId($_GET['childId']); //<= Edit
        break;

        default;
        echo 'There is no function with that name';
        break;
    }
}