Back to questions
JuniorTheoryCommon
What does the program print?
<?php
$days = ['mon', 'tue', 'wed'];
$pos = array_search('mon', $days);
if ($pos) {
echo 'found at ', $pos;
} else {
echo 'missing';
}
echo ' ', $pos === false ? 'no' : 'yes', "\n";missing yes
- Expecting
if ($pos)to catch a found element: a match under key0returns0, which is false in a condition, so test$pos !== falseinstead. - Expecting
array_searchto returntrueor a position counted from1: it returns the element's key, and the first key of a list is0.
- How would you rewrite the condition so that it works for the first element too?
- What does
array_searchreturn when the value is not in the array?