10 Awesome Things Most People Don't Know About PHP

By 

IIH Global

Most People Don't Know About PHP

The magic tricks, We all are aware of the way to make dates look cool and numbers look awesome. But there are a few things which most people don't think about, and I'm one of them. As I started working on this article, I did a little R & D and found a few things which will make me change the way why I use PHP. Let's dive right in for further details!

1) Validating Partially the Email Addresses before doing any hard work.

PHP has an inbuilt function called checkdnsrr() which will take an email address and check if it resolves as an IP address. This is very cool for example when we are sending emails. checkdnsrr() should return false while you are trying to send an email with this function, and you can return an error notifying the user that domain probably doesn't exist before you do anything else. This is great that you didn't really even have to validate the email address in any other way, saving you server resources, time, and just making things really better. 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.

2) Test Boolean Results with Switch.
We all know that we can use the 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 you can also 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 strike your mind until you think about it, but once it does it's very clear that switch can actually make things very simple for you if you use it in this way as well.

3) Variable Variables.
This is my favorite accidental find in programming. What comes down to is that a variable can have a variable name. Where would you use something like this? Well, the image we have a class name that is dependent on the URL in some way. For the sake of simplicity, I am going to forego filtering and validation and simply say

<?php

$class_name = $_GET['class_name'];

Now, let's suppose 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 hence, if the value of $class_name is 'water', we will have an object named $water. See the difference here? Many developers consider it a hack. But with the careful validation and right filtering, in other words, if you properly code it, it can work really well. You can apply it to function names, arrays, variables or objects, and PHP website was built to be able to do this.

4) MySQLI.
If you haven't used mysqli yet, you really need to start thinking about it. During a project, Mysqli takes the basic mysql functions that you usually end up rewriting 500 times, or if you are more inventive, you are creating functions for and lays them out on a table for you - ready to use. Mysqli makes database transactions easy because of the hard work done. Start using it, you won't be sorry.

5) Switch Off Error Reporting.
Seriously. When you go to production on your website, killing the error reporting. You don’t want to put all your errors and warnings out there for all mankind to see. Just simply set error_reporting(0), and all is well with the world.

6) 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 the only PHP development. In other words, you don't need the '?>' until there is a need to use the non-PHP code after some PHP. Accustom 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.

7) The 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 if you want to recall that you can use extract() to do the exact same thing.

Put simply, the 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 have to be mindful for validation and filtering, but there are the right way and a wrong way of doing anything with PHP website.

8) Comment Comment Comment.
You should comment your code if you don’t then no one is going to take you seriously as a developer until you do. It's that very simple.

9) Validate.
Validation is not only important, but it should also be mandatory. You need to validate all the inputs, and outputs, and hence make sure that the code you are using is the best possible code it can be. There is nothing else more important.

10) Code Every Day, Build Real Things.
Developing an application which, doesn't do anything and is never going to be used by anyone, is not going to make you great. Building something which you know has to work, has to influence or WOW people, and make people want to pay to use it or buy it that WILL make you great. At the very least it will make you learn something.

Discover Your Ideas With Us

Take the lead with integrated innovation in your company using high-quality software. Contact us now to get started with your project.

Intelligent IT Hub Ltd. is Registered in UK under Companies House with Company Number FC033871 & Establishment Number BR018959.
 Intelligent IT Hub Pvt. Ltd. is Registered in India under Registrar of Companies with CIN Number U72900GJ2013PTC076759.
4.9 / 5.0 by 160+ customers for 525+ Web and Mobile App development projects.
arrow-right-circle linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram