Chris J
Chris J

Reputation: 562

PHP - function name($variable = FALSE) - purpose of FALSE declaration - example from Codeigniter

This is probably quite a simply question but it's something I have never come across until recently.

I was beginning to research PHP frameworks and delved into the tutorial on Codeigniter.

When building the dynamic news section pages, the following method is defined:

public function get_news($slug = FALSE) {

in news_model.php. The method then goes on to check if ($slug === FALSE) to return all the database rows or, if not, get the new item corresponding to the passed $slug.

Can someone tell me what ($slug = FALSE) does when defining the method? Excluding it returns a missing argument warning.

The full method is:

public function get_news($slug) {
    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();
}

I realise that this isn't going to be specific to Codeigniter but I thought I'd use the example in the CI tutorial to explain my confusion.

Thanks.

Upvotes: 1

Views: 6679

Answers (6)

May Noppadol
May Noppadol

Reputation: 648

<?php
echo "With no arguments: ";
get_news();

echo "With a slug (abc): ";
get_news('abc');

echo "No arguments No define: ";
get_news_no_false();

function get_news($slug = FALSE) {
    var_dump($slug);
}
function get_news_no_false($slug) {
    var_dump($slug);
}
?>

if you not define function get_news_no_false($slug) no set

$slug = FALSE

you will get error

<?php
echo "With no arguments: ";
get_news();

echo "With a slug (abc): ";
get_news('abc');

echo "No arguments No define: ";
get_news_no_false();

function get_news($slug = FALSE) {
    var_dump($slug);
}
function get_news_no_false($slug = FALSE) {
    var_dump($slug);
}
?>

if you define $slug = FALSE it will return false automatic.

Upvotes: 0

Bailey Parker
Bailey Parker

Reputation: 15905

PHP calls these default arguments, and they are a way of providing a value for a variable that is not passed to the function when it is called. So given your function (I made it a function instead of a method for the purposes of the demonstration):

function get_news($slug = FALSE) {
    var_dump($slug);
}

echo "With no arguments: ";
get_news();

echo "With a slug (abc): ";
get_news('abc');

(Codepad Demo)

When no value for $slug is passed into get_news(), it assumes the default value in the function's signature, false. However, if a value is provided for $slug when get_news() is called, then within the function, $slug will be this value ('abc' in the example).

This can be particularly useful when specifying arguments that aren't often needed. Take care in carefully reading the documentation though because only certain values can be specified as a default. Those are: scalars (booleans, strings, integers, and floats), arrays, and NULL. You cannot define a parameters default to be a non-scalar such as an object, object method, or object property, function call, or constant (basically anything non-scalar or that is an expression).

Ones that work:

function test($a = false) {}

function test($a = 'default') {}

function test($a = 1) {}

function test($a = array()) {}

function test($a = SOME_CONST) {}

function test($a = SomeClass::someConstant) {}

(Codepad Demo)

Ones that will not work:

function test($a = someFunc()) {}

function test($a = new stdClass()) {}

function test($a = SomeClass::staticMethod()) {}

function test($a = 1 + 1) {} // this includes all expressions (and string concatenation)

For the ones that don't, a common paradigm is to make the parameter default to NULL and then assign the variable a default value within the function if its value is NULL. Example:

function getDefault() {
    return 'some dynamic default';
}

function test($a = NULL) {
    if($a === NULL) {
        $a = getDefault();
    }

    var_dump($a);
}

test();    // string(20) "some dynamic default"
test('a'); // string(1) "a"

(Codepad Demo)

Upvotes: 2

Daryl Gill
Daryl Gill

Reputation: 5524

public function FUnction_Name ($Var = false){ }

This is defining a default it not specified in the function call... Example:

class Test { 
  public function Return_Test ($Variable = false){
    if ($Variable === false){
      return false; 
    }
    return true;


  }

} 

Then Calling:

$Class = new Test();

if ($Class->Return_Test() === false){
  echo "Returned False";
}else{
  echo "Hasn't Returned false";
}

This will output returned false, because no param has been specified, so the function will treat the expected variable as false.

But:

if ($Class->Return_Test("Input") !== false){
  echo "Returned False"; 
}else{
  echo "Hasn't returned false"; 
}

Because A String is passed into the function, the expected variable is treated as a string, and not as the default. so the output will be:

Hasn't returned false

Upvotes: 0

AlexP
AlexP

Reputation: 9857

"Can someone tell me what ($slug = FALSE) does when defining the method?"

The $slug = false means that it is an optional argument, which will default to false if not provided.

Removing the = false will then cause that argument to be required. Hence your error.

Upvotes: 0

Anigel
Anigel

Reputation: 3437

public function get_news($slug = FALSE) {

What this says is that if the function is called without passing a variable then use false as the value for $slug

Upvotes: 4

Jessica
Jessica

Reputation: 7005

It gives it a default value so you don't have to give one when you call the function. It allows you to do get_news() instead of always doing get_news(FALSE) and only give the argument when you want something other than the default, ie: get_news(TRUE)

ETA: In this case the argument would likely be FALSE means no slug, and a string is a slug. So it would be get_news('category').

Upvotes: 1

Related Questions