php 函数 returns 零而不是预期的字符串

php Function returns zero instead of the expected string

在我的示例中,我从重力表单单选按钮字段(字段 ID 4)收集表单数据。对于 "First Choice" 的选择,字段 ID 4 的值为 0。因此,为了解决我正在使用变量 $field_id 的字段,选择值是通过使用 $entry[$field_id] (即值 0)收集的,该值的文本标签是"First Choice"。值为 0 的字段 ID 4 的预期结果是文本 "First Choice"。我需要将其打印到文本文件中。我做了以下事情:

    $field_id = 4;
    $field = GFFormsModel::get_field( $form, $field_id );
    $choice_text = $field['choices'][$entry[$field_id]]['text'];

    $data = $choice_text;

    file_put_contents($my_file,$data);
    $tmpfile = fopen($my_file, "r");
    $contents = fread($tmpfile, filesize($my_file));

上述方法将按预期将 "First Choice" 打印到我的文本文件。但是我需要用一个函数来做到这一点。如果我尝试使用以下函数执行此操作,我将出于某种原因返回 0:

    function choiceLabel($field_id){
        $field = GFFormsModel::get_field( $form, $field_id );
        $choice_text = $field['choices'][$entry[$field_id]]['text'];
        return $choice_text;
    }

    $data = choiceLabel(4);

    file_put_contents($my_file,$data);
    $tmpfile = fopen($my_file, "r");
    $contents = fread($tmpfile, filesize($my_file));

这会产生一个错误 "Warning: fread(): Length parameter must be greater than 0"。为什么我的函数返回 0 值而不是字符串 "First Choice"?感谢您查看此内容。

感谢 David 为我指明了范围方向。

这个有效:

    function choiceLabel($field_id,$form,$entry){
        $field = GFFormsModel::get_field( $form, $field_id );
        $choice_text = $field['choices'][$entry[$field_id]]['text'];
        return $choice_text;
    }

    $data = choiceLabel(4,$form,$entry);