Things I did not know about php

Interesting list of things that you probably did not know about php (I did not even know half) taken from this website and this one.

ip2long () and long2ip ()

These two functions ip2long () and long2ip () serve to store the IP addresses as integers rather than as strings in the database, this will reduce the storage resources obtained from the 15 bytes of a chasr (15) to the 4 bytes of an integer

Validation of email addresses

The partial validation of IP addresses is easy thanks to the checkdnsrr () function.
This function assures us that the domain returned exists, for example very useful in case of how it is:

‘’ Valid
‘’ Not Valid

How not to do unnecessary work

If you do not want to be working on a certain function for a while, then realize that you have reinvented the wheel … check PEAR before writing the following lines of code.

PEAR – A framework and system of distribution of utilities Php. PEAR offers us php complements grouped in “Packages”.

Format your code

A function that formats a last string, specially designed for source code, highlight_file ().

Very useful in case we want to share questions about our code with a friend either by messenger, IRC or any similar service.

Do not show the errors

Avoid displaying message errors with the error_reporting (0) function. Useful in case of having shared hosting you must include it in the first line of each of your scripts, this will prevent information about PHP connections and MySQL users from being displayed.


PHP uses a mix between interpretation and compilation to try to offer programmers the best between performance and flexibility.

PHP compiles a series of instructions (called opcodes) for your code whenever they are accessed. These instructions are then executed one by one until the script ends. This is different from the conventional way of compiling languages ​​such as C ++ where the code is compiled into executable code that is then executed. Php is recompiled every time a script is requested.

The tag?> Is optional

Each one of the php files that you have programmed probably have this format:

But did you know that the label?> Is optional? Delete it and everything will continue to work, I’m not sure it works with all versions but it works in PHP 5.1.
There is no good argument to justify its removal from our code, but it is quite curious.

Masks other languages within PHP

Java, Perl, Python and .NET are some examples of languages that you can use within PHP code. Support for other languages can be found in PECL.

Make objects look like arrays

You can use the standard SPL php library to convert objects to an array in the following way:

$object = new MyObject();
$object[‘name’] = ‘Hello World’;

object = new MyObject();

foreach ($object as $k=>$v) {
echo “$k = $v\n”;

Require is faster than require_once

In versions prior to PHO 5.2 using require instead of require_once you can get a speed increase especially in heavy applications. The reason is that require_once makes an extra call to the system to validate that you have not included this file previously. Require includes a file without any verification.

Handling your mistakes is easy

The summary of errors programmed by PHP certainly is not the best. The famous “headers already sent” or “call to undefined function” that appears in bold and with a line number that may or may not be correct.

But you can program your own error handler.s:

function mis_errores ($numero, $mensaje, $archivo, $linea) {
// Añade el CSS para tu codigo
echo ‘

El siguiente error ocurrio en linea ‘ . $linea . ‘ del archivo ‘ . $archivo . ‘: ‘ . $mensaje . ‘
echo ‘

Las actuales variables son:

‘ . print_r($GLOBALS, 1) . ‘


Then you will use your own default handler and not the PHP handler to PHP in the set_error_handler () function.

Then most of the errors will be handled by your function (but there are some exceptions). With the previous example the errors are printed with html format.

I should even consider the possibility of sending these errors via mail with the mailto function and in this way you will have real time control of your site, or even handle errors in the connection to a database.

try {
if (!@mysql_connect(‘localhost’, ‘username’, ‘password’))
throw new Exception (mysql_error());
} catch (Exception $e) {
echo ‘Imposible conectar a la base de datos: ‘ . $e->getMessage();