2012-09-21 3 views
21

PHP와 HTML을 많이 에코해야합니다.PHP가있는 PHP 다중 행 문자열

은 이미 명백한 시도했지만 제대로 동작하지 않습니다 :

<?php echo ' 
<?php if (has_post_thumbnail()) { ?> 
     <div class="gridly-image"><a href="<?php the_permalink() ?>"><?php the_post_thumbnail('summary-image', array('class' => 'overlay', 'title'=> the_title('Read Article ',' now',false)));?></a> 
     </div> 
     <?php } ?> 

     <div class="date"> 
     <span class="day"> 
     <?php the_time('d') ?></span> 
     <div class="holder"> 
     <span class="month"> 
      <?php the_time('M') ?></span> 
     <span class="year"> 
      <?php the_time('Y') ?></span> 
     </div> 
    </div> 
    <?php } ?>'; 
?> 

은 내가 어떻게 할 수 있습니까?

+4

당신은 말 그대로 PHP 코드를 에코하려고합니까? –

+0

unescaped 따옴표가있는 명백한 문제 외에도 잘해야 모든 < and > 태그를 해당하는 html 코드 < > – transilvlad

+1

으로 변환해야합니다.이 질문을하는 대부분의 사람들은 HEREDOC을 찾고있을 것입니다. 그 대답을 받아 들일 수도 있습니다. – Archonic

답변

20

당신은 출력 php 태그가 필요하지 않습니다 코드에서 작은 따옴표로 묶인 내부 집합이 문자열을 강제 종료합니다. 작은 따옴표를 치면 문자열을 끝내고 처리를 계속합니다.

$thisstring = 'this string is long \' in needs escaped single quotes or nothing will run'; 
+1

나에게 잘 어울리는 군 - 감사합니다. – Matt

28

PHP 코드를 이와 같은 문자열 내에서 실행할 수 없습니다. 그것은 단지 작동하지 않습니다. 또한 PHP 코드 ("?>")를 "벗어났다"면 PHP 블록 외부의 텍스트는 출력으로 간주되므로 echo 문은 필요하지 않습니다.

당신이 PHP 코드의 청크에서 여러 출력을 어떻게해야합니까 경우 HEREDOC 사용을 고려 :

<?php 
    if (has_post_thumbnail()) 
    { 
     echo '<div class="gridly-image"><a href="'. the_permalink() .'">'. the_post_thumbnail('summary-image', array('class' => 'overlay', 'title'=> the_title('Read Article ',' now',false))) .'</a></div>'; 
    } 

    echo '<div class="date"> 
       <span class="day">'. the_time('d') .'</span> 
       <div class="holder"> 
       <span class="month">'. the_time('M') .'</span> 
       <span class="year">'. the_time('Y') .'</span> 
       </div> 
      </div>'; 
?> 
0

:

<?php 

$var = 'Howdy'; 

echo <<<EOL 
This is output 
And this is a new line 
blah blah blah and this following $var will actually say Howdy as well 

and now the output ends 
EOL; 
15

변수가 들어있는 줄이있는 문자열을 출력하려면 Heredocs를 사용하십시오. 구문은 ...

$string = <<<HEREDOC 
    string stuff here 
HEREDOC; 

"HEREDOC"부분은 따옴표와 비슷하며 원하는대로 지정할 수 있습니다. 종료 태그는 줄의 유일한 것, 즉 앞에 또는 뒤에 공백이 없어야하며 콜론으로 끝나야합니다. 자세한 내용은 check out the manual.

1

문자열을 바꾸려면 문자에서 ' 개의 문자를 모두 제거하거나 이스케이프 문자를 사용해야합니다. Like :

<?php 
    echo '<?php 
       echo \'hello world\'; 
      ?>'; 
?> 
0

PHP의 show_source(); 기능을 사용하십시오. show_source에서 자세한 내용을 확인하십시오. 이것은 내가 생각하기에 더 나은 방법입니다.

0

또 다른 옵션은 콜론 if 대신 브라켓의 endif을 사용하는 것입니다 :

<?php if (has_post_thumbnail()): ?> 
    <div class="gridly-image"> 
     <a href="<?php the_permalink(); ?>"> 
     <?php the_post_thumbnail('summary-image', array('class' => 'overlay', 'title'=> the_title('Read Article ',' now',false))); ?> 
     </a> 
    </div> 
<?php endif; ?> 

<div class="date"> 
    <span class="day"><?php the_time('d'); ?></span> 
    <div class="holder"> 
     <span class="month"><?php the_time('M'); ?></span> 
     <span class="year"><?php the_time('Y'); ?></span> 
    </div> 
</div>