Welcome Guest, Not a member yet? Register   Sign In
Optional Function Parameters Within Controllers
#1

[eluser]blackhalobender[/eluser]
I'd like to make a variable passed from a URL optional without getting the missing argument error


Any ideas?
#2

[eluser]xwero[/eluser]
Code:
if(!$this->uri->segment(4))
{
   // not present
}
else
{
  // present
}
#3

[eluser]blackhalobender[/eluser]
THANKS xwero!!!
#4

[eluser]blackhalobender[/eluser]
Opps, actually I'm getting errors. Where can I put that?
#5

[eluser]blackhalobender[/eluser]
It looks like that has to go INTO the function and I need to remove the param from it?
#6

[eluser]Jim OHalloran[/eluser]
Using PHP's optional parameter feature also works....

Code:
function myfunction($param1, $param2 = FALSE) {
  if ($param2 === FALSE) {
    // Do something when param2 isn't supplied.
  }
}

In this example, $param2 will be assigned the boolean false if it wasn't in the URL, but you can easily change it to be whatever you like.

Jim.




Theme © iAndrew 2016 - Forum software by © MyBB