用PHP编写函数 [英] Writing a function in php
问题描述
我正在尝试找出如何编写php函数的方法,这样我就不必一遍又一遍地编写代码块,我正在玩耍并尝试编写此代码,但这不适用于出于某种原因,谁能告诉我我要去哪里错了?.
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()
?>
推荐答案
与其在函数内部回显,不如考虑返回一个字符串.还更喜欢传递参数而不是使用全局范围:
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);
最后,变量名以字母或下划线开头,因此$2nd
不是有效的名称.
请参阅此此链接以获取有关命名变量的更多信息
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
这篇关于用PHP编写函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!