Official website for Web Designer - defining the internet through beautiful design
Author: Steve Jenkins
3rd October 2012

Top 5 PHP tips for designers

PHP expert Jeffrey Way reveals 5 essential PHP tips that web designers need to know

Top 5 PHP tips for designers


PHP is an inconsistent language, particularly when it comes to things such as the argument order for its various functions. Do we set the haystack as the first or second parameter of the stristr function? It’s easy to forget.
Assuming you’re not using an IDE, you’d likely perform a quick Google search for the solution. An easier method is to pull up Terminal, and type:

001 php –rf FUNCTION_NAME
Referring back to the previous example, to determine the correct argument order for the stristr function, we can run:

001 php –rf stristr

This command will return:

001 Function [ <internal:standard>         function stristr ] {
002   – Parameters [3] {
003     Parameter #0 [ <required> $haystack ]
004     Parameter #1 [ <required> $needle ]
005     Parameter #2 [ <optional> $part ]
006   }
007 }
That was easy! Now we can see that, yes, the haystack should be set as the first parameter.


There tends to be a natural progression for new developers: learn, discover, abuse, mature. Which, expanded, means:
• Learn the basics
• Discover the tricks and shorthand
• Abuse the shorthand, creating horribly unreadable code
• Mature as a developer, and embrace readability.
Your goal is to reach that fourth stage: maturity. While it’s most certainly fun to test ourselves to see just how much logic we can fit into a single line, doing so is largely considered bad practice. Remember: readability trumps everything. If three extra lines will make the code significantly more readable in six months, then go right ahead and add those three lines!


Hopefully, we’re all familiar with the basic syntax for creating simple arrays.

Simple Array
001 $myArray = array(‘one’, ‘two’, ‘three’);

001 $myArray = array(
002 ‘first’ => ‘Napoleon’,
003 ‘last’ => ‘Dynamite’
004 );
In PHP 5.4, we can use a streamlined, bracket notation that most JavaScript developers will immediately recognise.

Simple Array
001 $myArray = [‘one’, ‘two’, ‘three’];

001 $myArray = [
002 ‘first’ => ‘Napoleon’,
003 ‘last’ => ‘Dynamite’
004 ];

Particularly as a developer whose time is split equally between JavaScript and PHP, this is a welcome addition to the language. The less language shuffling we have to perform in our heads, the better.


Those of us who have been working in PHP for quite some time will be familiar with a few different methods for embedding PHP into a page: short tags, ASP tags, and the standard method that most use today.

ASP Style

001 < % echo ‘hello’; %>
Short Tags


Don’t use the ASP method – it’s disabled in your php.ini file by default, and for good reason. As of PHP 5.3, short tags were largely discouraged, and labelled as a bad practice. The reason for this was because there was no guarantee that the server would have short tags enabled. As a result, most of us generally recommended that all PHP developers stick with the standard form: < ?php echo ‘hello’; ?>.

As of version 5.4, however, this is no longer the case for the echo short tag syntax: < ?=. You are free to use this shorthand syntax without worry, due to the fact that now,

Don’t underestimate how convenient the technique outlined in this tip. We’re not merely limited to variables. If you instead choose to render the style.php file in production, you can accomplish a variety of dynamic tasks, such as loading random header images on each page load.


For example:

001 < ?php $headerBG = rand(1,10); ?>
002 .header {
003 background: url(images/< ?= $headerBG %>.jpg) no-repeat;
004 }

With this technique, each time the page is rendered, the stylesheet will reference a random image within the images/ folder. Populate that folder with ten images – 1.jpg to 10.jpg – and you’re all set to go!
If you do elect to serve PHP files as CSS, please, please, please make sure that you do your research first.

Tags: ,
  • Tell a Friend
  • Follow our Twitter to find out about all the latest web development, news, reviews, previews, interviews, features and a whole more.