2015-09-25 79 views
0

我有一個如下的函數,但是我得到了一個關於$tab變量的警告,說明它沒有被定義。我如何定義它並不再收到此警告?在函數中定義一個變量php

<?php 
/*** suggested articles as random ***/ 

function doArticle_suggested_small_horizontall($articleid,$title,$photo,$parentid,$catid,$altdescription) { 

    $tab .= "<table width=150 cellspacing=5 style=border: 1px solid #0066ff align=right>\n"; 
    $tab .= "<tr>\n"; 
    $tab .= "<td align=center bgcolor=#ffffff><a href='../artandculture/adetails.php?articleid=$articleid&parentid=$parentid&catid=$catid'> 
    <img src='../images/simage/$photo' border='0' alt='$altdescription'></a></td>\n"; 
    $tab .= "</tr>\n"; 
    $tab .= "<tr align=right width=150 height=80 border=0 style=border: 1px solid #ffffff>\n"; 
    $tab .= "<td width=110 align=right dir='rtl' border=0 style=border: 1px solid #ffffff ><p class=articletitlenounderline><a href='../artandculture/adetails.php?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title </strong></p></a></td>\n"; 
    $tab .= "</tr>\n"; 
    $tab .= "</table> <p> <hr class='hr99' ></hr></p>"; 
    return $tab; 
} 
$tab = ""; 
?> 

回答

6

您需要在函數的開頭定義$tab

更換$tab .= "<table width=15 ....

$tab = "<table width=15....

或者你可以只添加$tab = "";作爲函數的第一行,你現在,你應該刪除功能之外定義它。

+0

如何定義它? – Kaveh

+1

它在第一行中定義。 (你需要刪除'.'來進行concatonation) – dstudeba

+0

非常感謝你的幫助。 – Kaveh

0

的變量定義$tab

$tab = [...]; 

然後,你需要做的。=。變量不存在於開始使用它的位置。

0

「。=」告訴PHP將字符串添加到現有變量。如果沒有現有的變量,PHP不能添加字符串。

// Add this before your first $tab .= 
$tab = NULL; 
0

移動$tab的功能的啓動,以便串聯正常工作:

取而代之的是到底: $標籤=「」;

這樣做:

<?php 
    /////////////////////suggested articles as random ///////////////////////// 

    function doArticle_suggested_small_horizontall($articleid,$title,$photo,$parentid,$catid,$altdescription) { 
     $tab = ""; 
     $tab .= "<table width=150 cellspacing=5 style=border: 1px solid #0066ff align=right>\n"; 
     $tab .= "<tr>\n"; 
     $tab .= "<td align=center bgcolor=#ffffff><a href='../artandculture/adetails.php?articleid=$articleid&parentid=$parentid&catid=$catid'> 
     <img src='../images/simage/$photo' border='0' alt='$altdescription'></a></td>\n"; 
     $tab .= "</tr>\n"; 
     $tab .= "<tr align=right width=150 height=80 border=0 style=border: 1px solid #ffffff>\n"; 
     $tab .= "<td width=110 align=right dir='rtl' border=0 style=border: 1px solid #ffffff ><p class=articletitlenounderline><a href='../artandculture/adetails.php?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title </strong></p></a></td>\n"; 
     $tab .= "</tr>\n"; 
     $tab .= "</table> <p> <hr class='hr99' ></hr></p>"; 
     return $tab; 
    } 
    ?> 
0

除了其他的答案,如果你已經有一個分配給$標籤的東西,想從功能追加它,則必須在將其作爲參數傳遞給函數或添加功能輸出

function example($tab, $something = false) { 
    $tab .= ', added this in example function'; 
    return $tab; 
} 

$tab = 'Original content'; 
echo example($tab); // Output: Original content, added this in example function 

function example2($something = false) { 
    return ', added this in example2 function'; 
} 

$tab = 'Original content 2'; 
$tab .= example2(); 
echo $tab: // Output: Original content 2, added this in example2 function 
相關問題