Top 4 variable handling functions in PHP

PHP has several variable handling functions, but I am going to discuss here top 4, that I use regularly when scripting. These examples are relatively fundamental, but should provide a useful starting point for those looking to use them to boost the capacity of their PHP scripts.

Let’s start with the ‘gettype‘ function. Realizing the variable type is quite vital. It can provide additional security within scripts, as well as offer back end users more authority over their content management system. Of course, among other advantages.

$arr = array(3, 6., NULL, new stdClass, 'type');
foreach ($arr as $res) {
echo '<div class="samples">' . gettype($res) . '</div>'; }

The above code block is quite basic, which returns what the variable type of each array value is. The outcome is as below in the browser.


It may look quite bare bones, but remember that one might use conditional statements and other techniques from this level on to boost a script’s functionality.

As it is used quite often, the next feature may look familiar. Within a few articles published, I used it here on the website because I thought to go through it here as well.

$arr = array('Yes,', 'this', 'is', 'an', 'array.');
if(is_array($arr)) {
foreach ($arr as $word) {
echo $word . " ";

Checking whether something is an array can stop unwanted errors and provide much-needed safety in public-eyed websites. This is also a basic block of code, but it gives a little more authority by using an if declaration to verify whether our variables are within an array.

Here is the outcome:

Yes, this is an array.

In fact, the next 2 scenarios are quite identical, but each offers its own features and constraints. The variations in these examples may be quite evident, but at a later date I will go into more detail about that.

$arr = array(17, 'no', 246, 'type');
foreach ($arr as $res) {
if(is_numeric($res)) {
echo "$res is numeric."; } 
else { echo "$res is not numeric.
"; } }

The above code block checks to see if each of the variables within the array are numeric or not. This is fairly self-explanatory, I believe. The result…

17 is numeric.
no is not numeric.
246 is numeric.
type is not numeric.

Now we can look at the function ‘ is int. ‘ Note that without using other features, anything with single or double quotations will not be acknowledged as an integer. Also, an integer is not regarded to be anything with a decimal point. I can go on to examine that further later.

$arr = array(1174, '29', 98.35, 1945);
foreach ($arr as $res) {
if(is_int($res)) {
echo "$res is an integer."; } 
else { echo "'$res' is not an integer."; } 

1174 is an integer.
’29’ is not an integer.
‘98.35’ is not an integer.
1945 is an integer.

Though these examples, again, are quite elementary, you may see the ability to take them to much greater functionality levels. Like most PHP language features, it is always suggested to use multiple at a moment. However, it is never a bad practice to have a fundamental understanding of the most popular variable handling features.

Add a Comment

Your email address will not be published. Required fields are marked *


My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.

Nohman Habib


Request a Quote