2017-03-09 1 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

가능한 중복 작업을 괄호로 둘러싸인 할 필요가 대신 인라인 버전을 사용 , 속기, 만약 .. 삼항 연산자를 사용하여 끝내기] (http://stackoverflow.com/questions/29814065/php-shorthand-if-else-using-ternary-operators) – ShiraNai7

+1

php에서 경험이 있습니까? – Andrew

+2

@Andrew 아니, 아니야. –

답변

0

귀하의 문자열 CONCAT 조금 떨어져 있으며, PHP는, 함께 자바 스크립트와 같은하지 +를 변수를 CONCAT하는 점 .를 사용합니다. 당신은 같은 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>'; 
0

경우

정상적인 통해 삼항 연산자

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

또는 문자열 concatination을 통해, 당신이 얻을 수있는 몇 가지 방법이 있습니다. (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의 제대로

관련 문제