0

I would like to return a static string using function, But nothing returned

This function needs to return "I am" So where is the error ? First i tried echo nothing returned then print,print_r also nothing finally var_export or var_dump returns 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);
  • Your function doesn't take any parameters. – Dharman Jul 26 '19 at 23:50
  • Possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – Dharman Jul 27 '19 at 00:00

1 Answers1

0

Because that's not how you send or accept function arguments.

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 is a crutch. Don't use it, use arguments.

Sammitch
  • 30,782
  • 7
  • 50
  • 77