- Check the domain part of Email Addrss . PHP has a built in function called checkdnsrr() which will take an email address and check if it resolves as an IP address. This is very cool when sending emails for example. Should checkdnsrr() return false while you are trying to send an email with this function, you can return an error informing the user that the domain probably doesn't exist before you do anything else. This is really nice as you didn't really even have to validate the email address in any other way, saving you time, server resources, and just making things really cool. Of course, you should do the usual slog and validate with filters, but this is a good way to actually see of the domain and email address actually exist.
- Test Boolean Results with Switch. We all know that we can use switch to test cases, like this:
<?php switch ($a) { case 'a': echo 'Yay it is A'; break; case 'b': echo 'Yahoo! Its B!'; break; }
But did you know that you can test for Boolean values with a switch?The trick is to use it as follows:<?php switch (TRUE) { case ($a == 'A'): echo 'Yay it is A'; break; case ($a == 'B'): echo 'Yahoo! Its B!'; break; }
The principle might not hit you until you think about it, but once it does it's quite clear that switch can actually make things very simple for you if you use it in this way as well. - Variable Variables. This is my favorite accidental find in programming. What it comes down to is that a variable can have a variable name. Where would you use something like this? Well, image we have a class name that is dependent on the url in some way. For the sake of simplicity I am going to fore-go filtering and validation and simply say
<?php $class_name = $_GET['class_name'];
Now, let's assume that once we have the class name, we need to instantiate the class and the object name also needs to be the same as the class name. Thus we can say$$class_name = new $class_name();
And therefore, if the value of $class_name is 'water', we will have an object named $water. See the power here? - MySQLI. If you haven't used mysqli yet, you really need to start thinking about it. Mysqli takes the basic mysql functions that you usually end up rewriting 500 times during a project, or if you are more resourceful, you are creating functions for, and lays them out on a table for you - ready to use. Mysqli makes database transactions simple, because the hard work is done. Start using it, you won't be sorry.
- Switch Off Error Reporting. Seriously. When you go into production on your website, kill the error reporting. You do not want to put all your warnings and errors out there for all mankind to see. Simply set error_reporting(0); and all is well with the world.
- You don't always need the closing PHP Tag. It's true. In fact, Zend Framework forbids it. PHP does not require you to close a PHP tag that is at the end of a file, or is in a file that contains only PHP. In other words, you don't need the '?>' unless you need to use non-PHP code after some PHP. Get used to leaving it out, leaving it in can cause problems in certain circumstances, and it has to do with leaking whitespace after the closing tag, and causes chaos when you output XML. Go figure.
- Extract is your friend. Ever been in the situation where you need to say something like:
<?php $name = $array['name']; $surname = $array['surname']; $message = $array['message'];
Then you may want to recall that you can use extract() to do the same.Put simply, extract will remove the work behind this.In this case, saying:<?php extract($array);
Will automatically make $name = $array['name'];So, you can say "hello ".$name." ".$surname." Without do all of the declarations.Of course, you always need to be mindful of validation and filtering, but there is a right way and a wrong way to do anything with PHP. - Comment Comment Comment. If you don't comment your code, then you should, because no one is going to take you seriously as a developer until you do. It's that simple.
- Validate. Validation is not only important, it should be mandatory. You need to validate all inputs, and outputs, and make sure that the code you are using is the best possible code it can be. There is nothing else more important.
- Code Every Day, Build Real Things. David Hanson created Ruby on Rails after extracting the work from Basecamp, a project he insists he was only able to do because he developed in anger. What this means is that he developed something real, something tangible that people would use. By thinking like this, he made it the best way he knew how. You need to do the same. Developing an application that doesn't do anything and is never going to be used by anyone is not going to make you great. Building something that you know has to work, has to WOW people, and make people want to pay to use it or buy it WILL make you great. At very least it will make you learn something.