0

I have a problem with my if and else statement in PHP where it never runs the else statement.

The input form is in HTML:

 <input type="radio" name="marital_stat" id="single" value="single" />Single <input type="radio" name="marital_stat" id="married" value="married" />Married <input name="age" type="text" size="5" maxlength="3" placeholder="30" required/> <input name="work" type="radio" id="employee" value="employee" />Employee <input name="work" type="radio" id="own" value="own" /> Own Business <input name="work" type="radio" id="jobless" value="jobless" />Jobless <input name="place" type="radio" id="urban" value="urban" />Urban <input name="place" type="radio" id="rural" value="rural" />Rural</td> 

Here is the PHP Code:

if ($marital_stat == 'married') { if ($age >= 18 || $age < 59) { if ($work == 'jobless') { if ($place == 'rural') { $loan_credibility == 5; } } } } else if ($marital_stat == 'single') { if ($age >= 18 || $age < 59) { if ($work == 'employee') { if ($place == 'rural') { $loan_credibility == 1; } } } } 

Here is a condition that will display some output:

$A = 'positive'; $B = 'negative'; if ($loan_credibility == 5 ){ echo $B ;} else{ echo $A; } 
9
  • 1
    what's the input to the if? forst guess the $marital_stat is neither married nor single - also, it would help to know which language is this. Commented Jun 4, 2015 at 15:47
  • Please provide a complete example: stackoverflow.com/help/mcve Commented Jun 4, 2015 at 15:49
  • hi @LorenzoBoccaccia, i've added some input code for the if and the language for if else is in php language. Commented Jun 4, 2015 at 16:58
  • @Farahin Samsudin: have you updated $loan_credibility == 1; and $loan_credibility == 5 to use a single equal sign, not double? If so, the please update your example code ...@Mohamed Belal pointed this out in his answer. So two problems to sum this up 1) incorrect if-else if-else and incorrect = vs == Commented Jun 4, 2015 at 18:16
  • @Farahin Samsudin see my updated answer with combined fixes - thanks. Commented Jun 4, 2015 at 18:23

2 Answers 2

2

As I see you make $loan_credibility == 5; or 1; the == only used in equality statement it checks the two sides if they equal and you have to use = to set value, not == so it will be $loan_credibility = 5; or 1;

Sign up to request clarification or add additional context in comments.

4 Comments

Please don't forget to mark best answer as correct for this question
i've tried as you suggested, it changed the result. but when i'm trying click the input for the positive result, it keep display the negative result.
Are you sure that the condition which in if statement are being made with correct values to produce $loan_credibility ?
i'm pretty sure. basically, it depends on job and place. if employee + urban the result will be negative. and if jobless + rural/ urban will be also negative. but if employee + rural, it should display positive. so, i've put the value 5 to return the negative statement and 1 for positive statement
0

You have no else clause (which logically is necessary when using else if)...

if (condition) { code to be executed if condition is true; } elseif (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; } 

Do any of the following (#1 or #2 make the most sense)...

  1. Make your else if into a separate if block
  2. Change else if to else
  3. Or add an else clause

Also, as @Mohamed Belal pointed out, when setting variables use = not ==.

So two things to fix your problem: 1) if-else if-else logic and 2) = vs ==...

if ($marital_stat == 'married') { if ($age >= 18 || $age < 59) { if ($work == 'jobless') { if ($place == 'rural') { $loan_credibility = 5; } } } } if ($marital_stat == 'single') { if ($age >= 18 || $age < 59) { if ($work == 'employee') { if ($place == 'rural') { $loan_credibility = 1; } } } } 

10 Comments

i've chaged the code as you suggested, the result is still same. but for this time it only display echo '$A' which is positive. previous, it only display the negative word as the result.
If age >= 18 || age < 59 is always true. Age 3 because 3 < 59 and age 105 >= 18. What you want to do is check >= 18 && age < 59.
This is a bit misleading... If nothing should be done unless $marital_stat equals 'married' or 'single', then else is not actually required. My guess is that $marital_stat will be 'married' or 'single' whenever valid data is submitted, in which case maybe an else that handles an error could be helpful.
what does echo " ($marital_stat) "; produce? Have you tried $_POST ['marital_stat']?
@mike i already put the $marital_stat = ($_POST['marital_stat']); the result should be if if she married and jobless, it will display negative. but now, it display the positive.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.