简体   繁体   English

php-如何将参数传递给函数

[英]php - How to pass parameters to a function

I'm pretty sure this is a very basic question to all of you, but i'm new with php, and i don't really get it... basically i've created a function in which i need to pass two parameters. 我敢肯定,这对所有人来说都是一个非常基本的问题,但是我是php的新手,但我并没有真正了解它...基本上,我已经创建了一个函数,需要在其中传递两个参数。

My functions is this: 我的功能是这样的:

function displayRoomDetails($customerRooms, $test)
{
    foreach ($customerRooms as $room) {
        $test.= $room->name;
    };
}

it is a very basic function, but will do for this example. 这是一个非常基本的功能,但将在此示例中进行。

Now, i'm creating templates to display several information, and i have 3 different layout where i need to display the same info but styled differently, so my approach was: 现在,我正在创建模板以显示多个信息,并且我有3种不同的布局,需要显示相同的信息,但样式不同,所以我的方法是:

template1 .= '<span>';

if (!$customerRooms == "") {
    displayRoomDetails($customerRooms,"template1");
};

template1 .= '</span>';

It should be pretty easy to understand, basically i'm calling the same functions in all the different templates passing as a parameter the template name and trying to append the results to the right template. 它应该很容易理解,基本上我在所有不同的模板中调用相同的函数,将模板名称作为参数传递,并尝试将结果附加到正确的模板上。

The problem i've got is this: According to this example here -> http://www.w3schools.com/php/showphp.asp?filename=demo_function3 我遇到的问题是:根据此处的示例-> http://www.w3schools.com/php/showphp.asp?filename=demo_function3

i should be able to do this exactly like i did, but when i try, when i debug my function, $template doesn't take the passed value as i though it would, but it is still called $test and not $template1... 我应该能够像我一样完全做到这一点,但是当我尝试调试函数时,$ template不会像我那样接受传递的值,但它仍称为$test而不是$template1...

What am i doing wrong? 我究竟做错了什么?

Thanks 谢谢

Try these changes: 尝试以下更改:

function displayRoomDetails($customerRooms, &$test)

And

$template1 .= '<span>';
if ($customerRooms != "") {
  displayRoomDetails($customerRooms, $template1);
};
$template1 .= '</span>';

From what I understand you want to append some text to the template1 variable using the displayRoomDetailsFunction 据我了解,您想使用displayRoomDetailsFunction将一些文本附加到template1变量中

Some things to fix: 要解决的一些问题:

  1. template1 should be $template1 template1应该是$template1
  2. You should be passing the $template1 not the "template1" (ie the variable itself not its name). 您应该传递$template1而不是"template1" (即变量本身而不是它的名称)。
  3. If you want to modify this variable you need to either: 如果要修改此变量,则需要执行以下任一操作:
    • pass it as reference , which you can do by changing the function's declaration to: function displayRoomDetails($customerRooms, &$test) 将其作为引用传递,可以通过将函数的声明更改为: function displayRoomDetails($customerRooms, &$test)
    • return new string from function and assign it to the $template1 by adding return $test; 从函数返回新字符串,并通过添加return $test;将其分配给$template1 return $test; just after your foreach block and changing the call to $template1 .= displayRoomDetails($customerRooms,$template1); 就在您的foreach块之后,并将调用更改为$template1 .= displayRoomDetails($customerRooms,$template1);

Additional note: if $customerRooms is an array, it'd be better to check if it's not empty using count() than !$customerRooms == "" , see @andrew's comment for details 补充说明:如果$customerRooms是一个数组,则最好使用count()来检查它是否不为空,而不是!$customerRooms == "" ,有关详细信息,请参见@andrew的注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM