How To Pass The Optional Parameter To A Function In PHP

Optional Parameter in php


For example, you have a class named Text style with the ff functions.

Class Textstyle{
     public function toUpper($string = NULL){
          return strtoupper($string);

To make the parameter optional, we need to define the initial value of the parameter to NULL. This way if we don’t pass anything, it won’t throw an error.

Try accessing our class and don’t pass anything and it won’t throw an error, but if you remove the NULL initial value, it will throw an error.

That ends this tutorial. Happy Coding!


Sharing is caring!

Facebook Comments

Leave a Reply