optional variables in php functions

Thread Solved

Join Date: Feb 2008
Posts: 26
Reputation: isotope is an unknown quantity at this point 
Solved Threads: 0
isotope's Avatar
isotope isotope is offline Offline
Light Poster

optional variables in php functions

 
0
  #1
Oct 30th, 2008
Hi everybody, there's a question I would ask about php functions.

I would like to know if it is possible to have optional variable in functions.
Let's say, for instance, that I have:

  1. function my_fun($a,$b,$c){
  2. various stuff
  3. }

In some cases I would recall the function, inputting values for "a","b" and "c", while in some other cases I would just input values for "a" and "b".

In both cases the function is working, but when I input just two values I have the following error shown on the page, before the code is executed:
Warning: Missing argument 3 for my_fun(), called in...

Is there a way to say that $c is optional, in order to avoid the message to be displayed?

Thank you very much,
s
IT goes green
(sustainable informatic development)
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: optional variables in php functions

 
0
  #2
Oct 30th, 2008
to make it optional, you have to set the varible with a value in the function.

ex.

  1. <?php
  2.  
  3. function test( $a,$b,$c='') {
  4. //do what you need
  5. }
  6.  
  7. ?>
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 26
Reputation: isotope is an unknown quantity at this point 
Solved Threads: 0
isotope's Avatar
isotope isotope is offline Offline
Light Poster

Re: optional variables in php functions

 
0
  #3
Oct 30th, 2008
Easy, fast, clean and working.
Thank you very much!
IT goes green
(sustainable informatic development)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum


Views: 894 | Replies: 2
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC