When writing PHP code, is it better to use single or double quotes? Using single quotes wherever possible will improve the performance of your code, but does it cost too much in productivity when less experienced developers work with the code? After all, certain things won't work as some might expect when using single quotes, such as variables inside the string. Continue reading →
Do Single-Quoted Strings Cause More Harm Than Good in PHP?
January 9, 2012 — Tags: coding practices, PHP
Using PHP’s implode() Function to Display an Array as a Value-Separated String
January 2, 2012 — Tags: PHP
When displaying the contents of an array in PHP, what is your go to method? For example, if the items need to be displayed as a comma-separated or HTML unordered list, would you use a foreach() or for() loop? Utilizing a loop would accomplish the task. But there are other options. Why not give the implode() function a shot. Continue reading →
Slicing Strings with PHP: Be Mindful of Output that Contains HTML Tags
December 12, 2011 — Tags: HTML, PHP
When experimenting with strings which contain HTML code, be mindful of what you're getting for output. Especially if there is something unexpected about the results. That's what I learned the hard way when extracting an open anchor tag from the source code of a web page. The variables used to locate the anchor tag appeared to be working, but for some reason the extracted code wouldn't display to the screen. Let's take a look at where I went wrong. Continue reading →
Sorting HTML Data Tables Part 2: Dynamically Sort in Ascending and Descending Order
November 19, 2011 — Tags: database, HTML, PHP
In last week's post we looked at dynamically sorting HTML data tables. But we only talked about sorting the columns in either ascending or descending. If the user is looking for a last name that appears near the end of the alphabet and the column is sorted from A to Z, they may have a lot of names to go through before finding the one they want. Instead we could provide an option for sorting in both descending and ascending order. Continue reading →
Sorting HTML Data Tables Using the Header Row with PHP
November 14, 2011 — Tags: database, HTML, PHP
When displaying a data tables, how do you usually sort the information? Do you sort by the field that you think is the most important to the visitor? For example, it's perfectly acceptable to show a list of order by date; showing the newest order first. But what if the user wants the data organized differently? We could let them choose the column to sort by. Continue reading →
Maintaining a Hidden Content Archive Within a PHP Page
October 17, 2011 — Tags: archive, code comments, PHP
Do you have content that cycles regularly, such as a news feed, events calendar, etc. If so, there's a good chance that the current entries would serve as useful templates for future updates. "But where do I store these content templates?" Hey, thanks for asking. If you normally delete old information and are unsure where/how to archive it, let's take a closer look at one of the methods described in last week's post (Three Simple Methods for Hiding Website Content Until It’s Ready)—hiding content with PHP comments. Continue reading →
Three Simple Methods for Hiding Website Content Until It’s Ready
October 10, 2011 — Tags: archive, code comments, HTML, PHP
How do you handle content that needs to be temporarily removed from a website? Maybe there is some text that gets recycled on a regular basis or something that hasn't been approved for posting yet. HTML comments could be utilized to hide everything until it's ready to go live. Just keep in mind that the content is still accessible via the browser for those who know where to look. If that's an issue, the information could be moved to a separate file and saved offline…or you could use PHP comments. Continue reading →
Utilizing the Short-hand if() within a PHP String
September 12, 2011 — Tags: PHP
Until recently, one thing has eluded me when it comes to the Ternary Operator [aka the short-hand if()]. It's easy to assign the resulting value to a variable or to display it on the screen, but how is the short-hand if() used in conjunction with the concatenation character? In other words, how is it used in the middle of a PHP string? Continue reading →
Getting Your Projects Done Faster by Writing Less Code with the Short-hand if()
September 5, 2011 — Tags: form validation, PHP, security
Have you coded a simple if() construct that sets a variable to one of two values and thought to yourself "Five lines of code; there should be a better way?" Okay, maybe it's just me. Either way, let's take a look at the Ternary Operator [aka the short-hand if()]. Continue reading →
Condensing if() Constructs to Improve the Scanability of Your Code
August 22, 2011 — Tags: PHP
In a previous post, we went through the typical setup for the if() construct. The good thing (or bad) is that there's a lot of freedom when it comes to writing code. You could for example write the entire if() construct that contains dozens of lines of code and never use a line break. You could, but I wouldn't necessarily recommend it. However, there are other non-"standard" ways of writing out your if() constructs. Continue reading →
