PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 28, 2022

[FIXED] How To Check 5 Empty Variables in PHP

 October 28, 2022     conditional, is-empty, php     No comments   

Issue

I need to make a conditional statement where the 5 variables should be empty before perform an action. The problem is some variables can be zero, false or NULL value. How to catch all of them?

Which one is better?

If (!$a && !$b && !$c && !$d && !$e) {

  // do some action 

} else {

  exit;

}

OR

If (empty($a) && empty($b) && empty($c) && empty($d) && empty($e)) {

  // do some action 

} else {

  exit;

}

Thank you.


Solution

This could be an approach to check multiple variables are empty at once.

<?php
$a = $b = $c = $d = $e = '';
#$b = 5; // comment out to go on else block
if (empty($a . $b . $c . $d . $e)){
    echo "All variables are empty, do what you want to do man";
}else{
    echo "One of variable is not empty";
}
?>


Answered By - Always Sunny
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing