Url Encoding - Use rawurlencode not urlencode !

The URL encoding is one the mysterious functions that PHP programmers does use but without knowing all the details behind it. When one wants to create a link dynamically he just use urlencode, and thi is so necessary because in an URl, there is some reserved characters that have sens for the browsers and the the server-side scripts. You should already know the meaning of ? , & and = characters in the query string, if you don't know see this important Wikipedia article http://en.wikipedia.org/wiki/Query_string

But, you may see somewhere that there is a function called rawurlencode, what is that ? Is it a different encoding convention ? The answer is yes and not.
Well, urlencode and rawurlencode are very similar, they do the same encoding except for one character, the Space character !
rawurlencode encodes the space character as %20, while urlencode encodes it as +, here are few use-difference between both, as much as I understand:

1-Path vs Query vars
In the url path, you should encode spaces as %20 and not +, for example if your folder is called "folder one"
this url will work http://www.exemple.com/folder%20one/index.php , and this will not : http://www.exemple.com/folder+one/index.php because in this last the server will search for a folder called folder+one instead of folder one .
But in the GET variables you should prefer the + character to simulate the space, this is what Google is doing ! But whether you use + or %20 in those url variables, ie you use urlencode or rawurlencode, they should both work.

2-PHP vs JavaScript:
Javascript uses the same convention as rawurlencode, so if you want to encode an url with php but you will decode it in javascript, you should use rawurlencode, and here is the reason:

Lets say you encoded "test 1" within php, if you need to "decode" it in JavaScript using decodeURI() function then decodeURI("test+1") will give you "test+1" while decodeURI("test%201") will give you "test 1" as result.

Summary:
The rawurlencode function is the last one, and it should work perfectly in modern systems with less problem. So this is the encoding function to use most of the time ! While urlencode is kept for legacy compatibility, as well as compatibility with Javascript.

Memory problem in PHP simple HTML DOM

I was using PHP Simple HTML dom, a great and free library for parsing html pages and retreiving info from it. I really liked it.. But when it comes to parsing many differents pages of some website in one php script, you will get an error:

Allowed memory size of 67108864 bytes exhausted,

and this is because simple HTML dom don't free up the memory in real time, so the solution is:

Each time you create a dom object ( foe exemple using: $html = file_get_html("http://someurl/"); or  str_get_html... ), then when you don't need it anymore you have to call _destruct:
$html->_destruct();
unset($html);

“Who Is Online” Widget With PHP, MySQL & jQuery

Do you see in some website a nice list of online users ? Do you want to have one in your website ? That should be easy now thanks to this very helpful and easy-to-install widget:

    “Who Is Online” Widget With PHP, MySQL & jQuery

I hope you will like it :)

String Capitalization Functions: strtoupper-strtolower-ucwords.

In php you can easily manipulate the capitalization of your PHP strings, there are ready to use functions that help you to convert yout text to upper case, lower case, or just the first letter of every word to upper case.

<?php

$originalString = "Testing string Capitalization AbCDe";

$upperCase = strtoupper($originalString); // Result: TESTING STRING CAPITALIZATION ABCDE
$lowerCase = strtolower($originalString); // Result: testing string capitalization abcde
$ucTitleString = ucwords($originalString); // Result: Testing String Capitalization Abcde

?>


This does explain everything about how to manipulate capitalization. In fact strtoupper returns the string in upper case, strtolower converts to lower case, and ucwords capitalize the first letter of each word, without making any changes to other characters in the word. (ie tEsT becomes TEsT with ucwords).

If you have any question, I'm ready to help you.

PHP For Loop

When you need to do the same script many times, for exemple you want to send emails to all your users, you don't need to write a php code for each user, you just need to use for Loop which will do it for each one.
This is how:
<?php
for ($userid=1;$userid<=10;$userid++){
send_email($userid);
}
?>

Where send_email() is your own function that send the email to the user with the id as parameter.

Another exemple to For Loop, let's say you want to count 1+2x2+3x3+...+nxn, this is how you can do it:
<?php
$n=10;
$t=0;
for ($i=1;$i<=$n;$i++){
$t+=$i*$i; // the same as $t=$t+$i*$i
}
?>


So how For loop works ?

1. Set a counter variable to some initial value (i.e $i=0).
2. Check to see if the conditional statement is true(i.e $i<=10).
3. Execute the code within the loop.
4. Increment a counter at the end of each iteration through the loop(i.e $i++, or $i+=1, ou $i=$i+1) .

And this is how to do it:
for ( initialize a counter; conditional statement; increment a counter){
do this code;
}


The php loop will help you a lot in php programming, and there are other ways to do loops other that For, like while.