0

我正在尝试从函数返回一个变量并将其打印出来。它现在显示意外的 T_STRING.... 有人可以帮忙吗?

function reg_word($i){
        $reg_word = "/[^A-Za-z0-9 ]/";
        $i = preg_replace($reg_word, '', $i);
    }

$suggestion = function reg_word($_POST['suggestions']);

    print_r($suggestion);
4

3 回答 3

0
function reg_word($i){
    $reg_word = "/[^A-Za-z0-9 ]/";
    return preg_replace($reg_word, '', $i);
}

$suggestion = reg_word($_POST['suggestions']);

print_r($suggestion);

你以前有function关键字reg_word($_POST['suggestions']);- 你不需要它。您需要使用return关键字从函数返回某些内容。

于 2012-07-07T07:46:38.517 回答
0

您有一个function关键字错误,请尝试:

function reg_word($i){
        $reg_word = "/[^A-Za-z0-9 ]/";
        $i = preg_replace($reg_word, '', $i);
    }

$suggestion = reg_word($_POST['suggestions']);

    print_r($suggestion);
于 2012-07-07T07:46:46.157 回答
0

错误function的关键字,您不会在函数中返回任何值。

function reg_word($i){
        $reg_word = "/[^A-Za-z0-9 ]/";
        return preg_replace($reg_word, '', $i); // return added
    }

$suggestion = reg_word($_POST['suggestions']); // here function was

    print_r($suggestion);
于 2012-07-07T07:48:01.280 回答