PHP 用户定义函数,(我是新人,需要帮助)
我知道我可以定义这样的函数:
<?php
function say_hello() {
echo "hello world!";
}
say_hello();
?>
...然后只需按名称调用该函数即可在其他地方根据需要多次重用该函数。
我不明白的是“将数据传递给函数内的参数”。我很困惑内部是什么意思。
从我一直在学习的一课中,我有这个功能:
<?php
function say_helloTWO( $word ) {
echo " {$word} hello world a second time ";
}
say_helloTWO( "my name is mike");
?>
这会打印在屏幕上
我的名字是迈克,再次向世界问好
当我使用参数 “my name is mike”
测试该函数时,它会打印到屏幕上,但我不明白如何这有效。变量 $word
没有在任何地方声明,因此,如果我从 echo 中取出 "$word"
,那么只有 "hello world 再次出现
”显示时没有我的名字是 mike
。
这让我相信在这段代码中的某个地方定义了 $word
,对吗?
I understand I can define a function like this:
<?php
function say_hello() {
echo "hello world!";
}
say_hello();
?>
...and then reuse this function elsewhere as many times as I need by just calling the function by name.
What I don't understand is "passing data to arguments within the function". I'm confused what's meant by within.
From one of the lessons I've been studying, I have this function:
<?php
function say_helloTWO( $word ) {
echo " {$word} hello world a second time ";
}
say_helloTWO( "my name is mike");
?>
This prints on screen
my name is mike hello world a second time
When I test the function with the argument "my name is mike"
, it prints to screen, but I don't understand how this works. The variable $word
wasn't declared anywhere, so, if I take out the "$word"
from the echo, then only "hello world a second time
" shows without the my name is mike
.
This leads me to believe that somewhere within this block of code, $word
was defined, is that right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在第二个函数中,
$word
是在函数定义function say_helloTWO( $word )
中声明的。因此该函数需要 1 个参数,该参数将被分配给变量
$word
以便在该函数内使用。因此,当您调用
say_helloTWO("my name is mike");
时,您将 1 个参数(“my name is mike”)传递给函数say_helloTWO
。这 1 个参数被分配给函数定义中的第一个变量,因此在函数中可以作为$word
使用。有道理吗?
In your second function
$word
is being declared in the function definitionfunction say_helloTWO( $word )
.So the function is expecting 1 parameter, which will be assigned to the variable
$word
for use within that function.So when you call
say_helloTWO( "my name is mike");
you are passing 1 parameter ("my name is mike") to the functionsay_helloTWO
. This 1 parameter gets assigned to the 1st variable in the function definition, and is therefore available within the function as$word
.Make sense?
在这种情况下,函数定义据说带有一个参数。
当您在代码中调用该函数时,您在括号中传递的第一个内容将作为第一个参数传递。因此,在您的示例中,字符串“my name is mike”被传递到函数中并分配给变量 $word。
如果我们对此进行扩展并有一个像这样接受两个参数的函数
那么该函数现在将期望将两个参数传递给它。第一个参数将分配给 $word,第二个参数将分配给 $secondArg。
In this case the function definition is said to take one arguments.
When you call the function in your code the first thing you pass in the brackets will be passed as the first argument. Therefore in your example the string
"my name is mike"
is being passed into the function and assigned to the variable $word.If we expand on this and have a function that takes two arguments like this
Then this function would now expect two arguments to be passed into it. The first argument would be assigned to $word and the second argument will be assigned to $secondArg.
您需要阅读以下内容: http://php.net/manual/en/functions.arguments .php
You need to read this: http://php.net/manual/en/functions.arguments.php
在此代码中,变量
$word
的“声明”是在函数声明内进行的。正如您在function say_helloTWO( $word ) {
行中看到的那样,您可以在该行找到$word
。这足以让 PHP 解析器知道 the 的第一个参数在此函数范围内被称为单词。然后,该函数在后面的行中回显该变量echo " {$word} hello world a Second time ";
再次出现$word
时会打印出您设置的内容作为此行say_helloTWO("my name is mike");
上第一个参数的值。好问题。
In this code, the 'declaration' of the variable
$word
is made within the function declaration. As you see on this linefunction say_helloTWO( $word ) {
you can find$word
on that line. This is enough for the PHP parser to know that for the first agument of the is to be known as word within this functions scope. The function then echos out that variable on a later lineecho " {$word} hello world a second time ";
where again$word
is present prints out both what you set as the value of the first argument on this linesay_helloTWO( "my name is mike");
.Good question.
这算作有效的声明。它是在调用函数时作为参数提供时定义的。
因为,在调用该函数时,它们提供“mike”作为参数,$name 变为“mike”。这就是函数的工作原理。
编辑:
这是一个特殊的规则。 $name 不需要声明,因为它会在调用函数时定义。 PHP 理解这一点。如果你不提供参数,它仍然是定义的,它只是空的......我相信。
This counts as a valid declaration. It's defined when supplied as an argument when the function is called.
Because, when calling the function, they supply 'mike' as an argument, $name becomes 'mike'. It's just how functions work.
EDIT:
It's a special rule. $name doesn't need to be declared because it will be defined when the function is called. PHP understands this. If you don't supply an argument, it's still defined, it's just empty... I believe.
它作为参数传递。所以它已经在括号内声明了
It's passed as a parameter. So it was sorta already declared inside the parenthesis
如果您这样做:
function say_helloTWO( $word1, $word2, $word3 )
并调用:
say_helloTWO("a", "b", "c");
那会调用
say_helloTWO
并将变量$word1
设置为"a"
,将$word2
设置为"b"
和$word3
到"c"
这三个变量在该函数运行期间在
say_helloTWO
中定义。举一个实际的例子,假设你有一个函数来求解二次方程,它看起来像
这样 然后,你可以这样调用它,而不是每次都重写代码来求解二次方程:
这会将 $a 设置为 1, $ b 变为 2,$c 变为 3,并将结果存储在 $y 中。
您可以将二次函数视为一种“迷你程序”,独立于程序的其余部分,$a、$b 和 $c 仅适用于二次函数。
If you did:
function say_helloTWO( $word1, $word2, $word3 )
and called:
say_helloTWO("a", "b", "c");
That would call
say_helloTWO
and set the variable$word1
to"a"
,$word2
to"b"
and$word3
to"c"
Those three variables are defined in
say_helloTWO
for the duration that that function is running.To give a practical example, say you had a function to solve quadratic equations, it would look like
Then, instead of rewriting the code to solve a quadratic each time, you could call it like this:
This would set $a to 1, $b to 2 and $c to 3, and store the result in $y.
You can consider the quadratic function to be a sort of "mini-program", independent of the rest of your program, $a, $b and $c only apply within the
quadratic
function.不,$word 没有在功能块中定义,当您调用时,它的值是“我的名字是 mike”。
换句话说,$word 是在您调用函数时定义的:
如果上面的内容对您来说仍然没有多大意义,那么我认为您可能需要回到基础并研究什么是编程世界中的函数。
No, $word is not defined within the function block, it is given the value of "my name is mike" when you mane the call.
In other words, $word is defined when you call the function:
If the above still don't make much sense to you then I think you may need to go back to the basic and study what is a function in programming world.