Skip to content

Category: Programming

Benchmarking PHP: Is it possible to have too many comments?

PHP is an interpreted language; and as such, the computer needs to process the script each time it is run. Honestly, I don’t know much about the internal workings of PHP; so I don’t know how it handles comments in the code. More specifically, I don’t know how well it handles comments.

I have seen sites over the web going back and forth on whether it’s possible to have too many comments. Most I found said that it didn’t really matter; a few said it slowed execution. As I tend to be the type to over comment my code, I decided to test it for myself with some over the top commented code.

Quick trick for speeding up pagination

One of my web projects, like so many others, has a series of pages where data is displayed in a table. Often this data becomes too large for a single page, and it has to be broken down into separate pages. The 5 dollar word for this is pagination, and you’ve seen it; you just may not have known what it was called. When a page shows links to the next and previous pages and shows you on page x of y, this is what is being done.

Multiple cases for switch construct

Looking at the PHP documentation, it is not clear how to add multiple values for the same case in a switch construct. It seems that you should be able to do

case ("whatever" || "something"):
//Do something;
break;

But, you can’t. I tried, and it didn’t work. Fortunately, after much searching, I found a way to do it; and it’s not near as difficult as I was making it.

A quick PHP function to get Post, Get, or Session variables; and Cookies too

It should be pretty easy to get $_GET, $_POST, $_SESSION, or $_COOKIE variables in PHP code. The problem is that just entering $_GET[‘variable’] causes an error if the variable does not exist. What’s needed is a way to open the variable, and get a blank string if the variable is not defined.

Javascript time formatting function

While working on a web application, I found myself with the need for a Javascript function that would format dates to a consistent state. Several different functions exist out on the internet to perform this task, but none were exactly what I was looking for. Most of them did a fairly decent job of taking a time like ‘6:45′ and plugging the am or pm onto the end. Some even allowed military time so ’20:30’ would convert to ‘8:30 pm’. What I really needed was one that could format a time entered in a normal fashion, but also format if I enter a time like ‘0645’ or ‘2315’. So, I wrote my own.

Benchmarking PHP: Inserting text with include(), require(), or get_file_contents()

Often times in PHP you find yourself needing to insert text. Most sites do this by using a database backend, but you also have the option to include data from a text file. Three methods are available for this; require, include, and file_get_contents. Which is fastest, which is best? When should you use one over the other?