PHP validate email addresses

To rapidly, and decently, validate email addresses, we can use something like this:

$email = "";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) { 
    echo "This email is valid"; 

The filter_var() returns the filtered data or FALSE if it fails

Read about it, and more filters, here:

function filter_var()

PHP error reporting – turning on or off

So you wrote this awesome php script, that does extreme stuff, but when you run it…it shows nothing. Empty browser. So whats going on?

Well, for one, your php error reporting might be turned off by default. So how to turn it on?

Read and learn. And forget tomorrow, like me.

Den ganzen Beitrag lesen…

Preventing SQL injection in PHP/MySQL with PDO

This is a bit old but i wanted to add it here so it stays on record.

SQL injection is the action to inject SQL code in web forms to perform a site attack and disrupt its services. For a better definition, go read wikipedia here (it even has some code for you to test on your friend’s websites)

So read on for the mini tutorial/reference.

Den ganzen Beitrag lesen…

AMFPHP 1.9 problem with PHP 5.4

I was developing an application at work that used amfphp 1.9. All was working until i decided to bring it home to work over the weekend (thats already a bad thing to do right there), and i started getting errors. At work i use windows 7+xampp (with php 5.3.8), at home i use windows 8+xampp (with php 5.4.7). Identifying the possible culprit was easy enough, but fixing the problem was something else. What i knew was that amfphp version 1.9 had some issues with later versions of PHP.

Den ganzen Beitrag lesen…

XAMPP PHP not working with PostgreSQL

This is a recurring problem i had. For some reason (which i later found out), i couldnt get my php scripts to connect to postgresql. The scripts borked at, for example, pg_connect() (unable to find the function, blah blah).

First off, usually php doesnt come configured to run the postgresql dll out of the box (the windows extension is commented), so you have to uncomment it yourselves.

In php.ini, uncomment (remove the “;”) the following line:


so it becomes


Still, when i restarted Apache, i got the error:

Unable to load dynamic library ‘c:\Programs\PHP\ext\php_pgsql.dll'”

Which was intriguing since the dll was where it was supposed to be.

As it turns out, on later versions of php, the dll is linked to “libpq.dll”, a postgresql dll. So, to fix things, i added the following line to httpd.conf:

LoadFile “C:/Programs/PostgreSQL/9.0/bin/libpq.dll”

(replace the path to your postgresql installation dir, of course – and be careful copying the line above because of the “)

this way the dll is preloaded when apache is started and we dont get the “Unable to link…” error.

This was tested with xampp on windows xp pro, with apache 2.2.17, php 5.3.5 and postgresql 9.0. Other versions can probably affect the solution presented.

PS – On some situations (probably php versions), it is needed to replace the php_pgsql.dll file that comes with php with one from a previous version. This wasnt necessary on my case.

Heres a useful link from where i got the info: (see the comments)


Ran into the same problem, but this time on windows 7 64bit. I was using XAMPP 1.7.7 (Apache 2.2.21 / PHP 5.3.8) and postgresql 9.1 64bit.

– I uncommented the php_pgsql.dll in php.ini, restarted apache and got an error.

– Added the loadfile line (see above) to httpd.conf and the apache service couldnt start.

No matter what i did, nothing worked. After a few hours of head banging, i had the bright idea of searching for the 64bit part on the postgresql install and i found this article:

(in case you dont want to read, it says, in a nutshell: you are safe using the 32bit version of postgresql on a 64bit OS)

So i thought the problem might be from just that. Heres what i did:

– Installed the 32bit version of postgresql 9.1

– Added the loadfile line to httpd.conf.

– Restarted the apache service…and it worked.

So we end up with: Windows 7 Ultimate 64bit + XAMPP 1.7.7 + PostgreSQL 9.1 32bit + Smile

Number formatting/padding before and after the decimal

Before i came up with this solution, i had some trouble formatting and padding decimal numbers, specifically when i want to do it before and after the decimal mark. To top it all, i wanted the result signed (with a plus sign(+) or minus sign(-) depending on the value of the variable)


I want this “-4.3” into this “-04.3”

Or this “6” into this “+06.0”

PHP has a lot of number formatting options, from number_format, to sprontf and printf. Well, just search the php docs and you will see what i mean.

The thing is that, as always, i wanted to do it in as less code as possible so, to do what i mentioned above, and after a bit of testing, i came up with this:

$value = -14.6;
printf('%1$s%2$02d.%3$01s', ($value <0 ) ? '-' : '+',floor(abs($value)),(abs($value)-floor(abs($value)))*10);

Although pretty simple, i think its still a bit contrived and dirty. But it works.

A bit of background:

the first part of the printf means:

%1 will contain the first parameter (the value after the first comma), with $s as the format (in sprintf, ‘s’ means string, so it will just output the result of the first parameter, no formatting)

%2 has $02d as format so it will show a pad of two digits of an integer treated number showed as decimal

%3 has $01s as format – like i mentioned for %1, shows a string with a pad of one digit

Since i want to keep these posts simple, to serve as reference and jump to other reference sites, i will just leave some links:

PHP docs for sprintf (it has all the formatting…formats):

W3C for PHP math:

Just ask if you need any more feedback or help.

First post / Primeiro post

This site will contain an archive of all the junk, problems, code snippets, tricks, hacks, solutions and garbage i want to keep to later use, reuse, alter, etc.

Hopefully it will be of help to someone else.

In portuguese:

Este site conterá um arquivo de todas as tralhas, problemas, soluções e lixo que eu vou querendo arquivar para mais tarde recordar.

Talvez ajude mais alguém.

%d bloggers like this: