2012-09-21 21 views
21

ho bisogno di eco un sacco di PHP e HTML.PHP stringa multilinea con PHP

Ho già provato l'ovvio, ma non funziona:

<?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 } ?>'; 
?> 

Come posso fare?

+4

Stai letteralmente cercando di eco codice PHP? –

+0

oltre agli ovvi problemi con le citazioni senza escape si dovrebbero convertire tutti i tag < and > nel loro codice html corrispondente < > – transilvlad

+1

La maggior parte delle persone che fanno questa domanda cercano HEREDOC. Potresti voler accettare quella risposta. – Archonic

risposta

20

Non c'è bisogno di uscita php tags:

<?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>'; 
?> 
+1

mi sembra buono - grazie – Matt

28

Non è possibile eseguire codice PHP all'interno di una stringa del genere. Semplicemente non funziona. Così, quando sei "fuori" di codice PHP (?>), qualsiasi testo al di fuori dei blocchi PHP è considerato uscita comunque, quindi non c'è bisogno per l'istruzione echo.

Se si ha bisogno di fare uscita multilinea da un pezzo di codice PHP, è consigliabile utilizzare un HEREDOC:

<?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; 
0

I l'insieme interno di virgolette singole nel codice sta uccidendo la stringa. Ogni volta che si preme una virgoletta, termina la stringa e continua l'elaborazione. Ti consigliamo qualcosa come:

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

Utilizzare Heredocs per generare stringhe di muli-line contenenti variabili. La sintassi è ...

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

La parte "heredoc" è come le virgolette, e può essere tutto quello che vuoi. Il tag di fine deve essere l'unica cosa sulla sua linea, cioè senza spazi bianchi prima o dopo, e deve terminare in due punti. Per maggiori informazioni check out the manual.

1

Per fare questo, è necessario rimuovere tutti i ' charachters nella stringa o utilizzare un carattere di escape. Come:

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

utilizzare la funzione show_source(); di PHP. Verificare la presenza di ulteriori dettagli in show_source. Questo è un metodo migliore, immagino.

0

Un'altra opzione sarebbe quella di utilizzare il if con due punti e un endif al posto delle staffe:

<?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>