Archive for the ‘PHP functions and features’ Category

PHP Command line interface, set and read ENV variables

When a variable on the console is set export VARIABLE=VALUE; You can get it from PHP Command line (CLI) using getenv(‘VARIABLE’)   Example #script.php <?php var_dump(getenv(‘VAR’)); ?> From command line php -f script.php //bool(false) export VAR=a; php -f script.php //string(1) “a”  

Useful options for PHP Command line interface

I find PHP command line interface very useful, for CRON jobs, testing and whenever apache is not necessary. Basic commands here: CLI php.net manual. In this article I’m writing some CL flags and few lines of related functions / PHP code I find useful To make script interactive and read the line from the console $var [...]

Using PHP Closures to get cached object with one call

Getting caching objects if a fairly frequent task when dealing with performative web applications. Standard approach if (cache object is valid)  { return cached object } else { get fresh object (*) save object into cache return object } I don’t like that approach as there are a lots of LOC that wraps the only [...]

sessions vs cookies with load balancer

It’s a well known fact that HTTP is a stateless protocol and cookies are needed to keep the communication session. When dealing with sessions with PHP, we have two main solutions: – set manual cookiesOne approach is using directly the PHP functions to set the cookies. When possible I don’t use manual cookies(except than keeping [...]

PHP for file moving / managing

PHP is a technology damned cool for web application but the language doesn’t provide an advanced syntax as Python in itself. However, by writing readable and maintenable code (for instance by smartly separating features in function and classes), PHP is satisfying also to make file managing / system scripts, IMHO of course ExampleHere is a [...]

PHP Arrays sort

There are lots of PHP defined function to order arrays (the core of PHP, technically are hashmaps), ordering by value or keys, preserving key order or not, using an user-defined function, normal or reverse order, etc… Following, some clear example of the main ones, and some tips about how to remember them ORDER BY VALUE [...]

PHP 5.3

I’ve just read this pdf from I.A.’s blog about PHP 5.3 performances. My comments: Performances What I really consider good is the performance increasing (5/10%) that include a smarter behaviour with require/inclusion, smaller binary size and better stack performance. Features – Namespaces. – Late static binding. It was the only big lack of the PHP [...]

UTF8 in LAMP applications: overview and how to solve the common issues

The problem In a LAMP application the text is frequently saved/retrieved from/to a database and files. We must consider all the different encoding (mapping characher-byte value): latin1 iso8859-1, latin9, UTF-8 (utf8) etc… Lots of applications use ISO8859 encoding and some PHP functions to convert the characters (htmlentities, htmlspecialchars etc…) The solution Converting all the text [...]

benchmark: boolean variables value check

$a = NULL; $t = microtime(true);for($i = 0; $i

benchmark: array_key_exists vs isset

When the aim is to check if the element exists in an array and has a value not null, better to use isset(array[index]), that is much faster than array_key_exists(index, array). #PHP 5.3.0 command line – Amd turion 64 1.6 Ghz #create array$a = array();for($i = 0; $i