i am getting category name using $_GET['category']
and assigning it to $category like this.
// if $_GET['category'] contains space then create array of categories
if(strpos($_GET['category'],' ') !== false) {
$category = explode (' ',$_GET['category']);
}else{
$category = $_GET['category'];
}
so $category can either be an array or string
and i have dropdown select box on website and would like to show the user searched category by setting selected="selected" to the specific option in dropdown select.
so my question is can i compare string with an array ?, without checking if its an array , like this.
<option value="option1"<?= (isset($category) && ($category == 'option1')) ? ' selected="selected"' : '';?>>option1</option>
<option value="option2"<?= (isset($category) && ($category == 'option2')) ? ' selected="selected"' : '';?>>option2</option>
<option value="option3"<?= (isset($category) && ($category == 'option3')) ? ' selected="selected"' : '';?>>option3</option>
<option value="option4"<?= (isset($category) && ($category == 'option4')) ? ' selected="selected"' : '';?>>option4</option>
or i have to check whether the $category is not an array like this.
<option value="option1"<?= (isset($category) && (!is_array($category)) && ($category == 'option1')) ? ' selected="selected"' : '';?>>option1</option>
<option value="option2"<?= (isset($category) && (!is_array($category)) && ($category == 'option2')) ? ' selected="selected"' : '';?>>option2</option>
<option value="option3"<?= (isset($category) && (!is_array($category)) && ($category == 'option3')) ? ' selected="selected"' : '';?>>option3</option>
<option value="option4"<?= (isset($category) && (!is_array($category)) && ($category == 'option4')) ? ' selected="selected"' : '';?>>option4</option>
$categoryas an array, it would be easier for you. Inelse block, if you will write$category = array($_GET['category']);, then in your HTML code, you can check with only array not with string.