0

I have a variable and its type is string. var_dump() shows:

var_dump() output is below:

string(14)

//this is my code..

$game_cat = "Some text"; // some text mean actually it is one of below Category 1,2,3,4 ...

if ( $game_cat === "Category 1" ) {
        $cid = 1;
}

if ( $game_cat === "Category 2" ) {
        $cid = 2;
}

if ( $game_cat === "Category 3" ) {
        $cid = 0;
}

else{
       $cid = 999999;
}

For example when I change $game_cat to Category 1 like $game_cat = "Category 1"; $cid must 1 but output is 999999.

Why?

7
  • Try using ==, if that works, then the strings might be the same value, but not EXACTLY the same. Notice your string has 14 characters while "Category 1" has 10, 12 including the double quotes Commented Sep 30, 2013 at 15:27
  • I tried it but it is not working Commented Sep 30, 2013 at 15:28
  • For string comparison he should have no difference between == and ===. Double check that your capitalization is the same, and that you aren't appending extra spaces with trim(). Commented Sep 30, 2013 at 15:29
  • print_r the variable and let's see if it's actually what you say it is Commented Sep 30, 2013 at 15:31
  • $game_cat giving its value dynamically but when I do var_dump type and length is the same. Commented Sep 30, 2013 at 15:31

4 Answers 4

3

Use elseif

if ( $game_cat === "Category 1" ) {
        $cid = 1;
}

elseif ( $game_cat === "Category 2" ) {
        $cid = 2;
}

elseif ( $game_cat === "Category 3" ) {
        $cid = 0;
}

else{
       $cid = 999999;
}

What your current code does:

if ( $game_cat === "Category 1" ) {
        $cid = 1; //cid is 1
}

if ( $game_cat === "Category 2" ) {
        $cid = 2; //skipped
}

if ( $game_cat === "Category 3" ) {
        $cid = 0; //skipped
}

else{
       $cid = 999999; //cid == 999999
}
Sign up to request clarification or add additional context in comments.

1 Comment

@hakiko he is correct, you are hitting the old if this and for everything else bug.
1

You're using multiple if-statements, while this should just be one (else-if). So, at first your variable will be 1, but because the last if-statement overwrites it, you'll get the wrong answer.

Do something like:

if ( $game_cat === "Category 1" ) {
        $cid = 1;
}
elseif ( $game_cat === "Category 2" ) {
        $cid = 2;
}
elseif ( $game_cat === "Category 3" ) {
        $cid = 0;
}
else{
       $cid = 999999;
}

For more information about elseif, check http://php.net/manual/en/control-structures.elseif.php :)

Comments

1

Edit: Working demo:

http://phpfiddle.org/main/code/g02-bjj

Try this:

$game_cat = "Category 2";

if ( $game_cat === "Category 1" ) {
        $cid = 1;
}

elseif ( $game_cat === "Category 2" ) {
    $cid = 2;
}

elseif ( $game_cat === "Category 3" ) {
    $cid = 0;
}

else {
   $cid = 999999;
}
  1. Use elseif: http://php.net/manual/en/control-structures.elseif.php
  2. http://www.php.net/manual/en/language.operators.comparison.php

Hope it helps!

1 Comment

@hakiko take a look at the example, it's working: phpfiddle.org/main/code/g02-bjj
0

Because of the last else. Since It's "Category 1" $cid will be set as 1 at the first if condition, however it will be reset because it's not "Category 3".

You should use "elseif" instead of concatenating many "if" in this case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.