How To Pass The Optional Parameter To A Function In PHP | CampCodes

How to Pass the Optional Parameter to a Function in PHP

May 31, 2020
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!


Leave a Reply

Your email address will not be published. Required fields are marked *