0

I've created a function but I don't manage to set the boolean value in it.

$msg = '';
$error = false;
function error($content)
{
    if (empty($msg)){
    $msg = $content;
    }
    $error = true;
    return $msg;
}

So if I test

<p>test: <?=error('some content');?></p>
<p>error: <?=$error?></p>
<?php
    if ($error){
        $temp = 'YES';
    }else{
        $temp = 'NO';
    }
     ?>
    <p>temp: <?=$temp?></p>

it will return

test: some content
error: 1
temp: NO

I really don't understand, can someone help me ?

  • You are creating a new variable called `$error` in the function and not accessing the variable in the global scope, same with `$msg`. You should either pass the values into and return them, or pass them by reference. – Nigel Ren Mar 14 '20 at 19:46
  • Thanks for your quick answer! I'm learning php, could you provide me an example? I don't really see how I can do that and I want to understand to avoid doing this kind of mistakes which is pretty basic – Nerofle Lincoln Mar 14 '20 at 19:51

0 Answers0