Writing a function in php

独自空忆成欢 提交于 2020-01-10 06:09:38

问题


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 this, but it doesnt work for some reason, can anyone tell me where I am going wrong ?.

<?php
$greeting 'Hello';

function frank ()
{
$name = 'frank';
$2nd = 'robson';
echo $greeting;
echo $name;
echo $2nd;
}


echo frank()
?>

回答1:


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




回答2:


where I am going wrong ?.

Several places.

but it doesnt work for some reason

That's not an error message - PHP will give you meaningful messages about stuff it doesn't understand if you configure it properly.

If you are getting an error message then you should have included it in your question.

<?php
$greeting 'Hello';

Here's the first error - which will cause a parse failure. There should be an assignment operator in there e.g.

$greeting = 'Hello';

The next error is here:

$2nd = 'robson';

Variables names must begin with an underscore or letter - not a number

echo frank()

2 errors here.

The statement is not terminated with a ; - it may still parse OK but is very messy.

Also the frank function doesn't return a value to echo.




回答3:


You have to return the string as you are using your function with echo

So, do something like this

//this is inside frank
$ret = $greeting.$name.$2nd;
return $ret;

Second, $greeting is a variable declared outside the function definition. So, you should not use it inside the function definition.

You function will now look like this

function frank ()
{
$greeting = 'hello';
$name = 'frank';
$2nd = 'robson';
$ret = $greeting.$name.$2nd;
return $ret;
}

Also, there is an error in the first line. $greeting 'Hello' is incorrect. It should be $greeting = 'Hello' (You are missing the = sign).




回答4:


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);



回答5:


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 );  



回答6:


<?php
$greeting = 'Hello';

function frank ()
{
  global $greeting;
  $name = 'frank';
  $2nd = 'robson';
  echo $greeting;
  echo $name;
  echo $2nd;
}


frank();
?>

You need to SET the variable and you need SEMICOLONS.



来源:https://stackoverflow.com/questions/8786428/writing-a-function-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!