Forum Moderators: coopster

Message Too Old, No Replies

Is there something wrong with this?

         

ajs83

2:00 am on Apr 5, 2005 (gmt 0)

10+ Year Member



Sorry for the vague title, but I wasn't sure what to call it and went for the quick way...

Anyways, I have urls similar to

blah.com/file.php?sort=1

Each number is suppose to have it's own content based on the sort number which I figured I'd accomplish by using an if statement. Well I did the code below and it doesn't work past sort=1. I thought it was right but may be missing the obvious and am hoping someone can take a look at it for me.

Here's the code....

$sort = $_GET['sort'];

if (empty($sort)) {
echo "empty";
}elseif($sort=1){
echo "1";
}elseif($sort=2){
echo "2";
}elseif($sort=3){
echo "3";
}

ironik

3:44 am on Apr 5, 2005 (gmt 0)

10+ Year Member



Change the = operator to ==


$sort = $_GET['sort'];

if (empty($sort)) {
echo "empty";
}elseif($sort==1){
echo "1";
}elseif($sort==2){
echo "2";
}elseif($sort==3){
echo "3";
}

The = operator is to assign values to a variable
The == is to test for equality between values

You could also go for a switch statement to make it a little easier to read:


$sort = $_GET['sort'];
if (!empty($sort))
{
switch ($sort)
{
case 1:
echo '1';
break;
case 2:
echo '2';
break;
case 3:
echo '3';
break;
}
} else {
echo 'empty';
}