Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP function building a default variables? [duplicate]

Tags:

function

php

I am trying to build a function that has 2 default values, but on some occasions I need to supply only the first argument, and others only the second. Is there a way in PHP that I can do this, or is there a way of doing what I want that is considered standard?

Example

function ex($a, $b = null, $c = null) {
    ....
}

ex($a, $b, $c) // WORKS FINE

ex($a) // WORKS FINE

ex($a, $b) // WORKS FINE

ex($a, $c) // WANT VAR C TO BE VAR C IN THE FUNCTION AND NOT VAR B

Thanks

like image 710
Griff Avatar asked Jun 06 '26 18:06

Griff


1 Answers

ex($a, $c) // WANT VAR C TO BE VAR C IN THE FUNCTION AND NOT VAR B

Skip second param:

ex($a, null, $c);

Learn more on official docs

like image 159
Sarfraz Avatar answered Jun 09 '26 07:06

Sarfraz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!