Cleaner Embedded PHP If/Foreach Syntax

In the last decade I’ve seen the wide variety of ways in which PHP has been used to control the output of HTML. Whether you’re using a template system like Smarty or you’re simply using PHP itself as a template language there are many different ways of combining the two.

Unfortunately many of them are messy. Years ago I decided that embedding any logic that had to be within the html was better than printing out all of your html using PHP. With the addition of using short tags for printing, you could usually do just fine.

When it comes to a loop of some kind that has to be embedded within a condition, it’s always ugly. For example, the following code was taken from a recent web application. This is hard to look at, and is even worse when you combine the HTML with it.

<?php
if(!empty($array)){
    foreach($array as $item){
?>
...data goes here
<?php
    }
} else { ?>
... no results message
<?php } ?>

Too many lines, too many indentation mismatches, HTML and PHP indentation conflicts, etc. I’m always in search of cleaner, more efficient ways of doing things.

After some time of playing around with various syntax choices and embedding needs, I’ve found what I believe to be the best method:

<?php if(!empty($array)): foreach($array as $item): ?>
...data goes here
<?php endforeach; else: ?>
... no results message
<?php endif; ?>

When you’re mixing PHP and HTML, this solution solves several problems I had with the previous example. Five lines shorter, all three lines of PHP code share the same indentation level, which also means it’s easy to keep HTML indentation separate so they’re not visually conflicting.

I’ve been playing with this solution in some current projects and it really has worked well so far. Even the designer on these projects is happy to see such a clean alternative.

Published by Michael Botsko on 8/4/2010 | Categories: PHP

PHP5 Class for Authorize.net AIM API

I recently had the need to integrate a project with the Authorize.net API and I spent some time searching for an existing PHP class/object that would handle it. While I found several, they seemed to be somewhat out of date, not properly maintained, and most importantly – ugly.

I set out write a very clean, well organized class that was specifically designed for PHP5. I’ve published the class on github and would appreciate any patches you wish to submit back.

http://github.com/botskonet/authorize.net

The class is very basic and will handle the transaction beautifully. It’s working great in a current project. While there are always features I would like to add as the projects grow, this is entirely ready for use and testing now.

Published by Michael Botsko on 4/23/2010 | Categories: PHP

Git Status in Command Line

There are many tricks out there to get the git status to show in the command line path, but here’s one that worked the best for me on a Mac OS X Snow Leopard machine.
Continue reading this post…

Published by Michael Botsko on 3/16/2010 | Categories: Git, Mac

Incompatibility between find and saveAll in CakePHP

In a recent project I wanted to create a separate form for each record from a table using the form helper in CakePHP. After following the instructions found on the net I noticed that there was one big glaring problem.
Continue reading this post…

Published by Michael Botsko on 2/25/2010 | Categories: CakePHP

Installing Git on Mac OS X Snow Leapord

Git is an amazing source control system, and is even better when used with GitHub.com. Installing git on Mac is pretty simple.
Continue reading this post…

Published by Michael Botsko on 2/18/2010 | Categories: Git, Mac

Magic Mouse

I was planning on getting a magic mouse for my office machine sometime in 2010, but my parents got one for me for the holidays. It finally arrived and I’ve had some time to play with it. Here are some briefs thoughts and comments.
Continue reading this post…

Published by Michael Botsko on 1/18/2010 | Categories: Mac

Fastspring.com Ecommerce PHP Class

I’ve recently done some work with the Fastspring.com ecommerce website. It’s essentially similar to the flow of a paypal purchase – companies create pages with products and Fastspring acts as the checkout process doing the user information/payment processing work for you.
Continue reading this post…

Published by Michael Botsko on 1/18/2010 | Categories: PHP

Download Old CakePHP Versions

I’m not really a fan of CakePHP, but I work with it often and understand it well. I recently had a project where a previous developer modified random cakephp library files directly, thus making it difficult for me to upgrade the framework.
Continue reading this post…

Published by Michael Botsko on 1/8/2010 | Categories: CakePHP, PHP

Announcing Peregrine – a PHP Security Class

Today I release a new php caging class for improved security – Peregrine.

I’ve always been impressed by the idea of a “variable cage” in programming. It’s an additional method of protecting your incoming variables and making sure that you only accept what you’re expecting.

If you’re not familiar with a cage, think of it as a class that copies any (usually incoming) data into an object while destroying the original “unclean” data. At this point you must access this data through the “cage”, which provides a whole bunch of ways to filter out what you don’t want.

So an incoming variable (through a GET/POST request for example) may only need to be a integer. You may use a method that returns or checks for integers, and either removes non-integer characters or returns false.

This provides an excellent wall against any malicious attackers, and really helps you define data validation more accurately within your projects.

There are some others out there, and some are very good. However, none worked as well or were as cleanly coded as I would have liked, so I’ve been working on Peregrine.

It’s hosted at github so you’re welcome to fork it and contribute back any fixes or feature requests you would like.

To learn how to use it, please read the documentation provided with the code and look through the class itself.

http://github.com/botskonet/Peregrine

Published by Michael Botsko on 12/30/2009 | Categories: PHP, Software

MySquibbles.com Offline

Almost four years ago I launched MySquibbles.com – a community for parents and teachers to find and share links their children enjoy. It was a great idea and would have been extremely useful, but it failed for several reasons.

Primarily, it wasn’t enough to draw visitors. Parents tend to frequent sites that have parenting information from articles and advice, to links to great deals. Without offering that content as well, Squibbles wasn’t a place you would go because kids are not online that often until they’re older.

The other problem was that the average parent going online with their kids is a target audience that’s very difficult to market to. They typically only learn about things through other parents, school materials, or parenting magazines. Without any time to market or promote the website, it was left online only to serve as a good portfolio piece.

I’ve recently moved servers and during the transition, I made the decision to take the website offline. I’ve kept everything in the event that I want to restore it at some point, but for now it’s no longer needed.

Published by Michael Botsko on 12/30/2009 | Categories: Random Thoughts