[英]How can I create a simple function?
Why is this working... 为什么这行得通...
$animal='';
$animal .= "You edited this animal: ";
if ($data['horse'] != $horse){
$animal .= 'Name from "'.$data['horse'].'" into "'.$horse.'"';
}
$sql = "INSERT INTO animal (animal,id) values(?,?)";
$q = $pdo->prepare($sql);
$q->execute(array($animal,$id));
(Result: "You edited this animal: Mustang into Marwari") (结果:“您编辑了此动物:野马成Marwari”)
...but this... ...但是这个...
$animal='';
$animal .= "You edited this animal: ";
function animal_func($label, $orig, $edit) {
if ($orig != $edit){
$animal .= $label.' from "'.$orig.'" into "'.$edit.'"';
}
}
animal_func("Name",$data['horse'],$horse);
$sql = "INSERT INTO animal (animal,id) values(?,?)";
$q = $pdo->prepare($sql);
$q->execute(array($animal,$id));
(Result: "You edited this animal: ") (结果:“您编辑了此动物:”)
... is not working ... 不管用
Put this animal into function 使这种动物起作用
function animal_func($label, $orig, $edit) {
$animal='';
$animal .= "You edited this animal: ";
if ($orig != $edit){
$animal .= $label.' from "'.$orig.'" into "'.$edit.'"';
}
}
animal_func("Name",$data['horse'],$horse);
or 要么
Declare 宣布
global $animal='';
$animal .= "You edited this animal: ";
Because of the scope of the $animal variable. 由于$ animal变量的范围。 http://php.net/manual/en/language.variables.scope.php
http://php.net/manual/zh/language.variables.scope.php
You need to pass an return the $animal var : /* if you need this only once, outside is better */ $animal .= "You edited this animal: "; 您需要传递一个返回值$ animal var:/ *如果只需要一次,外面比较好* / $ animal。=“您编辑了这个动物:”;
function animal_func($label, $orig, $edit, $animal) {
if ($orig != $edit){
$animal .= $label.' from "'.$orig.'" into "'.$edit.'"';
return $animal;
}
}
$animal = animal_func("Name",$data['horse'],$horse,$animal);
You can also do that, perhaps more readable : 您也可以这样做,可能更具可读性:
function animal_func($label, $orig, $edit, $animal) {
if ($orig != $edit) {
$animal .= $label.' from "'.$orig.'" into "'.$edit.'"';
return $animal;
}
}
$animal = "You edited this animal: ";
$animal = animal_func("Name",$data['horse'],$horse,$animal);
After another read (this is better): 再读一遍(这更好):
function animal_func($label, $orig, $edit) {
if ($orig != $edit) {
return $label.' from "'.$orig.'" into "'.$edit.'"';
}
}
$animal = "You edited this animal: ".animal_func("Name",$data['horse'],$horse);
Because of the scope of your $animal
inside your animal_func()
is limited to that function only. 由于
$animal
的作用范围内, animal_func()
仅限于该函数。
What you need to do is pass $animal
as reference variable in your animal_func()
. 您需要做的是在您的
animal_func()
传递$animal
作为参考变量。 So your function should look like: 因此,您的函数应如下所示:
$animal='';
$animal .= "You edited this animal: ";
function animal_func($label, $orig, $edit, &$animal) {
if ($orig != $edit){
$animal .= $label.' from "'.$orig.'" into "'.$edit.'"';
}
}
animal_func("Name",$data['horse'],$horse, $animal);
See PHP manuals for variable scope and variable pass by reference on this link . 有关此链接上的变量范围和变量引用,请参见PHP手册 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.