Posts from the “PHP” Category

No tail call optimization in PHP (yet)

Posted on October 20th, 2012

In a previous post about functional programming in PHP I used an example of reading a text file to show how a functional style of programming is different from ‘normal’ PHP coding. The example was functional in that it used first-class functions (a function treated like a variable) but there was one bit that still used an imperative style, the while loop at the end: while ( ( $l = $line() ) != null ) { echo $l; }; This while loop is a sequence of statements that change a program state, whereas we want to write some code that describes how the next line will be read from the file. To do this we need to write a recursive function instead of the…

Photo by: _ElijahPorter

Functional programming in PHP – anonymous functions, closures and tail calls

Posted on October 15th, 2012

This post gives a brief introduction to functional programming with PHP. It doesn’t dive into the details of functional programming but it does try and show how the functional style differs from the common imperative style of coding.Reading the Wikipedia entry for imperative programming should give you a good primer on the fundamental difference between how most of use code every day using PHP and functional programming. Below is a very simple loop for reading a file and printing its contents in PHP: $f = fopen( “robots.txt~”, “r” ); while ( ( $l = fgets( $f ) ) != null ) { echo $l; }; fclose( $f ); Now the same thing but in a functional style: function readLines( $filename ) { $f =…

Adding PHP build system to Sublime Text 2

Posted on July 4th, 2012

Sublime Text 2 has the concept of build systems. This basically means that if you are editing a Python file then you can run the Python interpreter on the source file your are editing and see the output in a console window, all without having to leave the Sublime editor. It’s surprising how much quicker and easier this is compared to having to leave Sublime to swap to a separate console window. This is Sublime’s Tools->Build System menu Surprisingly, there is no supplied build system for PHP, to add one go to “New Build System”. A new tab opens for file untitled.sublime-build with the following in it: { “cmd”: ["make"] } Delete that and replace it with this: { “cmd”: ["php", "$file"], “file_regex”: “php$”, “selector”: “source.php”…

My 2 cents on whether you should use Smarty/Twig/etc in view templates instead of raw PHP

Posted on June 26th, 2012

It’s up there with the big questions of our time: How do we stop global warming? What happens after we die? Do we have free will? Can we cure AIDS? How do we bring peace and stability to the middle east? And, of course: If PHP is a templating language, do we really need Smarty, Twig and other templating engines? Yeah, I know, it’s a biggie. First of all, a brief explanation of what a templating engine is used for: In a MVC framework used for displaying webpages, the view contains the HTML of the page being rendered. A templating engine like Smarty or Twig is often used to render the view, the idea is that controller populates the view with data based on…

Using native PHP sessions with CodeIgniter

Posted on May 9th, 2012

This post explains how to use native PHP sessions ( the $_SESSION[''] global array ) with CodeIgniter 2.1.0 in place of CodeIgniter’s default session storage.  CodeIgniter doesn’t use standard PHP sessions, instead it saves session-data directly in a browser cookie, which limits the amount of data you can save to 4k. Even though it is good practice not to store a lot of data in session it’s unlikely that 4k is going to be enough for any reasonably complicated website. I find it kinda odd that the CI developers didn’t at least give us a configuration option to switch to using standard PHP sessions. Luckily for us it is easy to add a library to save the day. Creating the Nativesession library A CodeIgniter…

Photo by: rafeejewell

PHP 5.4 syntax changes to make your pants tingle

Posted on April 16th, 2012

It’s the little irks and annoyances in a programming language that makes you love it or hate it. A language that has a terse syntax and plenty of quick ways to accomplish common programming tasks will win a developer over. PHP 5.4 has just been released and has a few changes in it that might be considered insignificant by some, but to a programmer who likes to write less code in less time, who likes to get stuff done, they are awesome additions. Class member access on instantiation I’ve already written about this one. As of 5.4 you can call a class member on instantiation, so if you are instantiating a class only because you need to call one of its functions or access…

Two things that would really improve PHP

Posted on October 21st, 2011

I use PHP to build most my personal projects, I’ve been using it for years and I can get stuff built easily and quickly using it. As a language it does suck a bit, I’ve been tempted many times to switch to Python or Ruby but every time I’ve thought about doing that the fact that I’ve got stable, re-usable PHP code always outweighs the learning curve of a new language. One problem with PHP is that it’s is just basically a large collection of functions in the global scope, which are easy to forget and need constantly looking up. Two changes that I would like to see in PHP is changing the inbuilt string and array types to be classes so that all…