John Davidson

PHP Operator / IF Statement not working correctly

0 comments
Message:


I have a problem, I want to check if all my fields aren't empty, I wanna do this in a single if statement, but there is a problem, it only works if both fields return empty, then I get the response returned which I call back in another file, but if one is empty, it doesn't return anything or a NULL. If I use a && (AND) operator it does work, can anyone explain me why and what is wrong?


if (!empty($voornaam) || !empty($achternaam)) {
} else {
$response = [
"status" => "error",
"message" => "Check nog even of alle verplichte velden ingevuld zijn!"
];
return $response;
}


My Answer:
single if statement to check if all fields are not empty, you can use the logical AND operator (&&) to combine multiple conditions. Here's an example:

php
if (!empty($field1) && !empty($field2) && !empty($field3)) {
// All fields are not empty
// Do something
echo "All fields are not empty";
} else {
// At least one field is empty
// Do something else
echo "At least one field is empty";
}


This code snippet checks if all three fields ($field1, $field2, $field3) are not empty. If all fields are not empty, it will echo "All fields are not empty". If at least one field is empty, it will echo "At least one field is empty".

Make sure to replace $field1, $field2, $field3 with the actual variables holding your field values.

Rate this post

4 of 5 based on 5466 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us