Php 将参数通过一个函数传递给另一个函数
我正在编写一个PHP Wordpress插件,希望能够调用我在网站其他部分(如页面模板)中定义的函数 我希望能够像这样向函数传递参数:Php 将参数通过一个函数传递给另一个函数,php,wordpress,function,Php,Wordpress,Function,我正在编写一个PHP Wordpress插件,希望能够调用我在网站其他部分(如页面模板)中定义的函数 我希望能够像这样向函数传递参数: // Contained on Page Template to display content $args1 = 'Hello'; $args2 = 'Goodbye'; saySomething( $args1, $args2); // Contained within plugin file function saySomething ($args
// Contained on Page Template to display content
$args1 = 'Hello';
$args2 = 'Goodbye';
saySomething( $args1, $args2);
// Contained within plugin file
function saySomething ($args1, $args2){
//echo $args1 //Test Only
//echo $args2 //Test Only
function sayHello () {
echo $args1;
}
function sayGoodbye () {
echo $args2;
}
}
我已经使用了“include_once”来确保我可以调用插件文件中的函数。然而,由于某些原因,子函数(因为缺少更好的词!)似乎不起作用。我尝试了一些方法,包括在第一个函数中重新定义参数(例如$newargs=$args1)。非常感谢您的任何想法。要在PHP中使用内部函数,您必须明确地传递作用域:
$args1 = 'Hello';
$args2 = 'Goodbye';
$saySomething = function() use ($args1, $args2) {
$sayHello = function() use ($args1) {
echo $args1;
};
$sayGoodbye = function() use ($args2) {
echo $args2;
};
$sayHello();
$sayGoodbye();
};
它不像JavaScript那样自动传递作用域:
let args1 = 'Hello';
let args2 = 'Goodbye';
let saySomething = () => {
let sayHello = () => {
console.log(args1);
};
let sayGoodbye = () => {
console.log(args2);
};
sayHello();
sayGoodbye();
};
要在PHP中使用内部函数,必须显式传递作用域:
$args1 = 'Hello';
$args2 = 'Goodbye';
$saySomething = function() use ($args1, $args2) {
$sayHello = function() use ($args1) {
echo $args1;
};
$sayGoodbye = function() use ($args2) {
echo $args2;
};
$sayHello();
$sayGoodbye();
};
它不像JavaScript那样自动传递作用域:
let args1 = 'Hello';
let args2 = 'Goodbye';
let saySomething = () => {
let sayHello = () => {
console.log(args1);
};
let sayGoodbye = () => {
console.log(args2);
};
sayHello();
sayGoodbye();
};
主要有两件事
sayHello
和saybye
函数,但它们没有被调用sayHello
和saybye
引用变量$args1
和$args2
,但这些变量是sayHello
和saybye
中的变量与saySomething
中的变量不同
function saySomething ($args1, $args2){
function sayHello ($x) { // update the function signature so that it takes an argument
echo $x; // use the given parameter
}
function sayGoodbye ($y) {
echo $y;
}
// call the functions
sayHello($args1);
sayGoodbye($args2);
}
还有一件事:
与它的外观相反,sayHello
和saybye
在与saySomething
相同的范围内可用。它们不仅在该函数的范围内定义,即使它们是在那里编写的。实际上,这与写作是一样的:
function saySomething ($args1, $args2){
sayHello($args1);
sayGoodbye($args2);
}
function sayHello ($x) {
echo $x;
}
function sayGoodbye ($y) {
echo $y;
}
主要有两件事
sayHello
和saybye
函数,但它们没有被调用sayHello
和saybye
引用变量$args1
和$args2
,但这些变量是sayHello
和saybye
中的变量与saySomething
中的变量不同
function saySomething ($args1, $args2){
function sayHello ($x) { // update the function signature so that it takes an argument
echo $x; // use the given parameter
}
function sayGoodbye ($y) {
echo $y;
}
// call the functions
sayHello($args1);
sayGoodbye($args2);
}
还有一件事:
与它的外观相反,sayHello
和saybye
在与saySomething
相同的范围内可用。它们不仅在该函数的范围内定义,即使它们是在那里编写的。实际上,这与写作是一样的:
function saySomething ($args1, $args2){
sayHello($args1);
sayGoodbye($args2);
}
function sayHello ($x) {
echo $x;
}
function sayGoodbye ($y) {
echo $y;
}
您没有在其中的任何地方调用“子函数”。而且,
$args1
和$args2
没有在其范围内定义。类和方法如何?没有“子函数”这样的东西,只是在另一个函数中定义的函数;但它们仍然是在全局范围内创建的,标准函数范围规则仍然适用于anks@Don't Panic。这就是我所做的。你觉得怎么样<代码>函数saySomething($args1,$args2){$args3=$args1;$args4=$args2;echo$args3;echo$args4;函数sayHello($args3){echo$args3;}saygoye($args4){@Hackerman-我认为类/方法绝对是更好的方法。最好去了解他们现在是什么;-)您没有在其中的任何地方调用“子函数”。而且,$args1
和$args2
没有在其范围内定义。类和方法如何?没有“子函数”这样的东西,只是在另一个函数中定义的函数;但它们仍然是在全局范围内创建的,标准函数范围规则仍然适用于anks@Don't Panic。这就是我所做的。你觉得怎么样<代码>函数saySomething($args1,$args2){$args3=$args1;$args4=$args2;echo$args3;echo$args4;函数sayHello($args3){echo$args3;}saygoye($args4){@Hackerman-我认为类/方法绝对是更好的方法。最好去了解他们现在是什么;-)谢谢,不要惊慌。这是辉煌的作品非常好。非常感谢。还感谢您对“sayHello”和“say再见”函数实际上是在“saySomething”函数之外定义的解释。这真的很有帮助。谢谢,不要惊慌。这是辉煌的作品非常好。非常感谢。还感谢您对“sayHello”和“say再见”函数实际上是在“saySomething”函数之外定义的解释。这真的很有帮助,太好了。因此,我认为第二个答案略高于我的技能水平,但为了将来读者的利益,您可以这样定义函数$sayHello=function(),如果您希望这些函数是匿名函数,并且只能在“saySomething”函数中使用,请使用($args1)
,太好了。因此,我认为第二个答案比我的技能水平高了一点,但为了将来读者的利益,如果您希望函数是匿名函数,并且只能在“sayshooth”函数中使用,您可以这样定义函数$sayHello=function()
?