Type hinting in PHP variables

PHP provides only a basic implementation of type hinting, namely for arrays and objects. Currently there's no way to determine if a given variable is a string, a boolean, an object or anything else. Thus, many PHP developers prepend a prefix to each variable name in order to determine if a given variable contains a certain type. The following table summarizes some possible prefixes:

Prefix Meaning
ar array
bl boolean
obj object
str string

Some examples:

$strUA = $_SERVER['HTTP_USER_AGENT'];
$arBrowserInfo = get_browser(null, true);

In my opinion, this is a useful way to improve the overall readability of our code. By doing so, we'll stop asking to ourselves whether a given variable contains a certain type or not.

Leave a Reply

Note: Only a member of this blog may post a comment.