2017-03-09 19 views
0

私はPHP変数の中に以下を入れようとしています。私はイムはここに間違っているものを知らない変数内のステートメントIf

$cartlink='<a class="cart-contents" href="'.$shoppingcart.'" title="View your shopping cart">' + if ($count > 0) {<span class="cart-contents-count">"'.$count.'"</span></a>} + '</a>'; 
+0

可能な複製を動作するように括弧で囲まれている代わりに、インラインバージョンを使用、簡体字、If..Else三項演算子を使用して](http://stackoverflow.com/questions/29814065/php-shorthand-if-else-using-ternary-operators) – ShiraNai7

+1

あなたはPHPで経験がありますか:| – Andrew

+2

@Andrewいいえ、彼はいません –

答えて

0

あなたの文字列の連結は少しオフになっている、とPHPは、一緒に変数をCONCATない+ JavaScriptなどのためにドット.を使用しています。あなたがそのような文であれば使用することはできません

$cartlink = '<a class="cart-contents" href="'.$shoppingcart.'" title="View your shopping cart">'; 
if ($count > 0) 
    $cartlink .= '<span class="cart-contents-count">"'.$count.'"</span></a>'; 
$cartlink .= '</a>'; 
0

場合

は、通常の介して、三項演算子

$cartlink = '<a class="cart-contents" href="'.$shoppingcart.'" title="View your shopping cart">'.($count > 0 ? '<span class="cart-contents-count">"'.$count.'"</span></a>' : "").'</a>'; 

または文字列連結のを経由して、あなたがこれを達成できるいくつかの方法があります。 (condition ? true_case : false_case)

あなたが引用符に直接 echo "Value: $variable";

$cartlink="<a class='cart-contents' href='$shoppingcart' title='View your shopping cart'>" . ($count > 0 ? "<span class='cart-contents-count'>$count</span></a>" : "") . '</a>'; 

編集する変数を書くことができます:phpの全体の三項演算子で[PHPの適切

関連する問題