Writing a function in php

后端 未结 6 2144
庸人自扰
庸人自扰 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:27

    Firstly, you need to pass in the $greeting variable for it to be in the functions scope - see the PHP manual for variable scope. It is best avoid using globals as they are harder to debug and test.

    Secondly, you have missed out the assignment for the $greeting variable.

    Thirdly, you have forgotten to return the value from the function so it can be echod out. See the PHP manual for functions.

    Fourth, $2nd is not a valid variable name. Variables must start with a letter. See the relevant manual page.

    $greeting = 'Hello';
    
    function frank ($greeting) {
        $return = '';
        $name = 'frank';
        $second = 'robson';
        $return .= $greeting;
        $return .= $name;
        $return .= $second;
        return $return;
    }
    
    echo frank($greeting);
    

提交回复
热议问题