0

the current page is getting the variables item and code from query string,so according to my code it should go into any of the first three conditions... but its going into the last else conditon.. while echoing the values $a and $i, i am getting 2 and A-1-1 respectively.

$a=$_GET['code']; $i=$_GET['item']; if($a==1 && $i!='') { header("location:http//:www.abc.com"); } else if($a==2 && $i!='') { header("location:http://www.xyz.com"); } else if($a==3 && $i!='') { header("location:http://www.xpqr.com"); } else if($a==1) { header("location: http://www.a1bc.com"); } else if($a==2) { header("location:http://www.x1yz.com"); } else if($a==3) { header("location:http://www.x1pqr.com"); } else { echo "ERROR"; } 

can someone help me find the issue why the if else not working in the expected manner.

8
  • 1
    You're assigning $a = 1 instead of comparing $a == 1. You can use Yoda conditions to avoid this problem: 1 == $a. 1 = $a will throw an error. Commented Aug 14, 2013 at 7:00
  • Sorry that was an error while posting..!still the problem persist.. Commented Aug 14, 2013 at 7:02
  • user "<>" to compare instead of "!=" Commented Aug 14, 2013 at 7:03
  • @MahavirMunot.. done that... but itsn't working.. Commented Aug 14, 2013 at 7:08
  • Mahavir Munot is just trolling. <> is the same as != as descibed here. I wish I had a minus on that answer... Commented Aug 14, 2013 at 7:10

5 Answers 5

1

In conditions you are writing

if($a=1 && $i!='') // "=" is assignment operator 

it should bt

if($a==1 && $i!='') 
Sign up to request clarification or add additional context in comments.

Comments

0

you need to use the equality operator: == double equal

if($a==1 && $i!='') 

single equal is the assignment operator.

Comments

0

You are using assignment operator instead of a conditional operator. For the above code the value of a will always be 1 because of the following line of code :

if($a=1 && $i!='') 

= is an assignment operator where as == is a conditional operator.

Use == in all your if condition.

Hope this will help.

Comments

0

Use == in all your if condition. And your header codes

header("location: http:www.abc.com"); 

Should be like this

header("location: http://www.abc.com"); // you are missing '//' in every header 

Comments

0
 $a=$_GET['code']; $i=$_GET['item']; if($a==1 && !$i) { header("location:http//:www.abc.com"); } else if($a==2 && !$i) { header("location:http://www.xyz.com"); } else if($a==3 && !$i) { header("location:http://www.xpqr.com"); } else if($a==1) { header("location: http://www.a1bc.com"); } else if($a==2) { header("location:http://www.x1yz.com"); } else if($a==3) { header("location:http://www.x1pqr.com"); } else { echo "ERROR"; } 

use !$i it sets to false.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.