2012-06-12 4 views
0

index.php와 같은 것을 열 때 뭔가를하려고합니까? bodyonly = 1 PHP는 body html 태그 안에있는 내용 만 반환합니다. 그러나 다음과 같은 코드를 시도 할 때 :PHP는 전에 <html> 태그를주는 태그

<?php if !isset($_GET["bodyonly"]): ?><html> 
    <head> 
     <title>TEST</title> 
    </head> 

    <body> 

     <p>Up!</p> 

     <?php endif; ?><p>Down!</p><?php if !isset($_GET["bodyonly"]): ?> 
    </body> 
</html><?php endif; ?> 

오류가 발생하고 아무 것도 나타나지 않습니다. if (isset ...) {echo ...} 같은 일이 일어나는 대신 if를 사용하여 대안을 시도해 보았습니다. 그러나 그러고 싶지 않은 다른 많은 변화를해야합니다.

나에게 계몽주의? :)

+0

무엇입니까? 'if! isset ($ _ GET [ "bodyonly"]) :' – Sebas

+2

괄호 안에'if' 조건이 오타가 있지 않습니까? 대체 구문 AFAIK에도 필요합니다. – Shoaib

답변

3

PHP 구문의 모든 유형에서 조건에는 대괄호가 필요합니다.

<?php if (!isset($_GET["bodyonly"])): ?><html> 
    <head> 
     <title>TEST</title> 
    </head> 

    <body> 

     <p>Up!</p> 

     <?php endif; ?><p>Down!</p><?php if (!isset($_GET["bodyonly"])): ?> 
    </body> 
</html><?php endif; ?> 

또한 코드에 최신 구조를 사용하는 것이 좋습니다. 중괄호와 PHP를 HTML 래퍼가 아닌 프로그래밍 언어로 사용하면 조건 적으로 실행되는 내용을 매우 쉽게 볼 수 있습니다.

<?php 

$status="Up"; 
// $status="Down"; 

$header="<html>\n\t<head>\n\t\t<title>TEST</title>\n\t</head>\n\n <body>\n"; 
$footer=" </body>\n</html>\n"; 

if (!isset($_GET["bodyonly"])) { 
    print $header; 
} 

printf("\t<p>%s</p>\n", $status); 

if (!isset($_GET["bodyonly"])) { 
    print $footer; 
} 

또는, 심지어 그것의 재미를 위해 :

<?php 

$status="Up"; 
// $status="Down"; 

$header="<html>\n\t<head>\n\t\t<title>TEST</title>\n\t</head>\n\n <body>\n"; 
$footer=" </body>\n</html>\n"; 

print isset($_GET["bodyonly"]) ? "" : $header; 

printf("\t<p>%s</p>\n", $status); 

print isset($_GET["bodyonly"]) ? "" : $footer; 

또는 (이 그냥 바보지고) :

<?php 

$status="Up"; 
// $status="Down"; 

$header=""; $footer=""; 

if (!isset($_GET["bodyonly"])) { 
    $header="<html>\n\t<head>\n\t\t<title>TEST</title>\n\t</head>\n\n <body>\n"; 
    $footer=" </body>\n</html>\n"; 
} 

print $header . sprintf("\t<p>%s</p>\n", $status) . $footer; 

는 PHP에서 instructions on syntax에서보세요. 그물.