在函数外部使用变量

时间:2014-10-01 07:59:44

标签: php function

我有以下(汇总)代码

define pauls_code($a, $b)
{
$c = $a + $b;
echo $a;
echo $b;
}
pauls_code(1,2);
echo $c;  //  how do I get this to print outside of the function?   I have tried everything. I am hoping to see 123

2 个答案:

答案 0 :(得分:3)

使您的功能如下并返回$c

function pauls_code($a, $b){
$c = $a + $b;
return $c;
}
echo pauls_code(1,2);

答案 1 :(得分:0)

要在php中声明一个函数,您只需将function pauls_code($param1, $param2)放入define

即可

您无法在该功能之外访问$c,请详细了解范围:http://php.net/manual/en/language.variables.scope.php