Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 'is_array' and '\is_array'

Tags:

php

if(is_arrray($arr)
{
    //code...
}

 if(\is_array($arr)
 {
     //code..
 }

The two conditions gives same result. But, exactly, what is the difference?

like image 760
Eldhose T J Avatar asked Nov 26 '13 19:11

Eldhose T J


1 Answers

When you use namespace you can override local functions in you namespace, when you use \ you are calling to the global one.

You can read more about in namespaces.fallback

This is a little example extracted from php.net:

<?php
namespace A\B\C;

const E_ERROR = 45;
function strlen($str)
{
    return \strlen($str) - 1;
}

echo E_ERROR, "\n"; // prints "45"
echo INI_ALL, "\n"; // prints "7" - falls back to global INI_ALL

echo strlen('hi'), "\n"; // prints "1"
if (is_array('hi')) { // prints "is not array"
    echo "is array\n";
} else {
    echo "is not array\n";
}
?>
like image 183
mcuadros Avatar answered Sep 20 '22 20:09

mcuadros