Loading data from a file with PHP

In programming languages common tasks should be easy, and surprisingly often I find my self loading data from some source file into a database. With PHP loading a CSV file into the database – or posting the data to an API – is quite easy.

The only trick is to know the functions file_get_contents, split (and possibly list). The routine goes something like this:

$fileName = 'rawData.csv';
$content = file_get_contents($fileName);
$rows = split("\n", $content );
foreach ($rows as $row) {
	list($item1, $item2, item3) = split(';', $row);
	// Do something interesting...

The script gets the contents of file into a variable ($contents). Split the contents into an array ($rows) and the forach loop across each row. The first line of the loop splits the row into three fictitious items and the rest of the loop could do something interesting to the data read from the file.

I usually run scripts like these from the command-line (yes, you can do that with PHP scripts).

PHP: Removing an item from an array

If you have a php array, but need to nuke an item from it, the unset function is just the tool to do that. When iterating through the array after removing the item, you should be slightly careful.

The unset function does remove the item, but it doesn’t “reindex” the array, so you should traverse the array by index-numbers after removing the item. The issue is probably best illustrated by this example:

$myArray = Array('item 1', 'item 2', 'item 3');
// remove item
// Failing bad loop
for ($loop = 0; $loop < count($myArray); $loop++) {
        echo $myArray[$loop] . "\\n";
// Good loop
foreach ($myArray as $item) {
        echo $item . "\\n";

You can dowload the above example code here.

ZendStudio for Eclipse

For professional PHP development, nothing beats ZendStudio in my book. Currently ZendStudio is in the process of moving from a standalone application to something build on
top of Eclipse. I’m sure it might be a wise move on the long term, but there are a few things bugging me with th current version. The number one issue is shown in the screenshot to the right.

Would someone please tell either Eclipse or ZendStudio, that PHP files do not need to be build, compiled or what ever it is doing – besides wasting my time for a few minutes.

PHP Coding Standards

If you’re part of a larger team developing (and sharing) code, it is a pretty good idea to have some common standards. In the PHP work, such a set of common standards include the PEAR Coding Standards. While it may be a challenge to agree on standards, it can be done – the hard part is following the standards.

One excellent tool to help you check for standards compliance is the PHP CodeSniffer. We’ve had it running for quite awhile and it seems to work as advertised. We’ve made our own (“TDC”) coding standards which is a variation over the PEAR coding standards, and checking if you follow them is as simple as running a simple command line.While it doesn’t enforce following of the coding standards, it does make it a no-brainer to check if you do follow the common standards.

The output from the codesniffer could look something like this:

mahler@mahler$ phpcs --standard=TDC Example.php

FILE: Example.php
[LINE 2] ERROR: "require_once" is a statement, not a function. No parentheses are required.
[LINE 5] ERROR: "require_once" is a statement, not a function. No parentheses are required.
[LINE 7] ERROR: Space before opening parenthesis of function call prohibited
[LINE 9] ERROR: Space before opening parenthesis of function call prohibited
[LINE 20] ERROR: Line indented incorrectly. Expected 0 tabs but found 1.
[LINE 49] ERROR: Line indented incorrectly. Expected 1 tabs but found 2.
[LINE 51] WARNING: Equals sign not aligned correctly. Expected 1 space, but found 2 spaces.
[LINE 59] ERROR: Space after opening parenthesis of function call prohibited
[LINE 61] ERROR: Line indented incorrectly. Expected 2 tabs but found 3.
[LINE 61] ERROR: Expected "while (...) {\n" found "while(...){\n".
[LINE 63] WARNING: Equals sign not aligned with surrounding assignments. Expected 3 spaces, but found 1 space.
[LINE 74] ERROR: Line indented incorrectly. Expected 1 tabs but found 2.

How do you know good code?

One of the great challenges of PHP is that it’s so easy to learn, that just about anyone can learn it with not too much of an effort. While this is great for the number of PHP developers, it also seems to have the effect, that there is a huge number of bad examples of code out there. How do you then know good code?
In my book there are a few signs, which you could judge from – and they may even apply broader than just php-code.

First sign: It does the job

The code should be designed for the challenge at hand. Too often developers seem to apply the same style and energy to solve a challenge – no matter how much they differ in complexity and importance. If it’s a simple little function being developed, it shouldn’t require a huge framework.

If it deals with financial or personal data, it should probably utilize transactions and apply logging. Did the developer think of the challenge they set out to solve? – if so it’s a good sign.

Second sign: Well-structed

How does the sourcecode look? Are there mile long lines of code or are they sanely formatted? Is the code broken into functions, classes or another structure – or is it just page after page of sourcecode? – I don’t need to see all code as classes nor as neat function-libraries, but I do like if the developer has made an effort to break an application into some manageable pieces somehow.

Third sign: Reasonable naming scheme and comments

How does the function names, classnames and variable names look? is it random garbage or does it make sense? – I really hate variables named from $a to $z – and I do hate functions named “doSomething” – without specifying it further. I would expect great code to utilize the same naming conventions (CamelCaseing, underscores and so on) across all functions and/or variables.

If strange – as in unnatural/unexpected – things happen in the source code, I would expect a (short) comment explaining what’s going on.

Fourth sign: Security and Contingency

Did the developer think about security? is the code wide open for xss attacks? is input validated? Is “the unexpected” handled gracefully or does the code explode if exposed to simple URL-manipulation? Do you know what a SQL-injection is? If you need data from another source, what happens if it isn’t available? – does the code blow up or does it burn gracefully?

How do you recognize good code?

Syntax checking PHP on the commandline

I’m sure most people only thing of PHP as a Weblanguage due to be called through a browser. It has however since version 4.3.0 also been possible to use PHP on the commandline – as you do with Perl, Shell scripts and likewise. If you’re using Linux (or an other Unix-like operating system – including Mac OSX) you probably have a few small programs available which can make it a breeze to check if the syntax in all you PHP scripts is correct.

Here’s how. On the commandline type:

find *.php |xargs -I {} php -l {}

… and here’s an explaination of what it does:

  • “find *.php” findes all files with a dot php ending.
  • the pipe-character “|” sends the result to a command called xargs.
  • “xargs -I {} php -l {}” takes the lines one by one password from the find and call “php -l <line input>”.
  • “php -l <line input>” (where line input would be the php-files found in bullit one) runs php with the “lint” parameter which does the syntax checking.

Converting between image formats with Perl

Changing files from one format to another is quite easy with a little bit of ImageMagick . In the example below a JPG image (test.jpg) is converted into a GIF-image (test.gif). To output in a different (ImageMagick supported ) format, just change the “image->Set” line.

[php] no_tags
#!/usr/bin/perl -w
use strict;
use Image::Magick;

my $image = Image::Magick->new();

# To explicitly set image format use this instead:
# my $image = Image::Magick->new(magick=> ‘JPEG’);

my $x = $image->Read(‘test.jpg’);
$x = $image->Set(magick => ‘GIF’);
$x = $image->Write(‘test.gif’);


Image sizes and PHP

If you have the GD extension available, you can do a few useful things. One of the simple things is getting an image size is a simple matter. You can either fetch the height and width in seperate variables – or even fetch the height and width in a string pre-formatted for use in an image tag.

Here’s the example in source:

$load_ext = get_loaded_extensions();
if (!in_array(gd, $load_ext)) {
echo "GD is NOT available";
$nikonFile   = './aarhus_demo_photo.jpg';
list($width, $height, $type, $img_txt)  = getimagesize($nikonFile);
echo "The image is ".$width . " by ".$height."\n";
echo "<img $img_txt src='$nikonFile'/>\n";

And here’s how it looks (source).