Writing a function in php

后端 未结 6 2127
庸人自扰
庸人自扰 2020-12-22 10:16

I am trying to work out how to write php functions, so that I don\'t have to keep writing a block of code over and over again, I am having a play around and tried writing th

6条回答
  •  隐瞒了意图╮
    2020-12-22 10:40

    Instead of echoing inside your function you should consider returning a string with it. Also prefer passing a parameter instead of using global scope :

    $greeting = 'Hello';
    
    function frank ($funcGreetings)
    {
        $name   = 'frank';
        $second = 'robson';
        //Concat variable together an return them
        return $funcGreetings.' '.$name.' '.$second;
    }
    
    
    echo frank($greeting);
    

    Finally a variable name start with a letter or an underscore so $2nd is not a valid name.
    See this this link for more info on naming variable

提交回复
热议问题