Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I'm trying to create a global variable within a function but it isn't passed when I try to echo outside the function.

function check_input($data)
{
        if ( preg_match("/http/i", $data)) {$GLOBALS['spam'] = 'yes'; 
}

check_input($data);
echo $spam;
echo $GLOBALS['spam'];
share|improve this question

closed as off-topic by PeeHaa, andrewsi, khr055, ryan1234, Cfreak Jul 22 '13 at 19:47

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist" – PeeHaa, andrewsi, khr055, ryan1234, Cfreak
If this question can be reworded to fit the rules in the help center, please edit the question.

4  
1. that's not even valid PHP 2. No you don't want to create a global – PeeHaa Jul 21 '13 at 18:47
    
Wouldn't it be $_GLOBALS or $_GLOBAL ? Maybe not, but it sure seems like it. – pattyd Jul 21 '13 at 18:48
    
Plus, you haven't set $data – pattyd Jul 21 '13 at 18:49
    
shouldn't it be global($spam); and then just $spam in the code.... but I can't think of any reason someone would want to do this. just pass $spam in by reference. – Orangepill Jul 21 '13 at 18:51

3 Answers 3

The correct course of action would be to return the value out of the function, instead of relying on global variables.

function check_input($data)
{
    //Note the use of true instead of "yes". 
    //You can do more stuff with 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 and false display in echo.

Also see: Why is global state so evil?

share|improve this answer

The problem with your code is that you have syntax errors:

function check_input($data)
{
        if ( preg_match("/http/i", $data)) { $GLOBALS['spam'] = 'yes'; } //Note the } I've added.
}

check_input($data);
echo $spam;
echo $GLOBALS['spam'];

Should do fine.

share|improve this answer

You can use the global keyword for this

function check_input($data)
{
    global $spam;
    if ( preg_match("/http/i", $data)) $spam = 'yes';
}
$data = "http://www.example.com";
check_input($data);
echo $spam;
echo $GLOBALS['spam'];

but the question is WHY would you want to do this, a much better approach would be to pass $spam is to the function by reference or return it as a return variable.

function check_input($data, &$spam)
{
    if ( preg_match("/http/i", $data)) $spam = 'yes';
}
$data = "http://www.example.com";
$spam = "no";
check_input($data, $spam);
echo $spam;
echo $GLOBALS['spam'];

or

function check_input($data)
{

    return ( preg_match("/http/i", $data))?'yes':'no';
}
$data = "http://www.example.com";
$spam = check_input($data);
echo $spam;
echo $GLOBALS['spam'];
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.