How would I make an 'IF' statement in PHP, but with the following conditions?

  • Thread starter Thread starter H
  • Start date Start date
H

H

Guest
Hi, I am a PHP newbie, and I just wanted to know how I would be able to make an IF statement that needed this:


if $subject has nothing selected from a drop down list (by that I mean the first value in the list, which is blank), I want it to print "Problem"
OR
if $subject has 'Other' selected AND $other has nothing written in it, I want it to print"Problem".


Here is part of my code, showing what I have tried



if ($subject = "" OR (($subject = "Other") AND empty ($other))) {
echo 'Problem';
}

However, when I run it, it seems that it only prints 'Problem' when nothing is written in $other, regardless of what is selected from the drop down list.

Thanks in advance

P.S sorry if this is all complicated - I found it hard to explain!
 
use the 'isset' syntax. this will check if a value has been set or not.

since you're checking to see if a value has not been set, you need to put an exclamation mark in front of the 'isset' syntax, like this !isset.

_____________________________

so your code will look something like this:

Code: 1
if (!isset($subject)){ // if $subject is not set (is blank)

// do this

echo "Problem";
}

The above code will print "Problem" if $subject is blank.

_____________________________

This is the code for: if $subject is equal to 'other' and $other is blank

Code: 2
If ($subject == "other") { //if $subject is equal to 'other'

// do this code
if (!isset($other)) { // if $other is blank

// do this code

echo "Problem with $other"; // print "Problem with $other"

}

}

The above code will only be run if $subject has a value of 'other'.

so when the value is 'other', it will check to see if the value of $other is blank.

if it is blank, it will print "Problem with $other"

Your final code will look something like this:

_____________________________

if (!isset($subject)){ // if $subject is not set (if $subject is blank)

// do this

echo "Problem";
}

If ($subject == "other") { //if $subject is equal to 'other'

// do this code
if (!isset($other)) { // if $other is not set

// do this code

echo "Problem with $other"; // print "Problem with $other"

}

}

_____________________________
 
It is a newbie mistake:

if( $subject == '' || (($subject == 'Other') && empty($other)) {
echo 'Problem';
}

The 'is equal' operator is '==' not '='
 
Back
Top