首页 > 解决方案 > 如何修复从函数返回的 NULL,错误在哪里?

问题描述

我想使用函数返回一个静态字符串,但没有返回

这个函数需要返回“I am” 那么错误在哪里呢?首先我尝试回显没有返回然后打印,print_r 最后也没有返回 var_export 或 var_dump 返回 NULL

// Here is the function
function w_is_the_error() {
    global $where;
    return $where;
}

// I executed here
$the_error = w_is_the_error($where = 'I Am');

// When doing var_export it returns NULL
var_export($the_error);

标签: phpfunctionvariables

解决方案


因为这不是您发送或接受函数参数的方式。

function w_is_the_error($where) {
    return $where;
}

// I executed here
$the_error = w_is_the_error('I Am');

// When doing var_export it returns NULL
var_export($the_error);

global是拐杖。不要使用它,使用参数。


推荐阅读