Required parameter $originalDate follows optional parameter $format

Created at 03-Mar-2022 , By samar

Required parameter $originalDate follows optional parameter $format

In this session, we are going to try to solve the "Required parameter $originalDate follows optional parameter $format" puzzle by using the computer language.

While declaring a function or a method, adding a required parameter after optional parameters is deprecated since PHP 8.0 version. You have to use the required parameter (first parameter) before the optional parameters while defining the function.
  • PHP 8.0: Deprecate required parameters after optional parameters in function/method signatures

    //Function definition with error
    function changedateFormat($format = 'd-m-Y', $originalDate){
        return date($format, strtotime($originalDate)); 
    }
    
    //Sample code with solution which helps you to avoid error Required parameter follows optional parameter.
    function changedateFormat($originalDate, $format = 'd-m-Y'){
        return date($format, strtotime($originalDate)); 
    }
    function changedateFormat(string $format, $originalDate){
        return date($format, strtotime($originalDate)); 
    }
    function changedateFormat(string $format = null, $originalDate){
        return date($format, strtotime($originalDate)); 
    }
    

    PHP documentation already explains that having required parameters after optional parameters is incorrect. There was no deprecation notice until the PHP 8.0 version. You can change the order of parameters and use the (type of parameter) string before the optional parameter or you can also pass the type of parameter with null value to solve the error.

Back to code snippet queries related php

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.