Category Archives: PHP

Ternary Operators and Other Less-Used Control Structures

I like my code to take up as little space as possible. I believe that compact code is more readable and more importantly navigation is easier.

I tend to write my if statements on a single line when there is only one statement in the block:

if($var) echo "Hooray";

Another thing I love to use are ternary operators:

$win = ($validation) ? "Yes" : "No";

The only problem with ternary operators seems to be that a certain proportion of developers refuse to use them. Most often than not, it’s fear of doing things ‘differently’; they’re used to if-else control structures and anything else is new and scary.

I’ve often heard the excuse that developers don’t want to write code which others won’t be able to pick up. In my opinion, that’s a total and utter cop-out! If a developer were to pick up my code and see something they don’t recognise, there’s a chance that they’ll look into it and learn something new, even if only to understand the code they are working with. Surely, that’s a good thing.

Developers need to be adaptable and open to new coding practices otherwise they stagnate and they stop learning. What’s the point in having 10 years of experience if the code you’re writing hasn’t evolved?

I look back at the code I wrote when I got my first commercial web development job and it’s vastly different to the code I write now. I’m also constantly reviewing the way I structure the PHP, SQL and Javascript code which I churn out. For instance, I recently had the revelation that rather than doing something like:

$win = ($validation && $winning == 'yes') ? TRUE : FALSE;
//Or:
if($validation && $winning == 'yes') $win = TRUE;
else $win = FALSE;

I can just do this:

$win = ($validation && $winning == 'yes');

Any expression (so I believe; comment if I’m wrong) within the parentheses will assign a boolean value. How clean is that?

Share Button

Script Wrappers in PHP

I’m guessing that most web developers with a few years of experience have at least one nightmare client. I’m currently struggling with a total arse of a project. This one in particular was originally built some years ago by a predecessor of mine who, to be fair, was fairly lazy in a lot of respects.

Part of the work I’m currently doing on the site involves moving the whole she-bang over to one of our newer, faster, better severs. The initial move went surprisingly well, (I LOVE¬†WHM’s¬†account transfer features), but I soon hit upon a large stumbling block.

Continue reading

Share Button

Array Looping in PHP

I remember back when I as learning PHP I had to write a directory “spider” function to list the contents of a directory. (Including sub-directories).

The approach I took involved reading the initial contents of the base directory into an array. The plan was then to loop through the array adding any sub-directories, (and their files), recursively.

In my naivety, I assumed that I could do something similar to:

foreach($results as $result){
	if(is_dir($result)){
		//Add directory's contents...
	}
}

But, I quickly learned that the foreach construct works on a copy of the array, so adding to it in a recursive fashion is not possible. Bum.
There’s a fairly simple solution to the problem though:

for($i=0; ; $i++){ //Infinite loop
	if($i < count($results)){
		$result = $results[$i];
		if(is_dir($result)){
			//Add directory's contents...
		}
	}
	else break; //Nothing left to process, so break out of the loop
}
Share Button