PHP – Test Variable Against an Array with in_array()

The in_array() function allows you to check if a value is stored with an Array. This can be used for things such as creating an array of which states a company ships products to.

in_array()

strtolower()

inArray.php

<?php

$state = strtolower($_POST['state']);

$shipTo = array("maryland","delaware","virginia","maine","vermont");

if (in_array($state,$shipTo)){
    echo "We Do Ship to ".$state;
} else {
    echo "We Do NOT Ship to ".$state;
}

?>

<form action="inArray.php" method="post">
    Your State: <input type="text" name="state">
    <input type="submit">
</form>

Be the first to comment

Leave a Reply