Register now or log in to join your professional community.
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
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.
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
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.
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.
// some code
empty() checks the content of your variable while is_null() checks the memory address (pointer to memory) of your variable.
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/
Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.