How to get results of more than one condition at a time

57
December 08, 2019, at 09:10 AM

I even don't know how to ask my question!

I have a variable like this:

$value['state 1'] = true;
$value['state 2'] = false;
$value['state 3'] = false;
$value['state 4'] = true;

I want to keep some strings about every state that has false value by conditions or something else and if all of states have true value, have another strings, for example:

$message = "";
if ($value['state 1'] !== true){
     $message .= "State one isn't working!\n";
} elseif ($value['state 2'] !== true){
     $message .= "State two isn't working!\n";
} elseif ($value['state 3'] !== true){
     $message .= "State three isn't working!\n";
} elseif ($value['state 4'] !== true){
     $message .= "State four isn't working!\n";
} else {
$message = "All states works!";
}

In this code, after the first true condition other conditions doesn't be checked, so I used switch case at this way:

switch (false){
     case $value['state 1']:
        $message .= "State one isn't working!\n";
     case $value['state 2']:
        $message .= "State two isn't working!\n";
     case $value['state 3']:
        $message .= "State three isn't working!\n";
     case $value['state 4']:
        $message .= "State four isn't working!\n";
        break;
     default:
        $message = "All states works!";
        break;
}

In this code after matching the first case, all strings after that case will be added to $message variable!

Well, I think now you know what I wanna do! can you help me, please?

Answer 1

Separate them and at the end check your $message variable:

$message = "";
if ($value['state 1'] !== true){
     $message .= "State one isn't working!\n";
}
if ($value['state 2'] !== true){
     $message .= "State two isn't working!\n";
}
if ($value['state 3'] !== true){
     $message .= "State three isn't working!\n";
}
if ($value['state 4'] !== true){
     $message .= "State four isn't working!\n";
}
if (!$message) {
$message = "All states works!";
}
Answer 2

You should really use a loop, to allow for easy expansion of conditions in the future:

<?php
$value['state 1'] = true;
$value['state 2'] = false;
$value['state 3'] = false;
$value['state 4'] = true;
$message = null;
foreach($value as $key => $state) {
    if($state !== true) {
        $message .= $key . " isn't working!\n";
    }
}
echo $message ?? 'all states working'; // null-coalescing (PHP 7+)
//If your php version is older you can echo the message using a regular ternary expression:
print $message ? $message : 'all states working';

demo

Answer 3

If you want to check all array indices, you can use something like this

if ($value['state 1'] !== true){
     $message .= "State one isn't working!\n";
} 
if ($value['state 2'] !== true){
     $message .= "State two isn't working!\n";
} 
if ($value['state 3'] !== true){
     $message .= "State three isn't working!\n";
} 
if ($value['state 4'] !== true){
     $message .= "State four isn't working!\n";
} 
if (!$message) {
    $message = "All states works!";
}

Or, if you change your array, you can use a loop and you will not need to write the 100500 of if (...)

Is loop realisation:

$value = [
    'state_1' => [
        'is_work' => false,
        'error_text' => "State 1 isn't working!\n"
    ],
    'state_2' => [
        'is_work' => true,
        'error_text' => "State 2 isn't working!\n"
    ],
    'state_3' => [
        'is_work' => true,
        'error_text' => "State 3 isn't working!\n"
    ]
];
$message = "";
foreach ($value as $arr) {
    if ($arr['is_work'] !== true) {
        $message .= $arr['error_text'];
    }
}
if ("" === $message){
    $message = "All states works!";
}
echo $message;

Output: State 1 isn't working!

READ ALSO
PHP: When using javascript in a html file I could assign var mythis=this in the global scope. Is there a similiar way in PHP?

PHP: When using javascript in a html file I could assign var mythis=this in the global scope. Is there a similiar way in PHP?

When using javascript in a html file I could assign var mythis=this in the global scopeIs there a similiar way in PHP?

52
Is there a Phar available for Microsoft Graph?

Is there a Phar available for Microsoft Graph?

I am trying to work with Microsoft Graph APIs using PHP as per https://docsmicrosoft

48
Tests do not run after upgrading from 4.3 to 4.4

Tests do not run after upgrading from 4.3 to 4.4

A project in Symfony 43 has a functional test suite with 43 tests that all pass

50
how can i limit the returned items into my second object

how can i limit the returned items into my second object

suppose that i have an object Discussions contain many messagesthe question here that i want to return a list discussion with the last message

53