View Full Version : rewriting array (and code)

03-10-2009, 10:22 AM
I'm using the code below to check if an value is in my array.. It works, however I'm sure it can be written better.. like all the array value's in one line.. I don't know how to do this though..any help would be appreciated.

$arraynaam['0'] = "17";
$arraynaam['1'] = "1";

if ( in_array ("17", $arraynaam) ) {

echo "17 is in the array !";

else {

echo "17 ain't in the array";



03-10-2009, 10:30 AM
Not sure if you're asking about the assignment or determination:

$arraynaam = array('17', '1');

print in_array('17', $arraynaam) ? "17 is in the array !" : "17 ain't in the array";

This is called the ternary operator.
On a side note, though PHP uses loose datatyping and 17 == '17', 17 !== '17'. The quotations indicate that the values are string, not numbers, so it will perform the equality check based on the string radix values instead of the numerical data.