Interesting Use of PHP Super Globals

When the php.ini directive auto_globals_jit is enabled, $_SERVER and $_ENV super global arrays are created at the time they are first used in the script, rather than being initialized at the start of script. The benefit of this directive is that these super global arrays are not created at all if not used within a script, resulting in a little performance gain.

Continue…

Insecure PHP Constants and Variables

Since the introduction of register_globals feature in PHP, there was a lot of discussion around the web criticizing this feature as a security risk. There established a perception that all websites on servers having register_globals enabled are insecure and vulnerable to hacking/injection. Consequently, the feature went to Off in PHP 4.2, deprecated in PHP 5.3 and completely removed in PHP 5.4. Even before all this, many web hosts disabled this feature. This caused problems to those not-by-profession PHP coders who rely on ease of use of PHP language to code their business or personal website.

Continue…

Faster Random Numbers with PHP

Random numbers functions rand() and mt_rand are easy to use and pretty fast. Numbers produced by these functions range from 0 to PHP_INT_MAX unless you specify your own range. In case you use a custom range, it generates a random number within that range, but – it gets slower.

Continue…

Your own trim, rtrim and ltrim functions in PHP

While we already have trim(), ltrim() and rtrim() in php, here is how we can implement the same feature. These are listed here just for understanding the process. In practice, you are recommended to use the built-in functions for performance and consistency reasons.

Continue…

How to validate ASCII Text – PHP

ASCII is one of the earliest character encoding scheme providing a way of encoding control characters, commonly used symbols, alphabets and digits. Many modern character encoding schemes extend the character set used by ASCII by providing additional characters so as to not only maintain backward compatibility but also achieve the benefits of encoding more international characters.

Continue…

Remove undesired characters with trim_all() – PHP

This function was inspired from PHP’s built-in function trim() that removes undesired characters from the start and end of a string, and in case no such characters are provided as second argument to the function, it removes white-space characters from both ends of the string.

Continue…