Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

What is the different between empty() , is_null() Function in PHP?

If I try to get an object of Active record on Yii $myObj = DATAMODEL::model()->findByAttributes($attr); if the query not returning any data what I should used is_null or empty

user-image
Question ajoutée par Khadijah Shtayat , Technical Lead , Opensooq
Date de publication: 2014/02/12
Clodelio Delfino
par Clodelio Delfino , Managing Consultant , Startup Company

You can use empty() because it always return a CActiveRecord[] which is an array based on Yii API. Using is_null() is good to use if you're checking for return value that is of type null or using "===" similar mechanism but not for array result.

Ibrahim Hatoum
par Ibrahim Hatoum , Web developer , Tristan Inc.

I usually create a function called is_null_empty($arg) as follow

 

function is_null_empty($arg) {

    if (is_null($arg) || empty($arg)) {

        return false;

    } else {

        return true;

    }

}

 

that way i make sure that the argument im resting for is neither null or empty. but if you want to know the exact difference is_null returns true when the value is of type null instead empty() returns true if the returned value is an empty string, an empty array,0 or a false value

Mohammed Safadi
par Mohammed Safadi , Manager of e-Services and Web Development Department , Ministry of Education

empty: Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE.

 

is_null: Finds whether a variable is NULL

 

<?php

$a = '';

var_dump(is_null($a), empty($a)); // bool(false) bool(true)

$a = NULL;

var_dump(is_null($a), empty($a)); // bool(true) bool(true)

?>

 

Thus, you can use empty() as it will return TRUE for both NULL or empty values.

Asteway Shitaye Woldehitsan
par Asteway Shitaye Woldehitsan , Mobile Application Engineer , golfscape

The best way to handle this situation is using simple "if" statement. as Yiis CActiverecord "findbyAttributes()" return Active data record or false if no match was found.

 

the best approch to avoid errors and problem is to include all your code thats dependent on the object under if statem. Here is an example.

$myObj = DATAMODEL::model()->findByAttributes($attr); 

if($myObj)

{

     //your code goes here

}

 

hope that helps. Let me know if there is more i can help.

 

 

Fadi abu alnaser
par Fadi abu alnaser , Senior Software Engineer , Cloudview Limited

I  use if not ( ! ) because findByAttributes return null if no data found

readmore findByAttributes-detail

ex:

if( !  $myObj ) {

 // some code

}

Muktar SayedSaleh
par Muktar SayedSaleh , Software Engineering Manager , AIRASIA

empty()  checks the content of your variable while is_null() checks the memory address (pointer to memory) of your variable.

Muhammad Majid Saleem
par Muhammad Majid Saleem , Senior PHP Developer / Project Manager , SwaamTech

A variable is NULL if it has no value, and points to nowhere in memory.

empty() is more a literal meaning of empty, e.g. the string "" is empty, but is not NULL.

 

Read complete article:

- http://stackoverflow.com/questions/5615747/what-is-the-difference-between-null-and-empty

- http://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/

More Questions Like This

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?