Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php: If input value is x, y, or z do stuff

Tags:

php

In a html page I have this input:

<input type="text" name="my_input" id="my_input">

Using PHP, if the user enters in the input the words "sun", "moon", "stars", I want the script to do stuff.

Something like this, just don't know how to write it correctly:

if (my_input.value == "sun,moon,stars") {
do stuff
}

Thank you very much!

like image 260
Malasorte Avatar asked Dec 02 '22 15:12

Malasorte


2 Answers

Use in_array()

if( in_array($_POST['my_input'], array("sun", "moon", "stars")) ) {
   //Do stuff
}
like image 148
ʰᵈˑ Avatar answered Dec 04 '22 05:12

ʰᵈˑ


There are a few ways you can achieve this. Some better than others, personally I prefer in_array

in_array

if (in_array($value, array("sun", "moon", "stars"))) {
    // Do something
}

If statement

if ($value == "sun" || $value == "moon" || $value == "stars") {
    // Do something
}

Switch

switch ($value) {
    case "sun":
    case "moon":
    case "stars":
        // Do something
    break;
}

Note there are more ways you can achieve this. The above are just a few.

like image 24
Jonathon Avatar answered Dec 04 '22 05:12

Jonathon