Why are we saying SLUG = FALSE? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Why are we saying SLUG = FALSE? (/showthread.php?tid=1530) |
Why are we saying SLUG = FALSE? - lexxtoronto - 03-18-2015 1. So when retrieving data from db they are saying $slug=FALSE - Why and what does it mean? Also why are we checking if $slug is false? public function get_news($slug = FALSE) { if ($slug === FALSE) { $query = $this->db->get('news'); return $query->result_array(); } $query = $this->db->get_where('news', array('slug' => $slug)); return $query->row_array(); } 2. So if $slug ==== FALSE then it will execute whatever is inside IF curly brackets and then will also execute the $query = $this->db->get_where('news', array('slug' => $slug)) ?? 3. Do I need to use if slug===false? RE: Why are we saying SLUG = FALSE? - mwhitney - 03-18-2015 1. http://php.net/manual/en/functions.arguments.php#functions.arguments.default A function/method may define default values for arguments, in this case, the default value of $slug is FALSE. If you call get_news() without supplying an argument, $slug will be FALSE. 2. http://php.net/manual/en/function.return.php Since the last statement inside the curly braces for the if statement is Code: return $query->result_array(); 3. Yes, unless you change the code outside the curly braces to change the return type and only set the where clause when $slug !== FALSE. For example: PHP Code: public function get_news($slug = FALSE) Since this is probably more confusing than the original code, you're probably better off with what you already have. RE: Why are we saying SLUG = FALSE? - lexxtoronto - 03-18-2015 Thank you. Ah, of course, I overlooked the existence of 'return'. 1. Can I do something like this? Just to retrieve all data. I dont have to use $slug? Code: public function get_news() 2. By saying like this get_news($slug = FALSE) wer are not setting $slug to false? $slug is not boolean, we are just checking if it exists? 3. Im just confused by the use of get_news($slug = FALSE) and then checking if ($slug===FALSE). So if $slug doesnt exist then it will be false because it is the default argument, but if it exists then the query outside of if curly braces will be executed. That's my understanding now. RE: Why are we saying SLUG = FALSE? - CroNiX - 03-18-2015 The default for slug is FALSE, which means if no slug is passed into the method, it will still be FALSE, which makes the code retrieve ALL articles. If a slug is passed in, like http://yoursite/get_news/some-slug, then it will retrieve the INDIVIDUAL article with the slug that equals "some-slug". So if you went to: http://yoursite/get_news it would retrieve all articles because there is no slug provided If you go to: http://yoursite/get_news/some-slug it would retrieve the article with the slug of "some-slug" Kills 2 birds with one stone, so to speak. RE: Why are we saying SLUG = FALSE? - lexxtoronto - 03-18-2015 Yep, thank you, I got that. Im interested why $slug is checked against being FALSE? Its not boolean, we're just checking if it exists, right? RE: Why are we saying SLUG = FALSE? - mwhitney - 03-19-2015 If $slug is passed in, it is expected to be a string which can be used in the where clause of the SQL to only return the item indicated by $slug. If no $slug is passed in, it gets set to FALSE, which is a boolean value (and therefore easy to check against quickly), and unlikely to be passed to the function accidentally. So, when $slug is not passed in (or is FALSE), you retrieve the list of items without using a where clause. Because the check against FALSE is using === (instead of ==), it is checking both the value (FALSE) and the type (boolean), so it will only return the full list if $slug is set to FALSE (which occurs when no $slug is passed to the function). So, if someone calls http://yoursite/get_news/FALSE it should attempt to set the where clause Code: $query = $this->db->get_where('news', array('slug' => 'FALSE')); More information on how PHP handles comparison with different types is at http://php.net/manual/en/types.comparisons.php You could just as easily use NULL instead of FALSE, but most developers have an easier time understanding what is intended by FALSE and how it is handled by comparison operators. Plus, if you do not set a default value, the function should throw an error when you call it without an argument, which would prevent you from using it for both cases. RE: Why are we saying SLUG = FALSE? - lexxtoronto - 04-08-2015 Thank you all! |