PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 7, 2022

[FIXED] php isset shorthand else return array not working

 February 07, 2022     codeigniter, isset, php, shorthand     No comments   

Issue

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();


Solution

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']))


Answered By - Francis Lewis
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing