简体   繁体   中英

PHP Undefined variable in Apache error_log

I'm getting a series of:

"Undefined variable: loginError in /Library/WebServer/Documents/clients . . ."

entries in my Apache error_log which I would like to prevent. I have a simple login.php page which, if there's an error logging in sets the $loginError variable as such:

$loginError = '<p class="text-error">Login Error: '. $layouts->getMessage(). ' (' . $layouts->code . ')</p>';

If there's no error logging in it does this:

$loginError = '';

I then output any errors as such:

if ($loginError !== '') { //line 112
echo $loginError; /line 113
} 

I'm getting the entries for the line 112 and 113 noted in my comments above. Anyone tell me how I can prevent the entries appearing? I'm using PHP Version 5.3.6.

thanks

Its saying you should check it is set before using:

One way is with isset()

if (isset($loginError) && $loginError !== '') {
  echo $loginError;
} 

But in your particular case you may as well use !empty()

if (!empty($loginError)) {
  echo $loginError;
} 

Hard to say without seeing the rest of your code. Trace through your logic to make sure that every possible branch initializes loginError at some point in its execution. Even better, set it to a default value before you go through the logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM