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()
- https://www.php.net/manual/en/function.in-array.php
- https://www.w3schools.com/php/func_array_in_array.asp
strtolower()
- https://www.php.net/manual/en/function.strtolower.php
- https://www.w3schools.com/pHP/func_string_strtolower.asp
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