Search code examples
phpphp-8

Compile Error: Cannot use positional argument after named argument


/**
 * @param $name
 * @return Response
 * @Route ("/afficheN/{name}",name="afficheN")
 */
public function afficheName($name){
    return $this->render(view:'student/affiche.html.twig',
        ['n' => $name]);
}

Solution

  • Php 8 added a new feature called named arguments, it allows you to pass input data into a function, based on their argument name instead of the argument order.

    It is possible to combine named and ordered arguments, but only if the ordered arguments came first.

    To make you code work, you have two options

    1. Use named arguments for all arguments:
        return $this->render(view:'student/affiche.html.twig',
           parameters:['n' => $name]);
    
    1. Do not use named arguments at all:
        return $this->render('student/affiche.html.twig',
            ['n' => $name]);
    

    Here is a link where you can learn more about named arguments

    You can also check the php documentation