How to make a variable determine: for example, this code does not work for me:

HTML code:

<form action = "http://localhost/PHP/chapt2/switch.php" method="get"> number: <input type="text" name="number"/><br> <input type="submit"/> </form> 

php script:

  <? switch($number) { case 1: echo ("one "); case 2: case 3: echo ("free"); case 4: case 5: echo ("five"); case 6: case 7: echo ("seven"); case 8: case 9: echo ("nine"); break; default: echo ("This isn't number or number is > 9 or < 1"); } ?> 

And it always returns to me default value.

  • 2
    And what is the $ number in the PHP code and where does it come from? If you think that the value of $ number will be automatically substituted from the HTML form, then, praise the Almighty, in your case it is not so :) Read the documentation on $ _GET, $ _POST and other utilities. - user6550
  • one
    I think it will be interesting php.net/manual/ru/security.globals.php - KaZac

3 answers 3

 switch($_GET['number']){ // и т.д. } 

    switch($number) replace with switch($_GET['number'])

      I'm a comic book, for the sake of .... just for the sake of ... it amused me.

      New story.

       Скрипт не узнает переменную switch($number) 

      cursing $ number who are you? Where are you from?

      $ number I $ number I just want to meet and be friends with you correctly!

      Form action comes into play

      Hack you there try to make friends then! I’m tired of sending you data, but you can’t accept them

      $ number as it can not, but I take $ number from you

       <input type="text" name="number"/> 

      No, you do not accept the form action . with which the switch agreed.

      GLOBALS came to the rescue!

      "Hey guys!" Said $ _GET array, "I have news for you! Get your variable from me!"

      But how, asked puzzled $ number ?

      YES just answered the array.

      Put a = sign after you and give me the right value.

       $number=$_GET['number']; 

      switch now and you can work properly with a new friend, who now has a reason to exist as a real variable!

      So we made friends $ _GET $ number switch and form

      ps thank you for your attention

      • one
        Minute addiction on hashcode? :) - mirus
      • along the way yes :) - Artem