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

Recommended Answers

All 2 Replies

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
}

?>

Easy, fast, clean and working.
Thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.