0

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:

function my_fun($a,$b,$c){
     various stuff
}

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

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by isotope
0

to make it optional, you have to set the varible with a value in the function.

ex.

<?php

function test( $a,$b,$c='') {
//do what you need
}

?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.