PHP fails silently when php-code is within html tag
PROBLEM UPDATED, READ BELOW
For some reason my CI fails silently when loading view.
Loading view is simply called from controller$this->load->view('templates/default.php');
Now. There are some functions in the loaded view that are not defined unless a proper helper is loaded as well. No开发者_StackOverflowrmally, php would throw an error, but instead it fails silently here. I have no idea why. The template gets outputted till the line containing the undefined function.
It took me long time to realise where my script is failing.
Here's my setup:
- Windows 7 Ultimate
- Apache 2.2.15
- PHP 5.3.2 with following error reporting settings:
display_errors = On
display_startup_errors = On
error_reporting = E_ALL | E_STRICT
- CodeIgniter 1.7.2
Any ideas why would that be?
UPDATE
After further debugging, it turned out that PHP fails to report any errors when php code is inline with HTML and within the HTML tag. Now this is bizarre.
This returns Fatal Error:
<p><?php echo $bogus(); ?></p>
This doesn't and fails silently:
<p class="<?php echo $bogus(); ?>">paragraph</p>
Why? :O
UPDATE 2
Further investigation showed that if an error_log
in PHP is specified, the errors are in fact reported in that file, but still not in the browser... Again, why?
UPDATE 3
Actually my code should be slightly different. Checked another PHP installation on completely different machine and it confirmed the PHP bug. Reported here: http://bugs.php.net/bug.php?id=52040
Thanks for all the help guys. You would not believe what was the problem!
The browser itself!!! Chrome 6-dev in fact.
It was actually stripping out the invalid html line altogether.
Further testing revealed:
- IE8 displays the error normally in the browser (which is wrong really as it should be only visible in the source)
- Firefox 3.6 shows the error in the source
- Opera 10.53 shows the error in the source
Thanks again for your help.
Look in the /system/application/config/config.php
file and /index.php
file and check to see what level of error reporting CI is instructing PHP to do.
At the top of the index.php file is the following, which should solve your problem:
/*
|---------------------------------------------------------------
| PHP ERROR REPORTING LEVEL
|---------------------------------------------------------------
|
| By default CI runs with error reporting set to ALL. For security
| reasons you are encouraged to change this when your site goes live.
| For more info visit: http://www.php.net/error_reporting
|
*/
error_reporting(E_ALL);
And in the config file (for completeness) is the following:
/*
|--------------------------------------------------------------------------
| Error Logging Threshold
|--------------------------------------------------------------------------
|
| If you have enabled error logging, you can set an error threshold to
| determine what gets logged. Threshold options are:
| You can enable error logging by setting a threshold over zero. The
| threshold determines what gets logged. Threshold options are:
|
| 0 = Disables logging, Error logging TURNED OFF
| 1 = Error Messages (including PHP errors)
| 2 = Debug Messages
| 3 = Informational Messages
| 4 = All Messages
|
| For a live site you'll usually only enable Errors (1) to be logged otherwise
| your log files will fill up very fast.
|
*/
$config['log_threshold'] = 2;
Which I believe is only for logging, not display.
Trying to access an object that's not there will cause a silent (blank screen) error.
The reason is really quite simple. It's because the browser isn't rendering your error, but if you right click -> View Source, you can see the error in your HTML source code.
<p class="<?php echo $bogus(); ?>">paragraph</p>
would output
<p class="Fatal error: Call to undefined function on line ...">paragraph</p>
or something similar. The browser simply thinks that those words are a collection of CSS classes, so you don't see anything on your screen. It's contained within an HTML attribute.
Without having to change the error display policy for the entire application you could add
ini_set('display_errors', 1);
error_reporting(E_ALL); // Or what ever fits
//If one would like to log the errors one could use
ini_set('log_errors', 1);
ini_set('error_log', FILE_TO_LOG_TO);
to the script in question.
Take a look at the error handling documentation, you can get CI to produce its own error reports.
精彩评论