Jump to content

if statement is always showinbg up even if its not true


pinny71691

Recommended Posts

hey im a little bit new to php but i know a lil so i tried this:

<?php

if ($_GET=W9G) {

echo "You have a U2 5.5 Gen. iPod Video";

}

elseif ($_GET=V9K or V9P or V9M or V9R or V9L or V9N or V9Q or V9S

or WU9 or WUA or WUB or WUC or X3N) {

echo "You have a 5.5g iPod Video";

}

else {

echo "You Got a 5th Generation iPod Video";

}

?>

 

oh and it GETS whatever you type into the html code:

<form action = "SN.php" method="get">

Last Three Digits of Your SN#:<br> <input type = "text" name = "count">

<input type="submit" value="Hit Me!">

 

im really sorry if this doesnt make sense but basicallly no matter what i type it always shows You have a U2 5.5 Gen. iPod Video.  i really hope someone can help! thnx so much!!

Link to comment
Share on other sites

OK, several things. Firstly, if you want to ask if something is equal to a string of text, you always use two equals signs, and put the string in quotes. Secondly, you won't get anything from just $_GET, it needs to be $_GET['count'] as that's what you called the input area in the form. You can't just put or or or, it needs to be the full statement again, which is a pain. So your code should read:

 

<?php
if ($_GET['count']=="W9G") {
echo "You have a U2 5.5 Gen. iPod Video";
}
elseif (($_GET['count']=="V9K") or ($_GET['count']=="V9P") or ($_GET['count']=="V9M") or ($_GET['count']=="V9R") or ($_GET['count']=="V9L") or ($_GET['count']=="V9N") or ($_GET['count']=="V9Q") or ($_GET['count']=="V9S") or ($_GET['count']=="WU9") or ($_GET['count']=="WUA") or ($_GET['count']=="WUB") or ($_GET['count']=="WUC") or ($_GET['count']=="X3N")) {
echo "You have a 5.5g iPod Video";
}
else {
echo "You Got a 5th Generation iPod Video";
}
?>

 

But there is an easier way of doing it with a php switch:

 

switch ($_GET['count']) {
case "W9G":
echo "You have a U2 5.5 Gen. iPod Video";
break;
case "V9K":
case "V9P":
case "V9M":
case "V9R":
case "V9L":
case "V9N":
case "V9Q":
case "V9S":
case "WU9":
case "WUA":
case "WUB":
case "WUC":
case "X3N":
echo "You have a 5.5g iPod Video";
break;
default:
echo "You Got a 5th Generation iPod Video";
}

Link to comment
Share on other sites

oh, and elseif above in both mine and your code should be "else if" with a space in between, sorry for not noticing that before.

elseif as one word is valid in PHP.

 

Additionally, the reason it is always becoming true is because a single equal sign is a way of setting equality rather than checking it.  When PHP goes through your script, it sees if ($_GET = W9C) and it thinks, ok, let's try to set $_GET to W9C, which of course will work, and will always return true, because $_GET can always be set to some value just as any other variable can.  Also, as stated above, you need to pick a certain value out of $_GET, you can't just use the entire array.

 

Edit: The return value of a set statement (i.e. $_GET['count'] = W9C) is always the value of the newly set variable.  Example, if W9C is defined as "some test string", then $_GET['count'] = W9C will return "some test string"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.