Jack Maessen Jack Maessen - 2 months ago 6x
PHP Question

how to create condition in php with and / or

I am trying to make a condition in php for this situation:

You can choose for a radio input or checkbox input.
No matter what you choose, but if you choose one of them, it may not be empty.

$quickpolltype = "radio"; // set to "radio" or "checkbox"; radio is one choice, checkbox is multiple choices

I have seperated the 2 conditions now like below:

if($quickpolltype == "radio" and $_POST['radiovote'] != '') {
// do something
if($quickpolltype == "checkbox" and $_POST['checkboxvote'] != '') {
// do something

what i want to achieve:

No matter what you choose as type in
, if the value of one of them is not empty --> do something

So how can i combine those 2 conditions in 1 condition, whatever quickpolltype is choosen?


First it is advisable to use && and || see here 'AND' vs '&&' as operator

Also you are doing an assignment = and not a comparison ==.

So you want something like:

if($quickpolltype == "radio" && $_POST['radiovote'] != '') {
    // do something
if($quickpolltype == "checkbox" && $_POST['checkboxvote'] != '') {
    // do something

Essentially you were making $quickpolltype "radio" rather then checking if it was equal to it. Same for "checkbox".

To make all of that in one if

if(($quickpolltype == "radio" && $_POST['radiovote'] != '') 
    || ($quickpolltype == "checkbox" && $_POST['checkboxvote'] != '')){
    //Do stuff