0

我知道print_r打印数组和对象并echo完成其余的工作。

我的问题是我编码某些东西的结果,并且以某种方式返回的 a 变量function不会打印,echo但它会打印print_ror var_dump。我很容易相信这个结果是我的代码的问题,而不是两者之间的差异echoprint_r因为返回的变量string不是array

所以我的问题如下:function showPreviousDiscipline如果我放入模板,为什么只显示它返回的 HTML 代码print_r?它不应该只通过我调用函数而不需要echoor来显示print_r吗?在一天结束的时候只是文本输出的内容

的HTML

<div class="ldcMainWrap">
<table>
    <tr>
        <td>
            <select multiple="multiple" size="5"> 
            <?php 
// Displays something in the page if is print_r but not if i echo it... or even if i dont put anything...
print_r ($this->showPreviousDiscipline(1)); 
?>
            </select>
        </td>
        <td>
            <select multiple="multiple" size="5"> 
            </select>
        </td>
    <tr>
</table>
<div>

PHP的

public function drawWebsite () {
    $tpl = include "step.one.view.php";
    return $tpl;
}

public function showPreviousDiscipline ( $uID ) {   
    $AllUserDetails = parent::$this->pullUserDetails ( $uID );
    $allDataRaw     = parent::$this->pullDeparmentTableData ();
    $html       = '';

    // Loops through the array $allDataRaw
    foreach ($allDataRaw as $key => $val) { 

        foreach ($val  as $key2 => $val2) {

            //CHECKs if he user has already selected one and if it does it applies a CSS class
            if($key2) {
                if($val2 === $AllUserDetails['rID']) {
                    $html .= '<option value ="'.$val2.'" class="selected">'.$key2.'</option>';
                }else {
                    $html .= '<option value ="'.$val2.'" class="unselected">'.$key2.'</option>';}
            }
        }   
    }   
    $html           .= ''; 
    return $html;
}

测试输出

        <select multiple="multiple" size="5"> 
  <option value="11" class="unselected">dID</option>
    <option value="test1" class="unselected">dName</option>
    <option value="" class="unselected">dDescription</option>
<option value="22" class="selected">dID</option>
    <option value="test2" class="unselected">dName</option>
    <option value="" class="unselected">dDescription</option> 
               </select>
4

1 回答 1

0

从手册(http://php.net/manual/en/function.echo.php)

echo (unlike some other language constructs) does not behave like a function,    
so it cannot always be used in the context of a function.    

所以 echo(fn()) 将返回 NULL。如果要“回显”函数的值,则需要将该值返回到某个本地 var 并回显该值。或者(如您所见)通过“print_r”调用它。

根据您的问题:

 Shouldn't it display only by me calling the function without the need of echo or print_r?

如果您在函数中调用 echo、print 或 print_r(而不是分配返回值),它将起作用。就目前而言,您的函数返回一个值但不输出任何内容。

于 2012-11-15T11:00:19.510 回答