2012-10-18 23 views
0

我想与数组一起回显常规文本。我立足下面的代码从这里找到答案,但它不工作:Echo arrays with regular text回声常规文本和数组

<?php 
$val1 = Yes; 
if (($row->relation) == ($val1)) { 
echo "<p><b>Applicant\'s Name:</b> {$row['relation_name']} | <b>Business:</b> {$row['relation_business']}</p>"; 
} 
?> 
+1

你有错误吗? '$ row'是一个对象还是一个数组?什么是输出? –

+0

var_dump($ val1,$ row-> relation);他们比较吗? –

回答

1

你正在做这么多事情错在这里

 V--------------------- Row Seems to be Object here 
if (($row->relation) == ($val1)) { 
    echo "<p><b>Applicant\'s Name:</b> {$row['relation_name']} 
             ^---------------------- Calling it as array here 

你澄清后以上你可以用printf代替

如果它的array

printf("<p><b>Applicant\'s Name:</b> %s|<b>Business:</b>%s</p>",$row['relation_name'],$row['relation_business']); 

如果其Object

printf("<p><b>Applicant\'s Name:</b>%s|<b>Business:</b>%s</p>",$row->relation_name,$row->relation_business); 
+0

另一种方法是使用echo,'echo'例如:echo“myString:”,$ variable;我相信这可以避免连接开销。 – daxroc

1

您可以使用.符号这显然是在php用于级联:

<?php 
    $val1 = Yes; 
    if (($row->relation) == ($val1)) { 
     echo "<p><b>Applicant\'s Name:</b>" . $row['relation_name'] . | . "<b>Business:</b>" . $row['relation_business'] . "</p>"; 
    } 
?> 

,或者你可以单独HTMLPHP这样的:

<?php 
    $val1 = Yes; 
    if (($row->relation) == ($val1)) { 
?> 
    <p><b>Applicant\'s Name:</b><?php echo $row['relation_name'] ?>|<b>Business:</b><?php echo $row['relation_business'] ?></p>  
<? 
    } 
?>