If $ _POST is empty. Multiple function

I have the following $ _POST function to check if the "start", "middle" and "end" fields are empty.

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 values ​​in one function, in other words, I want to check several values ​​at the same time. I saw several methods, but not sure which one is right using a 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 someone tell me the correct code for this kind of education?

-1
source share
3 answers

here are some basic ones .. I made it as a comment (since I wasn’t sure that this is what you asked for), but I think the answer will be appropriate with a few details.

  • AND operator

& & will check each condition, and if all are true, it will return true ...

take it like that

 if(FALSE && TRUE) 

it will always return False, and if it fails, because one of the conditions is false

  • OR operator

TE || will check the first condition, if its true value returns true else, check the second condition. If all are false (none are true), it will return false.

following the previous example again

 if(TRUE || False || False) 

now the compiler checks the first condition if its true ignores the following two conditions and returns true.

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

  • Comma operator

if you , operatior, then the last condition on the right will be checked, and if it is true, 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 

therefore, select the operator according to your logic.

USE THIS:

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

You are 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 } 
+2
source
 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.'); 
0
source

All Articles