Skip to content
Advertisement

php isset shorthand else return array not working

I have this php code line

$data['viewData']['filter'] = isset($parameters['filter']) ? $parameters['filter'] : array('filter1', 'filter2', 'filter3', 'filter4');

Is something wrong with the isset shorthand because when I print_r($data['viewData']['filter'] with else being active, returns nothing/null/empty.

What could be wrong?

Edit, to add more info:

Code inside the controller:

if (isset($parameters['filter']))
{
    $data['viewData']['filter'] = $parameters['filter'];
}
else
{
    $data['viewData']['filter'] = array('filter1', 'filter2', 'filter3', 'filter4');              
}
$data['viewData']['query']   = $parameters['search'];
$data['viewData']['results'] = $searchResults;
$data['view'] = 'searchResults';
$this->load->view('includes/template', $data);

Code inside view:

prePrint($viewData['apis']);

And I get nothing when the else enters

Codeigniter is used as framework *prePrint is just a custom function that contains <pre> and print_r();

Advertisement

Answer

I don’t really understand what you mean by it returns “nothing/null/empty” since a variable cannot return all 3 of those at once. isset doesn’t check to see if any data exists, it only checks to see if the variable or array index is set. If you want to both make sure it’s set and not empty, try this:

if (!empty($parameters['filter']))
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement