How to check multiple variables if has values?
USE && instead ||
if (!empty($da1) && !empty($ba2) && !empty($za3)) {
echo $da1.$ba2.$za3;
}else{
echo "one is mising";
}
Check multiple variables at once in PHP using IF
Depending on your scale requirements and how large the data you'll be storing in these vars is, you could just do a straight concatenation:
if($var1 . $var2 == '') {
// blank strings in both
}
Alternatively, using empty()
:
if(empty($var1 . $var2)) {
// blank strings in both
}
Repl.it
Checking if multiple variables is not null, undefined or empty in an efficient way
Because if(variable)
ignores any falsy
value, this will work for you
if(variable && variable2 && variable3)
The following values are always falsy in JS:
false.
0 (zero)
"" (empty string)
null.
undefined.
NaN (a special Number value meaning Not-a-Number!)
Update:-
If there is a case when you want to execute if block even if the value is 0, you have to add an extra check saying either 0 or some other value.
if((variable || variable === 0) && (variable2 || variable2 === 0) && (variable3 || variable3 === 0))
if multiple variables are empTY
if you want to check if variable is empty, you should use empty()
function not &&
when you using &&
string "0" is casted to false, this may be not what you expecting.
if you want to detect if any of keys in array is empty use this function:
function arrayEmpty($keys, $array) {
$keys = explode(" ", trim($keys));
foreach($keys as $key) {
if (!isset($array[$key]) || empty($array[$key])) return true; // isset prevents notice when $key not exists
}
return false;
}
use example:
$array = array( "foo" => "bar" );
arrayEmpty("foo", $array); // false
arrayEmpty("foo bar", $array); // $array["bar"] not exists, returns true
Shorten the way multiple variables are checked if they are empty
You can use ternary operators
The best would be to loop through your variable if you can do it like so :
Using ternary operator :
$variables = [ $var1, $var2, $var3 ]
foreach ($variables as $var){
$Data[] = !empty($var) ? $var: ' - ';
}
Using if/else statement :
$variables = [ $var1, $var2, $var3 ]
foreach ($variables as $var){
if( !empty($var) ){
$Data[] = $var;
}else{
$Data[] = ' - ';
}
}
But if you cannot and still want to do it individually for each variables :
$Data[] = !empty($var1) ? $var1 : ' - ';
$Data[] = !empty($var2) ? $var1 : ' - ';
$Data[] = !empty($var3) ? $var1 : ' - ';
Check if multiple variables are not empty -PHP
Use the logical and operator: &&
:
<?php if ($var1 !== '' && $var2 !== '') { ?>
Also, you can place all of this script in one tag.
Checking if two variables are not empty in PHP
When checking with the OR operator (||
) the code will execute if one or none of them is empty. But you echo both variables even if one of them is empty.
What I think you want to do is use the AND operator(&&
). This way, the code will only execute if none of the variables are empty.
<?php if(!empty($this->element->videos) && !empty($this->element->productdownloads)) {
echo $this->element->videos;
echo $this->element->productdownloads; }
?>
if you still want to show videos, even if productdownloads is empty (and vice versa), you could do a seperate if for each of them, like this:
if(!empty($this->element->videos){
echo $this->element->videos;
}
if(!empty($this->element->productdownloads){
echo $this->element->productdownloads;
}
edit: minor grammatical fixes
Shorthand for multiple or operator to check if different variables are an empty string
Assuming that they are variables, you could create an array from startsWith
, contains
and endsWith
and then check the array contains an empty string like below:
const array = [startsWith, contains, endsWith];
if (!array.includes('')) {
...
}
Related Topics
How to Convert Object into String in PHP
PHP Float Calculation Error When Subtracting
Return Errors from PHP Run Via. Ajax
Reaching 100% Code Coverage with PHPunit
Escaping/Encoding Single Quotes in JSON Encoded HTML5 Data Attributes
Why Do Timestamps Have a Limit to 2038
PHP Array VS [ ] in Method and Variable Declaration
Best Way to Periodically Execute a PHP Script
Weak Typing in PHP: Why Use Isset at All
How to Prefix a Positive Number with Plus Sign in PHP
Inserting Now() into Database with Codeigniter's Active Record
How to Get the Last Path in a Url
Understanding PHP Curl_Multi_Exec
Try Catch Cannot Work with Require_Once in PHP
What Security Problems Could Come from Exposing PHPinfo() to End Users