Welcome Guest, Not a member yet? Register   Sign In
Why are we saying SLUG = FALSE?

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
$query = $this->db->get_where('news', array('slug' => 'FALSE'));
(as it will send the string 'FALSE' to the function). This is usually the desired behavior, because someone could conceivably set a slug to 'FALSE', but a boolean value should only be possible through a direct call from code or as a default value.

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.

Messages In This Thread
Why are we saying SLUG = FALSE? - by lexxtoronto - 03-18-2015, 01:12 PM
RE: Why are we saying SLUG = FALSE? - by mwhitney - 03-18-2015, 01:25 PM
RE: Why are we saying SLUG = FALSE? - by CroNiX - 03-18-2015, 02:27 PM
RE: Why are we saying SLUG = FALSE? - by mwhitney - 03-19-2015, 08:01 AM

Theme © iAndrew 2016 - Forum software by © MyBB