Sponsored by Bing
When Should I Book My Flight Home for Christmas? view!
bing.com - Bing predicts if your fare is likely to fall by $50 or more within 7 days. Shockingly accurate.
69 Comments
- catoutfit, on 10/12/2007, -1/+13"All of these things are written in the PHP Manual."
yeah because most people do read the entire 3,000+ functions of the PHP manual! People learn what the 'need to learn' and pick up the rest along the way.
This isn't a bad article at all, though i think most people would know about SWITCH. - RBasil, on 10/12/2007, -1/+12"Handle Errors Your Way"
Love this one and plan to use it a.s.a.p. Thanks for the link! - merreborn, on 10/12/2007, -2/+11"All of these things are written in the PHP Manual."
Yeah, but really, how obvious is the name "glob()"? I'd never have found it if it wasn't this article.
"I don't think it is a good idea to "handle errors your way" because most info is well sent through normal PHP."
It doesn't include backtraces, and it's echoed to the page. Both of these are bad things. 1) Gotta have backtraces if you're gonna debug 2) You don't really need your users to see raw PHP errors 3) You've gotta log errors! Then you can reconstruct the series of events that lead to whatever problem it is you're debugging.
I promise you that every every big PHP-driven site on the net uses a custom error handler. Can you imagine how cheesy it would look if Digg or Flikr kicked out a PHP error? It's not that they don't have errors now and then -- they just use custom error handlers. - endyminion, on 10/12/2007, -0/+6catoutfit:
You can't use Tables for *tabular* data?
Your company needs to get their heads out of their ass and realize that tables are *made* for displaying information that is *tabular* in nature. Why the hell aren't you allowed to use them? - Exiler86, on 10/12/2007, -1/+7All pretty basic stuff - but a good one for beginners.
- i440, on 10/12/2007, -1/+7PHP is easy to learn. Trust me, if I can do it, most anyone can. Just Google "PHP tutorial" and you'll be on your much sooner then you think.
- NetElemental, on 10/12/2007, -0/+6Same here. I've been looking for something like this so I can track errors and have users report errors without exposing filenames/path, and other sensitive information.
- merreborn, on 10/12/2007, -0/+5"This isn't a bad article at all, though i think most people would know about SWITCH."
PHP's switch behavior is VERY non-standard. I'm pretty sure it's the only language with C-esque syntax that accepts ranges like that. C, C++, and Java only allow constants. - david76, on 10/12/2007, -0/+5That's not error handling, that's error reporting.
- smartssa, on 10/12/2007, -0/+5Well, this article is kind of weak.
Handling errors can be very complex. You can continue (or at least gracefully fail) from 'fatal' errors if you do things properly.
PHP 5 also does exceptions, which I think is better than just manipulating error reporting.
http://php.net/exceptions - it gives you much more control - smartssa, on 10/12/2007, -0/+4Cute, yes. But I give them credit for actually trying to diversify :)
- JasonMaloney101, on 10/12/2007, -0/+4merreborn wrote:
I promise you that every every big PHP-driven site on the net uses a custom error handler. Can you imagine how cheesy it would look if Digg or Flikr kicked out a PHP error? It's not that they don't have errors now and then -- they just use custom error handlers.
Amen to that. Take note from the millions of poorly-coded PHP sites that throw MySQL and other errors in your face. - merreborn, on 10/12/2007, -0/+4Great advice; however, this would work a lot better if PHP's library functions threw exceptions instead of errors.
File not found? That's an error. Bad regex in preg_match? That's an error.
So, if you use PHP library functions, you still have to handle errors anyway -- but you should definately use exceptions in your own code. - psyon, on 10/12/2007, -0/+3#5 talks about HTML Tidy being incorporated into PHP 5, and being available in PHP 4 as a PECL extension. So tidying your HTML output is something you could do with PHP.
- pplante, on 10/12/2007, -1/+4Does anyone else love that a "must-have" PHP article was linked from a site sponsored by Microsoft?
Cute :) - Grimdotdotdot, on 10/12/2007, -2/+5Or : "0 things you didn't know you could do with PHP".
- dbr_onix, on 10/12/2007, -0/+3Erm, maybe this ISN'T aimed at experienced PHP coders?
- Ben - doihaveto, on 10/12/2007, -0/+2@merreborn: seconded. It is non-standard and confusing, especially to old C programmers. :)
I wish they'd done what Lisp/Scheme did with the "cond" statement, and created a separate keyword for a switch with boolean tests. A little change, but greatly increases readability. :) - shayne_sweeney, on 10/12/2007, -0/+2Good stuff, already used/knew all of it but I am glad he published some methods that might help others.
- cozinator, on 10/12/2007, -0/+2I've always found a recursive array2table() function infinitely more useful than print_r for viewing and debugging array data in HTML:
http://aidan.dotgeek.org/repos/?file=function.array2table.php - JasonMaloney101, on 10/12/2007, -4/+6Hopefully this time it's correct.... there should be a Preview button. Mod the others down, please.
Also note that when registering your own error handler it is up to you to decide whether the triggered error should be reported. When using the @ operator, error_reporting() will return 0.
<?php
/*****************************************************
* functions.errors.php
* v 0.9
* (c) 2006 Jason Maloney
*****************************************************/
define( 'E_SQL_ERROR', 16 );
// bool bit_isset( int mask, int bit )
// Returns TRUE if the given bit is on in the given mask
function bit_isset( $mask, $bit )
{
return (bool) (( $mask & $bit ));
}
// bool is_reported( int errno )
// Returns TRUE if the given error number will be reported
function is_reported( $errno )
{
$level = error_reporting();
// PHP 5 fix -- E_STRICT = 2048
if( $level == 2048 )
{
return true;
}
return bit_isset( $level, $errno );
}
// void report_error( int errno, string errstr [, string errfile [, int errline [, array errcontext]]] )
function report_error( $errno, $errstr, $errfile = false, $errline = false, $errcontext = false )
{
if( !is_reported( $errno ) )
{
return;
}
echo "<br />n";
switch( $errno )
{
case E_USER_ERROR:
echo "<b>Error</b>: $errstr in <b>$errfile</b> on line <b>$errline</b><br />";
exit;
break;
case E_WARNING:
case E_USER_WARNING:
echo "<b>Warning</b>: $errstr in <b>$errfile</b> on line <b>$errline</b><br />";
break;
case E_NOTICE:
case E_USER_NOTICE:
echo "<b>Notice</b>: $errstr in <b>$errfile</b> on line <b>$errline</b><br />";
break;
case E_SQL_ERROR:
@list( $sql_errno, $sql_error ) = $errcontext['this']->errinfo;
echo "<b>SQL Error</b>: $errstr in <b>$errfile</b> on line <b>$errline</b>"
. "<br /<br />SQL said: <b>$sql_errno</b> $sql_error";
if( isset( $errcontext['query'] ) )
{
echo '<br /><br />Query: ' . $errcontext['query'];
}
break;
}
}
?> - MasterDwarf, on 10/12/2007, -4/+6For somebody like me the title should read, "Five MORE things you didn't know you could do with PHP"
i wish i could code. - dH2K, on 10/12/2007, -0/+2I think there must be billions of things what you didn't know you could do with ANY programming language - every languages has their own structure but the possibilities always depends on the enviroment where the problem exists and since the environment (problem horizont / software / hardware / i/o layer) could be unlimited... there is infinite possibilities... what you didn't know.
- Ikioi, on 10/12/2007, -1/+3It's "5 Things Microsoft Didn't Know You Could Do With PHP", hehe.
But, if you don't know these things, you really need to take the time to go through http://www.php.net and read up on the documentation. The great thing about the documentation is the comment system where developers post their own code examples. No matter how experienced you are at PHP, you will always find gems of code in the comments. Usually, they will even show exactly what you were looking to use the function for, like stepping through matrixes to input into a database, doing string matches and replacements, etc.
PHP has come a long way, and I advise any serious PHP programmer to Google for the IBM articles on OOP (object oriented programming). The PHP5 has the ability do some of the easiest and most powerful OOP you've ever seen in backend scripting, coding its own functions on the fly. (The tutorial on making a universal MySQL object is simply brilliant). I would put PHP5's OOP at the top of any "must know" lists. - shayne_sweeney, on 10/12/2007, -0/+2http://us3.php.net/manual/en/ref.errorfunc.php
There are many ways to log errors and keep them from a user. Even warnings and parse errors can be hidden. - tizz66, on 10/12/2007, -0/+2Get "Beginning PHP" by Wrox, from Amazon. You'll be coding before you know it :)
- malcolmboston, on 10/12/2007, -0/+2Five Things You Didn't Know You Could Do with PHP.....Im still waiting
seriously weak article, any half-decent developer knows how to do these
There is too many 'newbie' PHP articles being written FOR digg and purely for the traffic it brings - pkkid, on 10/12/2007, -0/+2On the contrary to what lkioi said, if you know Java or C++. You will find PHP's OOP is not yet that powerful I am afraid. I do agree that it provides a much better way to program over normal scripting, but thats the whole purpose of objects. :)
- SledgY, on 10/12/2007, -0/+1"All of these things are written in the PHP Manual."
While true because php has a very poor and inconsistent naming scheme (ie fucntions named myFunction vs my_function seemingly at random) it can be difficult to find the function you need.
Most of the tips listed are more aimed at new developers, they also miss some of the other great new features of php 5 namely redesigned class's. The last site php I built was entirely OO. - bfdhud, on 10/12/2007, -0/+1I got an enormous full article width flash pop-up with no xclose button. Needless to say I didnt get to finish the article.
- GeneralFailure, on 10/12/2007, -0/+1More like 5 things that newbies didn't know you could do with PHP! I got my hopes up, too.
- PaiTrakt, on 10/12/2007, -0/+1The only thing I didn't know about was the glob()-function, but that indeed seems like a nifty function. Guess I've learnt something useful today as well :)
- PrisonerOfPain, on 10/12/2007, -0/+1I usually do something like
function print_d($content){
if(defined('DEBUG')){
echo "[pre]"; print_r($content); echo "[/pre]";
}
}
Does the same and I can keep it in the source for later use :-)
*Change [ and ] to < and > - Grimdotdotdot, on 10/12/2007, -0/+1Maybe so, but if that's the case, it could have been mentioned in the Title or Summary somewhere.
- MasterDwarf, on 10/12/2007, -0/+1To all: I appreciate your enthusiasm and will have to lock myself down and get hot. There is an online app that I want to produce but my lack of knowledge can be quite agonizing.
- JoyrexJ9, on 10/12/2007, -1/+2One out of five ain't bad, (I'd never used the glob function before) but most of this isn't new to me.
- av4rice, on 10/12/2007, -3/+45 Things That Everyone Knows They Can Do With PHP!
- catoutfit, on 10/12/2007, -3/+4"I promise you that every every big PHP-driven site..."
The pirate bay doesn't ;) - etx313, on 10/12/2007, -2/+3Dugg for PHP, the best scripting language imo.
- inactive, on 10/12/2007, -0/+1Nice. The standard error report messages served up are not XHTML compliant. Now they can be, and Mozilla users wont see XML validation errors instead of content. Dugg.
- Zhay, on 10/12/2007, -0/+1Aww, I feel sorry for you. Coding is my life. Almost. But honestly, if you get a book and DO the exercises, it will help.
- PrisonerOfPain, on 10/12/2007, -0/+1Then you haven't used Linux either. (And if you did, do a man glob)
- KageKonjou, on 10/12/2007, -0/+1Dugg for PHP, but old news. Any good developer should know these in any case.
- Airkat, on 10/12/2007, -0/+0This is a pretty good article. That glob thing slipped by me!
- inactive, on 10/12/2007, -0/+0Thanks for that
learnt 3 new things out of 5
still a bit iffy about the HTML Tidy point though - AramilPHP, on 10/12/2007, -0/+0This topic is, to put it simply, a wase of time, anyon with a decent knowledge of the internet and a link to the php.net site should already know all of these.
- cazabam, on 10/12/2007, -0/+0The functions you define are called when the error occurs. If you use it for handling, reporting or both is up to you.
- phpfreak, on 10/12/2007, -0/+0Great article, especially for PHP newbies. :-D
- chovy, on 10/12/2007, -0/+0i like the case switching with conditional, hadn't thought of doing that before.
- Jaik666, on 10/12/2007, -0/+0Wow, number 1 and number 4 I didn't know about. Number 1 is truly useful, I've often wanted a way for switch to be able to handle value ranges. Awesome stuff.
-
Show 51 - 69 of 69 discussions



What is Digg?