PHP Functions ?

  • dEfEndEr
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Jul 22, 2004
  • Posts: 14
  • Status: Offline

Post July 30th, 2004, 5:53 am

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
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 30th, 2004, 5:53 am

  • Cafu
  • Student
  • Student
  • No Avatar
  • Joined: Jul 15, 2004
  • Posts: 97
  • Status: Offline

Post July 30th, 2004, 9:12 am

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
  • Joined: May 17, 2003
  • Posts: 1121
  • Loc: Boston
  • Status: Offline

Post July 30th, 2004, 9:49 am

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.
Free Programming Resources
  • dEfEndEr
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Jul 22, 2004
  • Posts: 14
  • Status: Offline

Post July 31st, 2004, 4:27 am

Thanks.
  • Tannu4u
  • Proficient
  • Proficient
  • User avatar
  • Joined: Apr 29, 2004
  • Posts: 480
  • Loc: India
  • Status: Offline

Post July 31st, 2004, 6:06 am

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.
Amit
My Blog http://www.amityadav.name

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 176 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
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.