Skip to content

Instantly share code, notes, and snippets.

@brainlet-ali
Last active December 3, 2022 11:45
Show Gist options
  • Select an option

  • Save brainlet-ali/ca3cedf67baab47babaea39104222c08 to your computer and use it in GitHub Desktop.

Select an option

Save brainlet-ali/ca3cedf67baab47babaea39104222c08 to your computer and use it in GitHub Desktop.
if conditions in PHP and Laravel
$condition = 'false'; // string length is evaluated to true
if ($condition) {
// true
}
$condition = ''; // string length is zero, so it is evaluated to false
if ($condition) {
// false
}
$condition = 0; // integer zero is evaluated to false
if ($condition) {
// false
}
$condition = 1; // integer one is evaluated to true
if ($condition) {
// true
}
$condition = null; // null is evaluated to false
if ($condition) {
// false
}
$condition = []; // array length is zero, so it is evaluated to false
if ($condition) {
// false
}
$condition = ['a']; // array length is one, so it is evaluated to true
if ($condition) {
// true
}
$condition = new stdClass(); // object is evaluated to true even if it is empty without any properties
if ($condition) {
// true
}
$condition = new stdClass();
$condition->a = 'a'; // object is evaluated to true if it has at least one property
if ($condition) {
// true
}
$condition = User::where('id', 1)
->first(); // use of first() on eloquent model will return App\Models\User object if there is a record in the database, otherwise it will return null
if ($condition) {
// user found
}else{
// null is returned
}
$condition = User::where('id', 1)
->get(); // use of get() will return Illuminate\Database\Eloquent\Collection wrapped 'App/Models/User' inside of items array
if($condition){
// this condition will be true even if there is no record in the database
// because the collection object is not empty and if is evaluating on the collection object size
// just like array length; in this case the collection object will
}
// so a better way to check when using get() is to check if the collection object is empty or not
if ($condition->isEmpty()) {
// no user is found
} else {
// user(s) is found
}
// same goes with Query Builder
$condition = DB::table('users')
->where('id', 1)
->first(); // use of first() on query builder will return stdClass object if there is a record in the database, otherwise it will return null
if ($condition) {
// user found
}else{
// null is returned
}
$condition = DB::table('users')
->where('id', 1)
->get(); // use of get() will return Illuminate\Support\Collection wrapped stdClass object inside of items array
if($condition){
// this condition will be true even if there is no record in the database
// because the collection object is not empty and if is evaluating on the collection object size
// just like array length; in this case the collection object
}
// so a better way to check when using get() is to check if the collection object is empty or not
if ($condition->isEmpty()) {
// no user is found
} else {
// user(s) is found
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment