Writing a function in php

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

    I see, you that you want to function which will return something string. You can pass variable as an argument. Things after return statement will be what you get by calling frank()

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

    Or if you want to use global variable in the function you must declared this by a global statement

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

提交回复
热议问题