Welcome Guest, Not a member yet? Register   Sign In
Stopping the "undefined" variable warnings?
#1

[eluser]lowboy[/eluser]
One of the things I like about php is that it handles undefined variables just fine, but CodeIgniter sees fit to warn me about them anyway.

Is there a way to stop it from doing so?

And on a related note, how do I get php errors to poke through? Anytime there's a parse or runtime error, all I get is a blank page from CI's output.
#2

[eluser]Ebot Ndip-Agbor[/eluser]
Go to the index.php file inside codeigniter and change this
Code:
error_reporting(E_ALL);
to
Code:
error_reporting(0);
#3

[eluser]lowboy[/eluser]
Excellent, thanks!
#4

[eluser]louis w[/eluser]
You should try to fix the error warnings rather then mask them. If there is a variable which could possibly not be there always check if (!empty($var)) before you use it.
#5

[eluser]wiredesignz[/eluser]
Code:
error_reporting(E_ALL ^E_NOTICE);
#6

[eluser]TheFuzzy0ne[/eluser]
As far as I am aware, CI doesn't just "see fit to warn you" about random errors. Instead it handles PHP errors generated by the PHP parser. So if you're getting the error/warning in CI, you would also be getting it without.
#7

[eluser]lowboy[/eluser]
[quote author="louis w" date="1209580012"]You should try to fix the error warnings rather then mask them. If there is a variable which could possibly not be there always check if (!empty($var)) before you use it.[/quote]

There are times when you simply don't need to initialize/check your variables - just adds extra code.

For example:

Code:
<title>Site name<?=$section_title?></title>

If $section_title doesn't exist, it will be initialized as an empty string and all is well.
#8

[eluser]lowboy[/eluser]
[quote author="TheFuzzy0ne" date="1209580619"]As far as I am aware, CI doesn't just "see fit to warn you" about random errors. Instead it handles PHP errors generated by the PHP parser. So if you're getting the error/warning in CI, you would also be getting it without.[/quote]

I just expected CI to adhere to the levels set in my php.ini - which is E_ALL & ~E_NOTICE. Other pages/scripts run from my install of PHP don't say anything when I use a variable that's uninitialized.
#9

[eluser]TheFuzzy0ne[/eluser]
Am I missing something?

Code:
<?php
error_reporting(E_ALL);

$section_title;

echo $section_title;
?>

Generates: Notice: Undefined variable: section_title in /home/daz/Desktop/test.php on line 6

Code:
<?php
error_reporting(E_ALL);

$section_title = '';

echo $section_title;
?>

Generates: Nothing, because the variable is initialised.

I find everything stays happier if you give it a default value. If you want it to return an empty string, then initialise it with one.
#10

[eluser]TheFuzzy0ne[/eluser]
[quote author="lowboy" date="1209587579"]I just expected CI to adhere to the levels set in my php.ini - which is E_ALL & ~E_NOTICE. Other pages/scripts run from my install of PHP don't say anything when I use a variable that's uninitialized.[/quote]

Your php.ini file contains only defaults. On shared hosts, it's impossible to set the defaults as everyone is likely to want, therefore, we set our own. If we don't, then the defaults are used.

Anyhow, your problem should be solved now, as you've seen where you configure it for the site. Smile




Theme © iAndrew 2016 - Forum software by © MyBB