PHP Script Tips: Cool Secrets of PHP

Over the years I’ve come across some things in PHP that are not obvious, but are worth mentioning. This is not meant to be a comprehensive list of all of the useful tricks that you can do with PHP. If you have anything to add to the tips, please comment!

 
 

1. Count Characters in a String

To do this, I’ve usually just used the function strlen(). However, there is a faster method of doing this. Take a look at the following code example:

<?php
$string = 'testing';

if(isset($string[6]))
	echo "The string '$string' is at least 7 characters long.";
else
	echo "The string '$string' is less than 7 characters long.";

You treat the $string value like an array by passing an integer value to isset(). If that number plus one is greater than or equal to the number of characters in the string, it will return true. You need to add one since it is zero based.

2. Use PHP echo like a Function

I’ve always thought that if you wanted to concatenate strings with echo, you needed to use periods. But you can actually treat echo like a function and use commas instead (it is also faster). Take a look at the following code:

<?php
$string1 = 'test-string1';
$string2 = 'test-string2';
$string3 = 'test-string3';

echo 'String #1: ', $string1, '<br />';
echo 'String #2: ', $string2, '<br />';
echo 'String #3: ', $string3, '<br />';

3. Use Single Quotes when Possible

By using single quotes instead of double quotes, you save PHP from having to parse your string for variables. It not only is faster, but I find it is more programmer friendly since it is easier to find variables in your code.

Also, when referencing an array that has a string index, always use single quotes. This prevents PHP from having to figure out exactly what you were trying to say.

4. PHP Variable Variables

There have been several cases that have come up, where I needed to access a dynamic variable (where the variable name changed). You can do this easily in PHP by using what is called Variable Variables. Take a look at this example:

<?php
$var1 = 'nameOfVariable';
$nameOfVariable = 'This is the value I want!!!';

echo $$var1;

5. Use Arrays in Form Fields

Not only can you create a form field that creates an element in an array (like: name[‘firstname’]), but you can create dynamic arrays as well. This is especially useful in checkboxes, where the user could check multiple options. Take a look at this html:

<label><input type="checkbox" name="hobbies[]" value="Sports" /> Sports</label><br />
<label><input type="checkbox" name="hobbies[]" value="Hiking" /> Hiking</label><br />
<label><input type="checkbox" name="hobbies[]" value="Swimming" /> Swimming</label><br />
<label><input type="checkbox" name="hobbies[]" value="Watching Movies" /> Watching Movies</label><br />

When the above fields are posted to a php page, each hobby is added to the hobbies array. You can then loop through that array and access each value that was checked.

6. PHP Output Buffering

I have come across cases where something was being output to the screen that I didn’t want (at least at that point, or maybe not at all).

A common example of this is if you have a function or a script that echo’s out a string, but you are using the code in that instance where you don’t want it to print to the screen at that point (when using a template system or framework system, for example). In this case you would still want to be able to re-use the code, but just prevent anything from being printed out at that exact point.

This will make more sense if you look at this simple example:

<?php
ob_start();

echo 'Print to the screen!!!';
$getContent = ob_get_contents();

ob_end_clean();

// Do whatever you want...

// Do something with the printed content (only if you want)...
echo 'Now: ' . $getContent;

You probably were familiar with at least some of these, but I’m hoping that someone will find this list useful in some capacity. 🙂 Please let me know if you have any questions or if there is anything you would like to add.

Photo taken by Gregory Szarkiewicz
Advertisements

11 thoughts on “PHP Script Tips: Cool Secrets of PHP

  1. Hello, A good friend of mine referred me to your site, and I must say that I am very glad that I visited. The information is “gold” to me. I will also be referring some of my friends to your site as well. It is amazing what you can learn online today. Thanks a lot.

    Like

  2. This was a great read, especially the echo like a Function.
    Concatenating with periods drives me bonkers, and I never thought of the possibility of an alternative.

    Like

  3. I just want to point out the following: The ability to do something does not mean you should do something.
    Variable Variables for example. These are generally bad form, and should be used rarely. I would strongly urge formats such as echo ${$variableName} so that it is clearer in your code, it is harder to see $$ than ${$ }.

    Like

  4. Do you know something about using three quotes? Like “”” “””? Someone told me something about this, but I have not found anything related.

    Like

Comments are closed.