PHP Tips

Date unknown · Last updated: 11th February 2022

For more, please see the PHP Category page. Note that these are just odd tips I have found useful, not an exhaustive list of every tip I know.


Use The Heredoc Syntax For Strings With Variables In

One of the best things I have come across recently is the heredoc syntax in PHP. There are two ways you often see to output a string that contains variables. The first looks like this:

echo "The keeper's squirrel /"Mikey/" ate $value nuts.";

What you see is a string that's happy to contain a variable, in this case $value. The problem lies in the fact that all strings in this format are checked for variables, even when there aren't any in the string. So you might prefer to use this second approach:

echo 'The keeper/'s squirrel "Mikey" ate '.$value.' nuts.';

Like the previous format, if the same character that's used to start and end the string is used inside the string itself, it will need to be escaped, using the slash before it. The second format also requires any variables to be joined by concatenation, as you can see with the variable $value.

Which method you prefer depends on your needs. I have read that the second method is slightly faster, but only if you prepare the string first before echoing it. Eg:

$string = 'The keeper/'s squirrel "Mikey" ate '.$value.' nuts.';
echo $string;

The heredoc syntax beats both methods for strings with many variables in. It allows you to avoid escaping characters as above, and include any variables without messy concatenation. Here's an example:

echo <<<HTML
The keeper's squirrel "Mikey" ate $value nuts

The rules for using the heredoc syntax are firstly that you must start it with <<< followed by an identifier name. This can be anything, but some people have found that using "HTML" triggers their code editors to display the code formatted as normal HTML would be in various colours, so it's a good idea to use that.

Secondly, you need to end the string with the same identifier, but this must be on its own line, with no tabs or spaces before it at all. Nor anything after the semicolon on the same line.

Also any variables next to valid characters for a variable name will need to be surrounded by curly brackets, as in this example:

echo <<<HTML
Put the column {$pixels}px across.

Otherwise PHP would think the variable name was $pixelspx not $pixels.

Lastly, arrays will also need to be treated in the same way. Eg:

echo <<<HTML
Put the column {$dimensions[0]}px across.

Assigning Multiple Variables To The Same Value

A group of variables can be assigned to the same value in just one line. Eg:

$a = $b = $c = 1;

Mathematical Shortcuts

Instead of writing this:

$a = $a + 2;

You can write the code like this instead:

$a += 2;

Likewise, the following work:

$a -= 2;
$a /= 2;
$a *= 2;

Converting UK To US Dates

A quick way to convert a date in UK format to US date format, so it can be converted into a UNIX timestamp, is to simply reverse the day and month.

Example: date = "19/12/01" (UK date for 19th December 2001)

list ($day, $month, $year) = explode ("/", "19/12/01");
$newdate = $month."/".$day."/".$year;
$unixdate = strtotime ($newdate);
$ukdate = date("d/m/y", $unixdate);

This splits the UK date into chunks then rearranges the first two to make it a US style date. Then it gets the UNIX timestamp ready for further processing.

To convert back to a UK date format use the last line of code above. Note how you can use the slash symbol with date().

Variables - Speeding Up Scripts

UPDATE 7th Sept 2005: This tip was written a long time ago and does not take into account the use of global variables. It's always better to make sure register_globals is off in PHP. I do this all the time now. But I'm not sure if this will impact on the speed improvements I got from this tip in the past. I have added the necessary extra code anyway.

Here's a simple way to speed up scripts considerably. I made a page that differs depending on a variable passed in the address. I found that if I checked the variable was blank several times the page took longer to create. Whereas if I set the variable to a default word, the speed jumped from 1.5 seconds to 0.5. (This matched the speed of the page when the variable was defined already by the address.)

Example - the link might be any of these:


In the first link I left the variable off so the default page appears. I didn't want to add it to the link as it makes the address unnecessarily long. (The page is always accessed from the default link.)

The code used was like this:

$value = $_GET['value'];
if ($value == "") echo "<h1>Default page</h1>";
if ($value == "yellow") echo "<h1>Yellow page</h1>";
if ($value == "red") echo "<h1>Red page</h1>";

I used the variable $value several times in a set of small loops. To speed the page up, all I did was assign the variable at the top of the page like this instead of leaving it blank:

$value = $_GET['value'];
if ($value == "") $value = "default";

Then I amended my code like this:

if ($value == "default") echo "<h1>Default page</h1>";
if ($value == "yellow") echo "<h1>Yellow page</h1>";
if ($value == "red") echo "<h1>Red page</h1>";

I was amazed at the speed increase.

Getting The Date Of Included Files

If you're using separate files in PHP via include or fopen then use this code to get the date the files were last modified on your main page:

$filemod = filemtime("filename");
echo "Last modified :".date("j F Y", $filemod);

Comments (2)

Comments are locked on this topic. Thanks to everyone who posted a comment.

  1. Remco Vermeer:
    What would happen in speed if you used this:

    $value = "default";
    $value = $_GET["value"];

    if ($value == "default") echo "<h1>Default page</h1>";
    if ($value == "yellow") echo "<h1>Yellow page</h1>";
    if ($value == "red") echo "<h1>Red page</h1>";

    Use $_GET[""] if you need to access variables in the QUERY_STRING



    Posted on 1st October 2004 at 2:01 pm
  2. carman:
    you might be able to speed it up a little more if you use switch : case statements.. in switch case statements, once a block is found to be true, the others are not executed.. and if none of the conditions are satisfied, the default code is executed.. which in this case is the <h1>Default Page</h1>

    switch ($value) {

       case "yellow" :
       echo "<h1>Red page</h1>";

       case "red" :
       echo "<h1>Yellow page</h1>";

       default :
       echo "<h1>Default page</h1>";


    Posted on 14th January 2005 at 11:58 pm