php 5 create global variable within function -
i'm trying create global variable within function isn't passed when try echo outside function.
function check_input($data) { if ( preg_match("/http/i", $data)) {$globals['spam'] = 'yes'; } check_input($data); echo $spam; echo $globals['spam'];
the correct course of action return
value out of function, instead of relying on global variables.
function check_input($data) { //note use of true instead of "yes". //you can more stuff true/false. if ( preg_match("/http/i", $data)) { return true; } else { return false; } } $is_spam = check_input($data); echo $is_spam; //1 or 0, because that's how true , false display in echo.
also see: why global state evil?
Comments
Post a Comment