-4

I have this PHP code and trying to get the value of the variable language from the url parameters when specified link is clicked.

How do I do this? The code below only gets the value of Java in the first statement and not the values in the elseif statements.

if (isset($_GET['language'])) {

    if ($_GET['language'] = 'Java') {
        $q = 'title:(Java+Developer)';
    }
    elseif ($_GET['language'] = 'PHP') {
        $q = 'title:(PHP+Developer)';
    }
    elseif ($_GET['language'] = 'JavaScript') {
        $q = 'title:(JavaScript+Developer)';
    }
}

Links:

      <li><a href="programming/?language=Java">Java</a></li>
      <li><a href="programming/?language=PHP">PHP</a></li>
      <li><a href="programming/?language=JavaScript">JavaScript</a></li>
Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
Mike
  • 99
  • 1
  • 12

7 Answers7

3

There's a difference between = and ==. One is for assigning, the other for comparing.

In your first if() statement, you're assigning the value Java to $_GET['language'], which will evaluate to Java. This will then be true.

Change the single = in the comparisons to ==, and you should be all set.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
1

You are using a single equals sign. This is an assignment (as you do when defining a variable).

You need to use double equals signs ==. This is how you test equality.

$_GET['language'] == SOME_STRING

In addition, I would recommend using a switch statement instead of multiple if statements:

if ( isset( $_GET[ 'language' ] ) ) {
  switch( $_GET[ 'language' ] ){
    case "Java":
      $q = 'title:(Java+Developer)';
    break;
    case "PHP":
      $q = 'title:(PHP+Developer)';
    break;
    case "JavaScript":
      $q = 'title:(JavaScript+Developer)';
    break;  
  }
}

Using a switch statement will make this code much easier to maintain and also to add extra conditions (other languages).

Lix
  • 47,311
  • 12
  • 103
  • 131
  • 1
    Would it be better to use "===" to also check the data type that is being received or is it that only strings are passed? – MixedVeg Jul 17 '14 at 10:37
  • @KartikA - I always promote explicit code - so using a strict equality operator would be a good idea. However, with my switch case example, it is not needed. – Lix Jul 17 '14 at 10:38
  • Hey sorry just saw your code, in your code it shall not be necessary I agree. – MixedVeg Jul 17 '14 at 10:44
0

Check the equal sign! Replace "=" with "=="

llullulluis
  • 3,472
  • 3
  • 27
  • 30
0

try with this code

if (isset($_GET['language'])) {

    if ($_GET['language'] == 'Java') {
    $q = 'title:(Java+Developer)';
    }
    elseif ($_GET['language'] == 'PHP') {
    $q = 'title:(PHP+Developer)';
    }
    elseif ($_GET['language'] == 'JavaScript') {
    $q = 'title:(JavaScript+Developer)';
    }
}
karan
  • 164
  • 10
0

You are assigning value Java to $_GET["language"] which always returns true. You must compare the value of $_GET["language"] against a string. Compare using == or === operator.

if ($_GET["language"] === "Java") { 
  /* here be dragons */ 
}

It is also good habit to use Yoda Conditions to catch this kind of errors.

if ("Java" === $_GET["language"]) { 
  /* here be dragons */ 
}
Mika Tuupola
  • 19,877
  • 5
  • 42
  • 49
0

You are using "=" here which is for assigning the variable some value. Have a look at the use of these:

  1. "=" is used for assigning a value to some variable.
  2. "==" is used for comparison (regardless of the data type). For example if

$var =1 and we evaluate a condition

if($var == TRUE )

The result will be bool True because TRUE is 1 and FALSE is 0 always.

3. "===" comparison based on datatype also. The above condition will evaluate to Bool False because the data types are different.

Community
  • 1
  • 1
MixedVeg
  • 319
  • 2
  • 15
-2

You should use == instead of =.

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
Simon Guldstrand
  • 488
  • 1
  • 5
  • 24