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

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

问题描述:

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.

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:

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

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...

What am i doing wrong?

Thanks

我很确定这对你们所有人来说都是一个非常基本的问题,但我是php新手, 我真的没有得到它... 基本上我已经创建了一个函数,我需要传递两个参数。 p>

我的函数是这样的: p>

  function displayRoomDetails($ customerRooms,$ test)
 {
 foreach($ customerRooms as $ room){
 $ test。= $ room-&gt; name; 
}; 
  } 
  code>  pre> 
 
 

这是一个非常基本的功能,但是这个例子也适用。 p>

现在,我正在创建模板 显示几个信息,我有3种不同的布局,我需要显示相同的信息,但风格不同,所以我的方法是: p>

  template1。='&lt; span&gt;  '; 
 
if(!$ customerRooms ==“”){
 displayRoomDetails($ customerRooms,“template1”); 
}; 
 
 
mpmplate1。='&lt; / span&gt;'; 
  代码>  pre> 
 
 

它应该很容易理解,基本上我在所有传递的不同模板中调用相同的函数 一个参数模板名称,并尝试将结果附加到正确的模板。 p>

我遇到的问题是: 根据这个例子这里 - &gt; http://www.w3schools.com/php/showphp.asp?filename=demo_function3 a > p>

我应该能够像我一样完成此操作,但是当我尝试时,当我调试我的函数时,$ template不会像我那样接受传递的值, 但它仍然被称为 $ test code>而不是 $ template1 ... code> p>

我做错了什么? p> \ n

谢谢 p> div>

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

Some things to fix:

  1. template1 should be $template1
  2. You should be passing the $template1 not the "template1" (i.e. the variable itself not its name).
  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)
    • return new string from function and assign it to the $template1 by adding return $test; just after your foreach block and changing the call to $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