CodeIgniter Forums
Why are we saying SLUG = FALSE? - Printable Version

+- CodeIgniter Forums (
+-- Forum: Using CodeIgniter (
+--- Forum: General Help (
+--- 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

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.

Since the last statement inside the curly braces for the if statement is
return $query->result_array();
, nothing outside the curly braces will be executed if $slug === FALSE

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)
$returnMethod 'result_array';
    if (
$slug !== FALSE) {
$returnMethod 'row_array';
$query $this->db->get('news');

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?
public function get_news()

$query = $this->db->get('news');
return $query->result_array();


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
$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

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!