Six Cool PHP Tricks You May Not Know

Over the years I’ve come across some useful tricks in PHP that are not obvious, but are worth mentioning. This is not meant to be a comprehensive list of all the useful tricks that you can do with PHP.
1. Count Characters in a String
To do this, I’ve usually just used the function strlen(). However, there is a faster method. Take a look at the following code example:

$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 because it is zero based.

Read more at PHPBuilder

Comments are closed.