Deference Between Is Numeric and Is Object Function in PHP

Introduction

In this article I explain the Is Numeric and Is Object functions in PHP. Both functions are used for checking integer and object type variables and the variable is called a variable handling function. First of all I will discusse the "Is_numeric" function in PHP.

is_numeric

The "is_numeric()" function specifies whether a variable is a number or a numeric string.

Syntax

is_numeric ($var_name);

 

Parameter Description
Var_name The variable being checked or evaluated.

Example

The following is an example of "is_numeric()":

<?php

$var1=678; 

$var2="a678"

$var3="678"

$var4="c-sharpcorner.com"

$var5=698.99; 

$var6=array("a1","a2"); 

$var7=+125689.66; 

if (is_numeric($var1)) 

echo "$var1 is Numeric.<br>" ; 

else 

echo "$var1 is not Numeric. <br>" ; 

if (is_numeric($var2)) 

echo "$var2 is Numeric.<br>" ; 

else 

echo "$var2 is not Numeric.<br>" ; 

$result=is_numeric($var3); 

echo "[ $var3 is numeric? ]" .var_dump($result)."<br>"; 

$result=is_numeric($var4); 

echo "[ $var4 is numeric? ]" .var_dump($result)."<br>"; 

$result=is_numeric($var5); 

echo "[ $var5 is numeric? ]" .var_dump($result)."<br>"; 

$result=is_numeric($var6); 

echo "[ $var6 is numeric? ]" .var_dump($result)."<br>"; 

$result=is_numeric($var7); 

echo "[ $var7 is numeric? ]" .var_dump($result); 

?> 

Output

isnumeric.jpg

is_object

The "is_object()" function specifies whether a variable is an object.

Syntax

is_object($var_name);

 

Parameter Description
Var name The variable being checked or evaluate.

Example

The following is an example of "is_object()":

<?php

// Declare a simple function to return an array from our object

function get_students($obj)

{

    if (!is_object($obj))<br>

 {

        return false;

 }

 

    return $obj->students;

}

 

// Declare a new class instance and fill up some values

$obj = new stdClass();

$obj->students = array('Sachin', 'Dhoni', 'kapil');

var_dump(get_students(null));

var_dump(get_students($obj));

?>

Output

isobject.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all