How to Pass the Optional Parameter to a Function in PHP

May 31, 2020
PHP
Optional Parameter in php

Code

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!

 

Facebook Comments

Leave a Reply

Your email address will not be published.