I know the variable value, but the switch only goes to the default

Asked

Viewed 305 times

7

After a search, I get the following result:

           // O resultado desta linha é I ou II ou III;
           echo $subsecao;

           // O resultado desta linha é string;
           echo gettype($subsecao);

           // Faço um switch para atribuir um caso, mas, retorna apenas o default;

          switch ($subsecao){
            case "I":
              $subsecao = "1";
              break;
            case "II":
              $subsecao = "2";
              break;
            case "III":
              $subsecao = "3";
              break;    
           default:
              $subsecao = "Não encontrou";
          }

           // O resultado desta linha é "Não encontrou";
           echo $subsecao;

           // O resultado desta linha é string;
           echo gettype($subsecao);

Why the switch does not work with this scenario?

  • 1

    Makes a echo "::$subsecao::"; and see if there’s no extra room.

  • 1

    See here at work: http://ideone.com/VhdN7G

  • At this value echo "::$subsecao::"; gave this result ::I :: in this echo $subsecao + "|"; gave this 0

  • 1

    You’re passing an extra space there after I You see? The problem is there.

  • 1

    Because "I " is different from "I".

  • 2

    For checks like this, use the Trim on the variable before switching.

  • It’s bone, programming is a lot of show, but it’s bone. :D. But I used this line $subsecao = preg_replace('/ s+/','$arr[1]);

  • He tested after that to see if he’s really alone I? When something doesn’t work always test the inputs and the outputs and if all is well then check the code.

  • Before the switch, it is now showing: ::I::, , but after the switch, it looks like this: ::

  • If it no longer has space it has to work. Experiment with the II. Is that an I of Equals or is it a lizard l?

  • 1

    try switch (trim($subsecao))

  • So @Alêmoraes worked?

  • I edited the question and put at the end the way I did to get to this variable, but still, it didn’t work.

  • 1

    (Trim(strip_tags($subsection)))

Show 9 more comments

2 answers

10

I have tested and works your code well as you can see in this example so the problem has to be in the input which is passed on to $subsecao;.

Do echo "::$subsecao::"; to check whether the input that is correct.

Note: As said and very well @Novok can use the function trim to remove the spaces trim($subsecao);

  • The result looks like this Section I echo"::$subsecao::"; ::I:: echo $subsecao + "|"; 0 After switch echo"::$subsecao:"; ::

  • gettype before and after switch is string

  • @Alêmoraes You are bringing the value of a GET?

  • I edited the question and put at the end the way I did to get to this variable, but still, it didn’t work.

  • 1

    Worked with (Trim(strip_tags($subsecao)))

3


Try to use:

(trim(strip_tags($subsecao)))

This code removes all HTML tags and removes all left and right spaces from the function return, Obs: the use of strip_tags in HTML BADLY formatted can remove more or less text than it should take, there is an unofficial implementation on this Link which is more efficient.

  • It worked!!! Ohhhhhhhhh

  • @Be careful you didn’t solve the problem, just took a walk in it. The problem is really what you get there, which shouldn’t come with spaces or HTML tags.

  • 1

    @Alêmoraes, OK then signal that the answer has been found

  • How do I signal? I changed the question and the title, you’re right?

  • Green arrow on the side of this response and in the triangle

  • @Alêmoraes No, the right is to put seen next to the answer and has the up arrow to give positive point.

  • 1

    Did it right now huh? Thanks for your help on everything.

  • @Alêmoraes did yes.

  • Ricardo, you could tell me why you have to use it (Trim(strip_tags($subsecao)))?

  • @Alêmoraes, the function strip_tags aims to remove any HTML tag that may have come along with the form values (check XSS on the internet) and the use of trim is to remove the spaces on the left and right side of the string that came as a form value.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.