• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
optional parameters in functions

I dont know if this is a general PHP question or wether code igniter implements its own solution,but here goes. My pages have a builder function for the page that takes 2 parameters, the page to be loaded, and and any data that may or may not be needed by the page. Ive done quite a lot of asp.net/C# and i know you can specify a parameter as optional with the ? symbol, but is there a similar solution in php?


For an optional parameter, you just set it a default value
function my_function($i_need_this_paramter, $optional_second_parameter = 'a default value') {

Mind, you don't *need* to mention the second parameter there - CI will happily ignore additional parameters that it doesn't know what to do with anyway. And, also, you don't need to set a default value for any of your parameters. If there's no default, the variable is not set (or set to NULL - I forget which, and it's easy enough for you to test).


if (empty($variable)
   // variable is empty no value
    // variable has a value

// Or you can do a single check like this

if ( ! empty($variable)
   // variable has a value


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.