2012-07-03 2 views

답변

1

PHP에서?

if(statement) { 
    echo '<button style="background: green;">BUTTON</button>'; 
} else { 
    echo '<button style="background: red;">BUTTON</button>'; 
} 
+0

' – JSantos

0

이 작동합니다 :

if(condition1 === true) 
{ 
    echo "<button style=\"background-color: green;\">Green</button>"; 
} 
else 
{ 
    echo "<button style=\"background-color: red;\">Red</button>"; 
} 

또는 다른 방법으로, 당신은 HTML을 많이 인쇄 할 쉽게 필요로 할 때 :

if(condition1 === true) 
{ 
    ?> 
    <button style="background-color: green;">Green</button> 
    <?php 
} 
else 
{ 
    ?> 
    <button style="background-color: red;">Red</button> 
    <?php 
} 
+0

제안을 추가했을 때 내 코드가 있습니다. 그것에, function checkStatus ($ loopCounter) { $ num = pow (2, $ loopCounter); $ linkstatus = shell_exec ("/ opt/napatech/bin/LinkTool -cmd Get -cb 0x". $ num); // $ pos = stripos ($ linkstatus, "Link status"); // $ pos1 = stripos ($ linkstatus, "Port type"); // $ status = substr ($ linkstatus, $ pos + 1, $ pos1- $ pos-10); $ bool = strpos ($ status, "not"); if ($ bool == false) echo ''; // else // $ status = "Not Connected"; // return $ status; } ?> – helloworld0722

+0

$ stoolpos 때문에 int가 아닌 부울 값이어야합니다. 'strpos ($ status, not not)> -1'인지 확인하는 것이 좋습니다. 그것이 사실이라면, 당신은 그것을 에코 할 수 있습니다. 현재 코드는 $ bool을 false로 만들지 않습니다. –

1

은 또한 당신이 삼항 연산자를 사용할 수 있습니다

echo '<button style="background-color:'. ($condition ? '#ff0000' : '#00ff00') .';">Button</button>'; 
+0

저는 수년간 프로그래밍을 해오 고 있으며 삼항 연산자는 여전히 저를 토하고 싶습니다. 자기 자신에게. – Lusitanian