'If $_POST is empty Multiple function

I have the following $_POST function to check if the fields of 'start', 'middle' and 'end' is empty or not.

if(!empty($_POST['start'])) {
   $description = "a sentence".$_POST['start']." with something in the START.";
}

if(!empty($_POST['middle'])) {
   $description = "a sentence".$_POST['middle']." with something in the MIDDLE.";
}

if(!empty($_POST['end'])) {
   $description .= "a sentence".$_POST['end']." with something in the END.";
}

I want to check the values in one function, in other words I want to check multiple values at the same time. I have seen few method but not sure which one is right, using comma or && or ||, something like below ...

if(!empty($_POST['start']) , (!empty($_POST['middle']) , (!empty($_POST['end']))

or

if(!empty($_POST['start']) && (!empty($_POST['middle']) && (!empty($_POST['end']))

or

if(!empty($_POST['start']) || (!empty($_POST['middle']) || (!empty($_POST['end']))

Can anyone tell me the right code for this kind of formation?



Solution 1:[1]

here are some basic.. i made it as a comment(as i was not sure if this is the thing you asked for) but i guess an answer would be appropriate with a bit of details.

  • the AND operatior

the && will check every condition and if all are true it will return true...

take it like this

if(FALSE && TRUE)

it will always return False and if will not execute because one of the condition is false

  • The OR operator

THe || will check the first condition if its true it will return true else check the second condition If all are false(not even a single is true) it will return false.

again following the previous example

if(TRUE || False || False)

now the compiler checks the first condition if its true it will ignore the next two conditions and return true.

if(FALSE || FALSE || FALSE) - this will return false as all are false

  • THe comma Operator

if you , operatior then the last condition to the right will be evaluated and if it is true then it will return true else false

example

if(True,True,True,False)  -  it will return false

if(FALSE, TRUE, FALSE, TRUE) - it will return true

so choose the operator according to your logic.

USE THIS :

if((!empty($_POST['start'])) && (!empty($_POST['start'])) && (!empty($_POST['start'])));

Solution 2:[2]

Your looking for something like:

// Establish valid post key values
$valid_post_variables = array_flip( ['start', 'middle', 'end'] );

// Fetch post data
$post = $_POST;

// $result will contain the values of post where the keys matched valid
$result = array_intersect_key( $post, $valid_post_variables );

// if the resulting array contains our 3 options, its go time
if ( count( $result ) == 3 ) {
     //start middle and end where all passed via POST
}

Solution 3:[3]

function insertPost($before, $offset, $after)
{
    if(!empty($_POST[$offset])) {
         return $before . $_POST[$offset] . $after;
    }
    return '';
}

$description = insertPost('a sentence', 'start', ' with something in the START.');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Ziumin