2013-08-31 121 views
0

我正在爲我正在編寫的腳本編寫一些函數。我的問題是,內容生成的方式是由一個PHP變量。php - 函數回顯變量

因此,生成頁面內容,它必須是這樣的:

$contents .="page content inside this"; 

如果我做一個echo這樣的:

echo "page content inside this"; 

則呼應的文字會出現在頁面的頂部。因此,我必須使用$contents.="";才能生成頁面內容。

我目前正在編寫一個可以快速生成複選框的函數。我的功能看起來是這樣的:

function checkbox($name, $value, $checked){ 

    if($checked == 1){ 
     $checked = "checked"; 
    }else{ 
     $checked =""; 
    } 

    $contents.="<div class='roundedOne'> 
    <input type='checkbox' value='$value' id='roundedOne' name='$name' $checked /> 
    <label for='roundedOne'></label> 
    </div>"; 

} 

當我打電話一個頁面裏面的功能,會出現什麼:

$contents.=" 
".checkbox("name","value","1")." 
"; 

我可以想像,當我打電話的功能沒有任何理由發生,是我我用$contents而不是echo,但不幸的是這是我唯一的選擇,因爲腳本是加密的,所以我不能改變$contents的行爲方式。

我的問題是,我如何使用$ contents打印函數?

+0

回聲$內容; –

+0

而不是'$ contents。='只需在函數內輸入'return' – 2013-08-31 10:35:12

回答

0

你需要使用它的功能(不推薦)內之前聲明$contents全球 -

global $contents; 
$contents.="<div class='roundedOne'> 
    <input type='checkbox' value='$value' id='roundedOne' 
    name='$name' $checked /> 
    <label for='roundedOne'></label> 
    </div>"; 

或者,更好的,你可以生成你的函數的內容,然後返回它 -

function checkbox($name, $value, $checked){ 

    if($checked == 1){ 
     $checked = "checked"; 
    }else{ 
     $checked =""; 
    } 

    return "<div class='roundedOne'> 
     <input type='checkbox' value='$value' id='roundedOne' name='$name' 
     $checked /> 
     <label for='roundedOne'></label> 
     </div>";   
} 

$contents.= checkbox("name","value","1"); 

此外,您正在更改函數內的$checked參數的類型。這種編碼風格強烈地被阻止。儘量不要以這種方式改變變量的類型,它會爲您節省很多麻煩。

+0

爲什麼需要改變函數內部的參數?那我怎麼能這樣做?我對這種語言相當陌生,我很樂意學習秋天的小組。 – oliverbj

+0

@oliverbj:考慮這一點 - 你有一個變量聲明在一個長腳本的頂部,它包含一個數值。現在,在某行後,您將其更改爲保存字符串值。假設稍後有人決定更改代碼並用該變量(腳本底部的某處)添加一個數值。他不會得到準確的結果,因爲在這種情況下,該數字將與字符串一起與現有值連接。它不會產生錯誤。維護人員會看到他沒有得到準確的結果,但沒有錯誤!這些類型的錯誤很難修復。 –

+0

@oliverbj:對於你的方法,你可能不會陷入麻煩,因爲它非常簡單。但是,如果這個函數被修改/擴展,稍後有人可能會遇到麻煩....無論如何,你可以使用一個不同的變量來保存你的數值。 –

0

要麼使用global $contents或在您的功能return $contents

0

你從函數返回什麼的結束。從函數中返回字符串。或者使用$ content作爲oliverbj提示的全局變量

0

不知道我是否正確理解您的問題。您是否嘗試過使用全球

<?php 

$contents = "string string string"; 

function checkbox($name, $value, $checked){ 

    global $contents; 

    if($checked == 1){ 
     $checked = "checked"; 
    }else{ 
     $checked =""; 
    } 

    $contents.="<div class='roundedOne'> 
    <input type='checkbox' value='$value' id='roundedOne' name='$name' $checked /> 
    <label for='roundedOne'></label> 
    </div>"; 

} 

$contents.=" ".checkbox("name","value","1")." "; 

echo $contents; 

?> 
1

從函數返回值$contents

通過這種方式,您可以在頁面內獲得$contents的價值。

一樣,

function checkbox($name, $value, $checked){ 

    if($checked == 1){ 
     $checked = "checked"; 
    }else{ 
     $checked =""; 
    } 

    $contents.="<div class='roundedOne'> 
    <input type='checkbox' value='$value' id='roundedOne' name='$name' $checked /> 
    <label for='roundedOne'></label> 
    </div>"; 
    return $contents; 
} 
1

您從功能無返回值。

所以你沒有得到任何東西作爲checkbox函數的迴應。

只需要return $contents從函數,然後回聲該結果會爲你做。

function checkbox($name, $value, $checked){ 

    if($checked == 1){ 
     $checked = "checked"; 
    }else{ 
     $checked =""; 
    } 

    $contents.="<div class='roundedOne'> 
    <input type='checkbox' value='$value' id='roundedOne' name='$name' $checked /> 
    <label for='roundedOne'></label> 
    </div>"; 
    return $contents; 
} 

然後,

echo $contents;