Php Error Messages Formatting
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How do you format php error messages? they don't respect css up vote 1 down vote favorite Whenever PHP outputs an error message it disregards css and a beautifully designed page by outputting the message at the top of the page removing anything that stands in its way. for example some code} else { echo "error, please do something!"; How do I get it to (or ask it nicely) to output the text inside a div that already exists inside my css so that it will obey the formatting and alignment rules that comes with that div. php css share|improve this question edited Dec 24 '12 at 0:42 hakre 134k26216390 asked Mar 3 '11 at 23:30 Stu 78021224 1 In order to be formatted it has to be inside your formatting. It's difficult to tell where you should move this error without more detailed code sample. Please provide a piece of your code with html –criticus Mar 3 '11 at 23:34 Can you provide more information on what's going on? For instance, in what context is your above code sample running? I think your echo is the problem (see my answer for more detail on that theory), but it's hard to tell without more information. –Jared Farrish Mar 4 '11 at 0:36 Sorry for the confusion, I was running a nested if statement to validate a form, and then outputting an error through echo if invalid. I also had other pages outputting html. The problem was echo as you kindly pointed out and explained as to why this was happening. Your answer and Mario's was very insightful and did alot more than just answer my question. Thank you both. It behaves now! and I understand php better. –Stu Mar 4 '11 at 21:35 add a comment| 5 Answers 5 active oldest votes up vote 5 down vote accepted EDIT Actually, I just realized the "error" you're talking about involves an echo/print out. Here's the problem. You're printing (echoing) the string error DIRECTLY TO the output buffer (which sends the HTML to the browser when you're finished running all your code). echo() and print() sends what yo
Implemented in PHP 5.4 Related RFCs: https://wiki.php.net/rfc/newinis Abstract This RFC discusses the restoring of pre-PHP 5.3 functionality regarding the formatting of error messages. Prior to PHP 5.3, PHP had two bundled example .ini files: php.ini-dist and php.ini-recommended. In both cases the setting html_errors was not changed from PHP’s default (on). docref_root was left to “” (empty string). In PHP 5.3 two new .ini files were introduced: php.ini-development and php.ini-production. In the latter, html_errors was changed from http://stackoverflow.com/questions/5187948/how-do-you-format-php-error-messages-they-dont-respect-css PHP’s internal default to off. This RFC discussed the reversal of that change, so that html_errors is “on” by default again. It also suggests to only show documentation links if the docref_root setting is explicitly set to an HTTP path. Rationale The html_errors setting is used in PHP to add HTML https://wiki.php.net/rfc/error-formatting-for-developers formatting around error messages in a web environment. It adds some mark-up and a link to the documentation as well. Some PHP extensions, such as Xdebug, enhance this mark-up to provide an outstanding development aid. Most Linux distributions (if not all), force the use of php.ini-production, bundle that only variant or have php.ini-production as default. This means that for many users of such distributions the enhanced mark-up is no longer available by default, resulting in much confusion. Those distributions are just as much used for development as for production of course. A few examples of confused people: http://cloudfysh.wordpress.com/2010/06/11/php-xdebug-not-formatting-var_dump/ http://stackoverflow.com/questions/4534312/xdebug-var-dump-function-colors http://www.paoloiannelli.com/2011/04/15/solution-for-xdebug-not-overloading-var_dump/ http://stackoverflow.com/questions/2108576/unreadable-var-dump-output-on-snow-leopard http://stackoverflow.com/questions/4678389/does-xdebug-beautify-var-dump There is also a PHP bug report at https://bugs.php.net/bug.php?id=54537 It is also a FAQ for Xdebug: http://xdebug.org/docs/faq#format In a development environment you would want this setting “on” to aid developing. In a production environment you rather have errors not showing up, but instead logged to a file. For that, you
2015 Over the last couple years, I’ve started putting my Exception messages inside static methods on custom exception classes. This is hardly a new trick, Doctrine’s been http://rosstuck.com/formatting-exception-messages doing it for the better part of a decade. Still, many folks are surprised by it, so this article explains the how and why. How does it work? Let’s say you’re writing a large CSV import and you stumble across an invalid row, perhaps it’s missing a column. Your code might look like this: if (!$row->hasColumns($expectedColumns)) { throw new Exception("Row is missing php error one or more columns"); } This works in terms of stopping the program but it’s not very flexible for the developer. We can improve this is creating a custom exception class. class InvalidRowException extends \Exception { } Now we throw our custom Exception instead: if (!$row->hasColumns($expectedColumns)) { throw new InvalidRowException("Row is missing one or more columns"); } This might look like boilerplate php error messages but it allows higher level code to recognize which error was raised and handle it accordingly. For example, we might stop the entire program on a NoDatabaseConnectionException but only log an InvalidRowException before continuing. Still, the error message isn’t very helpful from a debugging perspective. Which row failed? It would be better if we always included the row number in our error message. if (!$row->hasColumns($expectedColumns)) { throw new InvalidRowException( "Row #" . $row->getIndex() . " is missing one or more columns" ); } That’s better in the log but now the formatting on this one little message is getting a bit noisy and distracting. There’s no upper bound on this complexity: as the log message gets complex, the code will get uglier. Not to mention, there are multiple reasons we might throw an InvalidRowException but we’d need to format them all to include the row number. Booorrrriiing. Moving the Formatting We can remove the noise by pushing the formatting into the custom Exception class. The best way to do this is with a static factory: class InvalidRowException extends \Exception { public static function incorrec