2

I'm passing values on a url to a page on my site. So when a user clicks a link different values will pass to the new page depending on which link they click. I'm using a conditional on the second page to vary the code that gets run depending on the value in $_GET.

Unfortunately, for reasons unknown to me the conditionals don't seem to be working properly. In the if statements I'm using || because there are multiple passed values that need to trigger the same code on the second page (they need to be named differently because they also all trigger unique code).

It seems rather simple, I start with:

if($_GET['id']=('x' || 'y' || 'z')) {
block of code
}elseif($_GET['id']=('a' || 'b' || 'c')) {
block of code
}else{
block of code
}

I have printed out the values of the $_GET['id'] to check that the correct value is being passed from the first page to the second based on the link clicked. This is working properly. Every link I click prints the expected value. The odd thing is I echo'd these values both within the first conditional and above it. It printed in both cases, even when it printed a value that shouldn't have triggered that conditional. Any ideas?

SOFe
  • 7,867
  • 4
  • 33
  • 61
programmingnewb
  • 129
  • 2
  • 9

3 Answers3

3

The syntax you're using for checking if $_GET['id'] is one of the valid values in the set is wrong.

I think you mean to be doing something more like this:

<?php
if (in_array($_GET['id'], array('x', 'y', 'z') { 
    /* block 1*/ 
}
else if (in_array($_GET['id'], array('a', 'b', 'c')) { 
    /* block 2 */ 
}
else { 
    /* block 3 */ 
}

Some further information on the code you did write:

It's passing because the syntax is valid, but not for what you want to do.

Essentially you're saying "Assign $_GET['id'] to the first truthy value of 'a', 'b', or 'c'." The problem there is, all strings (unless they are "0") are evaluated as true.

Furthermore, the single equals symbol (=) assigns the value to the variable and evaluates as true (the assignment was successful). So you're always setting $_GET['id'] to the first string in ('a' || 'b' || 'c'). Therefore, you're assigning $_GET['id'] and it's evaluating to true so the first if condition is always met.

Jim Rubenstein
  • 6,836
  • 4
  • 36
  • 54
  • That worked, awesome. I didn't know about that function. Makes sense since it's an array. I had also tried assigning it to a variable $id but that didn't work. If you can't access the arrays like that, why does it work to assign an array to a variable and use that in a mysql query? – programmingnewb Jan 24 '14 at 22:52
  • You didn't actually create an array, you created an expression that was being evaluated `('a' || 'b' || 'c')` will evaluate to `'a'`. I'm not sure what you're asking, honestly – Jim Rubenstein Jan 25 '14 at 05:48
1
if($_GET['id'] == 'x' || $_GET['id'] == 'y' || $_GET['id'] == 'z') {
    block of code
}elseif($_GET['id'] == 'a' || $_GET['id'] == 'b' || $_GET['id'] == 'c') {
    block of code
}else{
    block of code
}
ccozad
  • 1,119
  • 8
  • 13
1

I think you can also use switch statement. Like the following

$value = 'x'; // $_GET['id']
switch($value) { 
  //matches if one or more case is met
    case 'x':
    case 'y':
    case 'z':
        echo 'i am x, y or z';//here your block code
        break;
    //matches if one or more case is met
    case 'a':
    case 'b':
    case 'c':
        echo 'i am x, y or z';//here your block code

        break;

    default:
        echo 'I have no value';
}
Mubo
  • 1,078
  • 8
  • 16