0

I have a

function displayErr(){
    $err[] = "You have an error";
}

later in my code

if($bla==false) { displayErr(); }
if(!empty($err)) { echo print_r($err); }

I don't get anything, can't I call a function that adds on $err and then check it later?

2
  • 1
    I'd recommend refactoring into an error logging class. Commented Jan 23, 2011 at 17:27
  • I suggest to rename your function. displayErr is not displaying the errors at all. Commented Jan 23, 2011 at 17:33

5 Answers 5

2

The variable $err is is only visible within the function displayErr(). What you want is to access a global variable within the function. To do this, you need to import it using the global keyword, as the following example shows:

function displayErr(){
    global $err;
    $err[] = "You have an error";
}

For more information regarding variable scope, see this link: http://php.net/manual/en/language.variables.scope.php

Sign up to request clarification or add additional context in comments.

Comments

1

$err is only visible in the scope of your displayErr() function. You need to pass it, or its values, around. A couple of options:

  1. Return the error message and append to $err within the calling scope

    function displayErr() {
        return "You have an error";
    }
    
    $err = array();
    if($bla==false) { $err[] = displayErr(); }
    if(!empty($err)) { print_r($err); }
    
  2. Use global

    function displayErr() {
        global $err;
        $err[] = "You have an error";
    }
    
    $err = array();
    if($bla==false) { displayErr(); }
    if(!empty($err)) { print_r($err); }
    

Also, print_r() outputs stuff by itself, you don't need to echo print_r().

2 Comments

thank you, is this the same with if i should check for $_SESSION inside a function?
@Karem: No, $_SESSION is a superglobal which means you don't need to do global $_SESSION;.
0

You can use the global keyword to store your variable as global:

function displayErr()
{ 
global $err;
$err = "You have an error"; 
}

[...]

global $err;
if($bla==false) { displayErr(); } if(!empty($err)) { echo print_r($err); }

Comments

0

$err is only available in your displayErr function.

You need to add a global statement. Like this: displayErr() {global $err; ... }

This is called variable scoping, and you can read more about it here.

Comments

0

A simple error handling class example:

class ErrorHandler()
{
    private $errors = array();

    public function add_error($error = 'You have an error')
    {
        $this->errors[] = $error;
    }

    public function get_errors()
    {
        return $this->errors;
    }

    public function print_errors()
    {
        print_r($this->errors);
    }
}

$errorhandler = new ErrorHandler;
if($bla==false) $errorhandler->add_error('Variable bla is not set!');
if($bla=='unknown error') $errorhandler->add_error();

$errors = $errorhandler->get_errors();
if($errors) print_r($errors); // or
if($errors) $errorhandler->print_errors();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.