Give feedback to your users (PHP CLI coding)

Posted on July 17, 2011
by

Terminal PHP CLI

Whereas PHP’s main usage is to generate web pages, it can also be used as a scripting language in your shell (just like bash or python). To that purpose, you can use the CLI version of PHP (CLI standing for Command Line Interface).

The purpose of a CLI script is different, it’s not to generate a web page. Instead, it can be an automatisation task for your website or a maintenance task for your server. Therefore, of course, when you write a CLI script, you don’t output HTML, but instead you output short messages that will tell the user if the script went well or not.

This script can be run manually by you or a coworker, or a cron job can run it periodically. In all the cases, giving a feedback about the success or failure of your script is important. It’s not difficult, simply print a message and append a new line character to it (\n, not the HTML breaking line tag <br/>). I find my custom (and very simple) function println() (yes, inspired from Java :)) quite convenient for this. See code below:

Forex for beginners

Posted on May 19, 2011
by

Today, I’m taking the time to talk about my new project, Forexagone.com. It’s a website (in french only so far) whose aim is to teach Forex to very beginners, in a way it has not been done before. We really make Forex easy to learn for beginners, who can really start from the very beginning and end by reaching a good level in Forex trading. Our core business is divided into 3 main sectors.

Forex

Proposal: best practices for writing PHP

Posted on October 3, 2010
by

My motivation in writing this proposal comes from years of PHP practice, when I had to use other people’s code and their code looks like garbage so much that I wanted to throw it to the their face rather than using it. I can’t expect everybody to write clear code, but if I could turn the actual world in a world of beauty and smartness, I’d like people to follow the guidelines in this paper. Note that I didn’t pretend to write complete PHP good practices, as I’m just covering the writing aspect of PHP coding, with concerns such as readability and logic in the flow of code.

Good practices for PHP writing

strtolower and UTF-8

Posted on September 27, 2010
by
Charset issues is something that always made me go mad. And since I’m french and I’ve designed many french websites, it’s something I couldn’t escape, thanks to all these special chars we have in our language 🙂

Well, today an issue came up with the strtolower function. Look at what follows:

$t1 = 'Expérience';
$t2 = strtolower($t1);
echo $t2; // echos 'expience'

See? It drops the two letters “ér”. No matter why and how it processes (for more details about UTF-8/ISO issues, please use google), what matters is that it totally screwed up my beautiful string.

On PHP.net, you can read this function uses the charset defined in the current locale. It means that whatever the encodage of your string is (UTF-8, ISO, …), even if you work with UTF-8 all along (database tables, database connection, page chars …), it will use the current locale charset anyway.

To this, I can see two options.

5 things to know before starting to design a WordPress theme

Posted on September 22, 2010
by

With hindsight, there are things that are definitively not obvious to me when I started to design my first WordPress theme, things which are not very well explained in the official documentation. To begin, be aware that WordPress theming is everything but beautiful coding: no OOP, no MVC, no respect of coding good practices, nor even of common sense sometimes… I’ve learnt these things at my expense, so I’m now going to warn you from the beginning so that you don’t get the cooling effect that I got.

yop.la, my URL shortener coded in 3 hours (with breaks)

Posted on September 16, 2010
by

It was on my todo list for a while: “code a URL shortener“. And yesterday, I don’t know why, I decided it was the day to definitively check this line off. Here is the story of a guy who wanted to code a service in one small afternoon.

tldr: it took around 3 hours to code it all (it took a little more afterwards actually when mates pointed out CSS bugs in IE) and you can see it at http://yop.la.

What to do today?

I woke up at around 1pm this wednesday (I know I know, my rythme of life is a disaster). I went to the kitchen to serve myself the usual bowl of cereals, and switched on TV. What to do today – did i ask to myself? Hmm, I did all the urgent work for the current projects, I redesigned my blog … now what? Ah, there is this line at the bottom of my todo list that hangs for months. Pff, how much would it take, I’m not kind on loosing too much time on a project that won’t get me a penny? Oh, I’m sure it won’t take too much, let’s see if I can do it in one afternoon hehe.

Deal? Deal! Let’s play 🙂 Below, the table of contents:

  1. Phase 1: thoughts and analysis
  2. Phase 2: design the database
  3. Phase 3: build an architecture
  4. Phase 4: finally write the pages
  5. Phase 5: have a coffee
  6. Phase6: put it online and show to mates

Suexec behaviour with nginx

Posted on September 5, 2010
by

This week, I had to set up and configure an nginx server for the first time. If there is something that I think is essential for a web server, it’s to clearly separate the environment of each of the websites that run on it. Especially, when you execute PHP (or whatever) scripts on your website, security is something you have to pay attention to.

Socket server in PHP

Posted on March 29, 2010
by

A socket UK plug

Recently I had to develop a chat room system very quickly. I absolutely wanted to use my favorite language: PHP. So I had to figure out how do sockets work in PHP, and how to give PHP a daemon behavior (read: Give PHP a daemon behavior).

I found many resources on the Internet, more or less exact, more or less commented… All the articles I read were going around the same solution, and actually by having read some other articles about sockets in C and Java afterwards, it’s indeed the same logic for the 3 languages.

I won’t explain the principles of sockets, nor how to implement them in PHP, it’s already fully documented on the Net (see at the bottom of the article). However, as I wrote a class to help me creating socket servers in PHP, I’m going to share it and explain how to use it.

Give PHP a daemon behavior

Posted on March 22, 2010
by

BSD daemon with PHP logoWell, I fully admit PHP is not the best language to write daemons, because of all the performance issues we all know, and infinite loops are not the most beautiful thing you can see in a procedural script. However, it may happen that for a reason X or Y, you want to use PHP to write daemons (it has even happened to me!).

These are my thoughts about this subject. Again, I read a lot of articles over the Internet, took the best of each, add some of my imagination, and made a set of functions and scripts to easily turn a PHP script into a daemon. For those who want the code right now, help yourself: PHP daemonizer