Entries tagged "PHP"

Keeping Code Up-to-Date

When managing websites, keep in mind that the Internet doesn't sit still. That PHP script written 10 years ago probably has a thing or two needing to be updated. There may be security issues, outdated code, etc. In addition to the evolution of programming / scripting languages, you as a developer have likely changed. Your coding practices are likely to be more efficient and more secure. So let's look at why we need to review old scripts on a semi-regular basis. [Continue reading]

Why PHP_SELF Should Be Avoided When Creating Website Links

When looking for articles about PHP_SELF, it seems like most only refer to the dangers of using the variable with HTML forms. However, there are risks with using it in other parts of a website. For example, it may be tempting to use the variable within the href attribute for links. The problem is that those links become susceptible to Cross-Site Scripting (XSS). Let's take a closer look at the security vulnerability of PHP_SELF and a simple alternative to avoid the problem altogether. [Continue reading]

Do Single-Quoted Strings Cause More Harm Than Good in PHP?

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]

Using PHP’s implode() Function to Display an Array as a Value-Separated String

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

When experimenting with strings that 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

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

When displaying 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 list entries chronologically; where the newest is shown first. But what if the user wants the data organized differently? We could let them choose. [Continue reading]

Maintaining a Hidden Content Archive Within a PHP Page

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

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

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]