0

因此,我正在使用一個函數,它在HTML中的兩個不同位置使用,如下所示。PHP函數返回或回顯變量

function remaining($expDate,$output){ 
$dStart = new DateTime(date('Y-m-d')); 
    $dEnd = new DateTime(date('Y-m-d ',strtotime($expDate))); 

    $dDiff = $dStart->diff($dEnd); 
    $dDiff->format('%R'); 

    if($output = "return"){ 
     return $dDiff->days; 
    }else{ 
     echo $dDiff->days;} 
} 
<div 
    class="progress-bar <?php if(remaining($expDate,"return")==90){echo"progress-bar-success";}else{echo "progress-bar-danger";}?> 
    role="progressbar" 
    aria-valuenow="40" 
    aria-valuemin="0" aria-valuemax="100" 
    style="width: <?php remaining($expDate,"echo") ?>%"> 

當我返回值style部分不工作,當我在if()聲明呼應然後我得到的值:(於是,我就添加第二個參數,並給出相對輸出。 我明明在這裏失去了一些東西,但無法弄清楚:(你能給我上可變輸出的提示上的功能?或者可能是一個更好的方式來實現這一目標:)

回答

0

的問題是if語句

This is an attribution not a comparison 
if($output = "return"){ ... 

應該是這樣的:

function remaining($expDate,$output){ 
    $dStart = new DateTime(date('Y-m-d')); 
    $dEnd = new DateTime(date('Y-m-d ',strtotime($expDate))); 
    $dDiff = $dStart->diff($dEnd); 
    return $dDiff->days; 
} 

AH!順便說一句,上面的代碼工作,你沒有管理得到它的工作,因爲你忘記這麼做:<?php echo remaining()... ?>你忘記了回聲!

問候。

+0

嗯,只是糾正if語句發揮了訣竅。我很想念它:(非常感謝你@Joao – cevizmx

+0

@cevizmx這是一個常見的錯誤;) –

+0

@cevizmx檢查我的更新,你正在做的「輸出」事情是討厭的:D! –

0
if($output = "return") 

永遠是真實的,它修正爲

if($output == "return") 

所以程序進行比較,並返回TRUE或FALSE

0
if($output = "return"){ 

需求是

if($output == "return"){ 

如果你把

if($output = "return"){ 

那麼有效$輸出設定爲等於回報(這將始終返回true!)

0

你的if語句似乎不好。您正在使用'=',這聽起來不像PHP中的'equals'運算符。所以,只需用'=='替換條件中的'='即可。

您可能更喜歡在這裏傳遞一個簡單的布爾值(true/false)而不是字符串,但這取決於您。