Hey guys here is my php:
$x = array("one", "two", "three");
foreach ($x as $value)
{
if ($value != 'one' && $value != 'two')
{
echo $value . "<br />";
}
}
This echo's only the word three. I had to use $value != 'one' && $value != 'two' to make this happen and I was wondering if I could consolidate this into something like this:
if ($value != 'one, two')
That doesn't work so I was wondering if you guys could provide some help.
if (!in_array($value, array('one', 'two')))
echo $value;
if ($value == 'three')
maybe this?
You could create a switch statement to make it look prettier, but that's as far as you can go.
switch ($i) {
case "one":
case "two":
case "three":
echo $value . '<br/>';
break;
}
if you need to print or do certain operation when value == 'three' then only look for value =='three'
foreach ($x as $value)
{
if($value === "three")
echo $value;
}
also you can take a look at using === operator to compare strings. since == uses type juggling while === enforces same type (2 strings, 2 int no mixing) link: http://www.php.net/manual/en/language.operators.comparison.php
Skip the foreach altogether:
$x=array("one","two","three");
$exclude=array("one","two");
print_r(array_diff($x, $exclude));