0

Im trying to put the following inside a php variable.

I dont know what im doing wrong here :

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

2 Answers2

0

Your string concat is a bit off, and PHP uses dots . to concat variables together, not + like JavaScript.

There are a couple of ways you could achieve this, via a ternary operator

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

Or string concatination via a normal if

$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>';
Qirel
  • 25,449
  • 7
  • 45
  • 62
0

You can't use if statement like that. Instead use inline version: (condition ? true_case : false_case)

You can write variables directly into quotes 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>';

edit: in php whole ternary operator have to be surrounded by brackets to work properly

Alegro
  • 36
  • 4