Pure PHP code, part one

Posted on 19/10/2014
Pure PHP code, part one

It is not a secret that everyone was a beginner and wrote a code of disgusting quality. The skills that allow writing really tidy code come with experience and sometimes one has to admit his omissions in PHP syntax skills. In this article I collect a few simple examples of helpful PHP functions which make you code look much more pleasant.

php -a

Ask yourself have you created a separate PHP file to run a function, which description occupies a line? You can do all this in a console using PHP interactive mode. Of course, this possibility may interest Linux/Mac OS clients and those having virtual server (VPS). Look, what comfort it is to test something small in PHP interactive mode.


What is to be done if you need to apply a function to each array element? Can you only do it in foreach loop? But there is a more elegant solution!

php > $values = array('cat', 'dog', 'elephant');
php > print_r(array_map('strtoupper', $values));
   [0] => CAT
   [1] => DOG
   [2] => ELEPHANT


Imagine you have an array of values where the blank rows or NULL values can appear. What is the quickest way to clear such an array? For example

php > $values = array('cat', 'dog', 'elephant', null, '', 'monkey');
php > print_r(array_filter($values));
   [0] => cat
   [1] => dog
   [2] => elephant
   [5] => monkey


Do you remember your feelings when you need to be sure that if you are passed a number less than 10, it needs to have a zero leading format (e.g. 07) . Everything is solved by str_pad()

echo str_pad('0', 2, '4');


What if you need to make a row dynamically and its row contains different values? Something like

$str = ‘My first name is ’ . $first_name . ‘ and my last name is ’ . $last_name?

Sometime ago I found out such line could be created applying sprintf(). For example,

$str = sprintf(‘My first name is %s and my last name is %s’, $first_name, $last_name);

Sizable difference, isn’t it?

chain method

Did you remember your own surprise at PHP classes which allow doing something like:

 $driver -> cache() -> file() -> save();

The secret is to return $this at the end of each routine. For example

class Cat {
public $hungry = true;
public $happy = true;
public function feed($food)
$this -> hungry = empty($food) ? true : false;
return $this;
public function hug($qty)
$this -> happy = $qty > 5 ? true : false;
return $this;

$my_cat = new Cat();
$my_cat -> feed(‘fish’) -> hug(4) -> feed(‘water’) -> hug(10);


Trying to find out one or another variable value, what do you do? Don’t you output this variable into the browser using print_r()? This can be done with the help of error_log() which supplies the variable values to log file and doesn’t show any strange symbols in user’s browser. For example:

error_log(print_r($_POST, true));

By the way, it's much easier “to monitor" log file with the help of tail.

tail -f /var/log/nginx/error.log

Back to Blog

Subscribe to blog updates


comments powered by Disqus