PHP Functions ?

  • dEfEndEr
  • Newbie
  • Newbie
  • dEfEndEr
  • Posts: 14

Post 3+ Months Ago

I have created a function in php that takes 4 strings passed to it

ie.
Code: [ Select ]
demo_function($var, $mode, $data, $user)
{

}
  1. demo_function($var, $mode, $data, $user)
  2. {
  3. }


My question is, if I was to pass 5 or 6 but not change any of the code that already calls for the first 4 how do I do that ?

If I add 1 or two more, my php errors in the old code, but it's stilly changing all the calling programs and adding ,'','' to them all

HELP.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Cafu
  • Student
  • Student
  • Cafu
  • Posts: 97

Post 3+ Months Ago

I think you can do something like this to give the new variables default values:

Code: [ Select ]
demo_function($var, $mode, $data, $user, $newstring1 = "", newstring2="")
{

}
  1. demo_function($var, $mode, $data, $user, $newstring1 = "", newstring2="")
  2. {
  3. }
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I'm not sure if I'm misunderstanding the question or not, but you can use func_num_args to find out how many arguments have been passed to a function and use func_get_args or func_get_arg to retrieve either an array of all args or an individual argument. Perhaps you could test for the number of arguments that have been passed with func_num_args and if there are additional arguments passed you can retrieve them with one of the other two functions and write code to handle them.
  • dEfEndEr
  • Newbie
  • Newbie
  • dEfEndEr
  • Posts: 14

Post 3+ Months Ago

Thanks.
  • Tannu4u
  • Proficient
  • Proficient
  • User avatar
  • Posts: 480
  • Loc: India

Post 3+ Months Ago

demo_function($var, $mode, $data, $user, $newstring1 = "", newstring2="")
{

}

Yes i fully agree with CAFU These are known as default arguments if u provide value for them when u r calling those functions then that values are used otherwise the default arguments are used.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 71 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.