Archive for the ‘php’ Category

[php] turn off deprecated errors

December 6, 2016 Leave a comment

I have a PHP application running on a webserver and the system administrator sent me an email one day that the Apache log is full of PHP deprecated warning messages and it is caused by my page.

The warning message was the following:

PHP Deprecated:  preg_replace(): The /e modifier 
is deprecated, use preg_replace_callback instead 

Investigating the code, I had “preg_replace()” all over the source. No way that I would correct them manually everywhere. I chose a quick and dirty solution and added the following line to the top of the entry PHP file:

error_reporting(0); // Turn off warning, deprecated, 
                    // notice everything except error

Done, no more barking. This tip is from here.

When the application stops running and terminates with an error, I’ll update the application.

Categories: php

PHP: faking templates

August 1, 2016 Leave a comment

In PHP, how to use templates without a template engine?

Well, the nice way would be to use a template engine, like Twig for instance.

But let’s say you don’t want to use a template engine (for some reason), but you still want to avoid spaghetti code where the controller and view parts are mixed up…

I have an idea for this. Maybe it’s a stupid thing, but I’d share it here. So, without a template engine you can still separate the controller and the view. However, this separation will be in the same PHP file :) On the top you’ll have the controller, where you collect the necessary data, and right below it you will have the view, where you simply use the variables defined in the controller. Let’s see two examples:

Way #1:

$c = array(
    'name' => 'PHP',
<!DOCTYPE html>
Hello <?= $c['name'] ?>!

Here, similarly to Twig, all data are collected in a hash. Now I call the hash simply “c” instead of “context“.

Way #2:
If you don’t want to use a hash, use simple variables. To make the variables that you want to use in the view part more visible, start their names with an underscore.

$_name = 'PHP';
<!DOCTYPE html>
Hello <?= $_name ?>!

What is gained? The controller and the view are not mixed. Although they are still in the same file, they are separated (top and bottom part of the source).

It can be an easy way to avoid spaghetti code. However, as mentioned before, the ideal solution would be to use a real template engine.

We could call it the poor man’s templating system :)

Categories: php Tags:

Jinja2-like template for PHP

August 1, 2016 Leave a comment

My primary language is Python. When I need to do a simple webpage or a REST API, I use Flask with its built-in Jinja2 template engine.

However, I started to work on a project with some friends and our UI developer chose PHP for the frontend. As I also want to contribute to the UI, I looked around the PHP template engines if there is someting similar to Jinja2.

It turned out that Jinja2 was ported to PHP! It’s called Twig and it’s almost the same. So if you use Flask, Twig is a natural choice for PHP.

There are also several MVC frameworks for PHP but I don’t use any (yet?). I have a PHP file (the controller), and a corresponding HTML file (the view, i.e. the template). Let’s see a simple example:


require_once 'vendor/twig/lib/Twig/Autoloader.php';

$loader = new Twig_Loader_Filesystem('templates');
$twig = new Twig_Environment($loader, array(
    // 'cache' => 'compilation_cache',

$context = array(
    'name' => 'Twig',

echo $twig->render('index.html', $context);


Hello {{ name }}!

It will print the text “Hello Twig!” to the screen.

What happens? The index.php file is the controller. Here you collect all the data that you want to print in the resulting HTML output. These data are put in a hash table (dictionary), and it’s passed to the template file index.html.

You can enable the cache in the index.php file. In this case the view will be “compiled” to a PHP file, making it faster. However, during the development you’d better switch it off. As I noticed, when I change the source code, the cache is not always updated automatically. So if you enable the cache and change the source, don’t forget to purge the cache.

Project layout
My project structure looks like this:

├── compilation_cache
├── index.php
├── templates
│   └── index.html
└── vendor
    └── twig
        └── lib
            └── Twig
                └── Autoloader.php
                └── ... (other files of the Twig template engine)

For security reasons, I think it’s a good idea to move the “vendor” folder somewhere else that is not accessible via the http protocol. That is, if your project is served from your “~/public_html” folder, move the “vendor” folder outside of “~/public_html“. I’m not sure but it may be true for the “compilation_cache” folder too.

Categories: php, python Tags: , ,

installing EasyPHP

June 27, 2016 Leave a comment

I wanted to contribute to a PHP project but under Manjaro I couldn’t test it locally. I got a “Fatal error: Call to undefined function curl_init()” message that I didn’t manage to resolve, though the line “” was present in my php.ini :(

After a few hours of trial and error, I decided to develop this project under Windows. I have Windows in VirtualBox, I put the PHP project in a shared folder, so my idea was to edit the source under Linux and visualize the result under Windows.

I chose EasyPHP and installed the latest EasyPHP Devserver that provides a complete development environment.

When I opened the project I got the same error since curl was not enabled by default. On the dashboard I could edit the php.ini file and uncommented the line “extension=php_curl.dll“. However, after restarting the webserver I got another error: libssh2 is missing. I found the solution here (ken’s comment):

"I had to also also copy libssh2.dll into my Apache24 folder 
for this to work with my PHP 5.6.2 installation. So altogether 
I had to do the following:

Move to Windows\system32 folder:
libssh2.dll, php_curl.dll, ssleay32.dll, libeay32.dll

Move to Apache24\bin folder

Uncomment extension=php_curl.dll"

I found all these files in the install folder of EasyPHP. There are two versions of each, one in a “…vc11…” folder, while the other one in a “…vc14…” folder. I worked under PHP 5.6 so I copied the vc11 versions of each file mentioned above.

After this Apache restarted without any error.

Categories: php, windows Tags: , , ,

Convert HTML to PDF

February 17, 2011 4 comments

In this thread you will find a list of “HTML to PDF” converters, e.g.:


Simple shell utility to convert html to pdf using the webkit rendering engine, and qt.

You can get its binary executable here. Then, you can convert an html to pdf like this:

wkhtmltopdf-i386  test.html  test.pdf

The result will be similar to exporting the page in PDF in a browser. This is not a surprise, wkhtmltopdf contains the WebKit rendering engine.

Command line scripting in PHP

February 17, 2011 Leave a comment

When talking about command line scripting, most people think of Python or Perl. But it’s good to know that you can do something similar with PHP too!

Example (hello.php):

   # /etc/php5/cli/php.ini
   # The configuration file for the CLI version of PHP.

   print "Hello, World!\n";

You can execute it with “./hello.php” or “php hello.php“.

Notice that the command line version of PHP uses a different php.ini file.

Consult this page to learn more.

Categories: php Tags:

phpMyAdmin: No activity within 1440 seconds; please log in again

February 10, 2011 12 comments


phpMyAdmin’s “No activity within 1440 seconds; please log in again” message is slowly but definitely driving you crazy. How to remain sane?


Increase the time limit. Open the file /etc/phpmyadmin/ and add the following line to its end:

$cfg['LoginCookieValidity'] = 60 * 60 * 8;  // in seconds (8 hours)

Here I set 8 hours, but you can change that.

When you log in again in phpMyAdmin, this new value will be taken into account.

Categories: php Tags: , , ,