Metin PHP Echo büyük bir blok

7 Cevap php

PHP için yeni Im ve ben kurallar yankı işlevini kullanarak için ne bilemiyorum. Ben css / js büyük bir blok yankı gerekiyorsa Örneğin, ben metnin her satırına yankı eklemeniz gerekir ya da tek bir yankı ile kod büyük bir blok yankı için bir yol var mı?

Ben bu gibi kodun büyük bir blok yankı çalıştığınızda, ben bir hata alıyorum:

if (is_single()) {
echo '<link type="text/css" rel="stylesheet" href="http://jotform.com/css/styles/form.css"/><style type="text/css"> 
.form-label{
width:150px !important;
}
.form-label-left{
width:150px !important;
}
.form-line{
padding:10px;
}
.form-label-right{
width:150px !important;
}
body, html{
margin:0;
padding:0;
background:false;
}

.form-all{
margin:0px auto;
padding-top:20px;
width:650px !important;
color:Black;
font-family:Verdana;
font-size:12px;
}
</style> 

<link href="http://jotform.com/css/calendarview.css" rel="stylesheet" type="text/css" /> 
<script src="http://jotform.com/js/prototype.js" type="text/javascript"></script> 
<script src="http://jotform.com/js/protoplus.js" type="text/javascript"></script> 
<script src="http://jotform.com/js/protoplus-ui.js" type="text/javascript"></script> 
<script src="http://jotform.com/js/jotform.js?v3" type="text/javascript"></script> 
<script src="http://jotform.com/js/location.js" type="text/javascript"></script> 
<script src="http://jotform.com/js/calendarview.js" type="text/javascript"></script> 
<script type="text/javascript"> 

JotForm.init(function(){
$('input_6').hint('ex: myname@example.com');
});
</script>';
}else {

}

(Örneğin, her satıra yankı ekleyerek) bir sürü iş olmadan kod büyük bloklar yankı için daha iyi bir yolu var mı?

7 Cevap

Bir seçenek php blok çıkmak ve sadece HTML yazmak için.

Senin kod ile, eğer ifadenin açılış kaşlı ayraç sonra, PHP sonlandırmak:

if (is_single()) { ?>

Daha sonra, echo ' çıkarın ve ';

Tüm html ve css sonra, kapatmadan önce }, yazmak:

<? } else {

Eğer sayfaya yazmak istediğiniz metni dinamik ise, biraz yanıltıcıdır alır, ama şimdi bu iyi çalışması gerekir.

Heredoc syntax çok yararlı olabilir:

// start the string with 3 <'s and then a word
// it doesn't have to be any particular string or length
// but it's common to make it in all caps.
echo <<< EOT
    in here is your string
    it has the same variable substitution rules
    as a double quoted string.
    when you end it, put the indicator word at the
    start of the line (no spaces before it)
    and put a semicolon after it
EOT;

Satır sonları içeren metni yineleyen ince ve metin veya (kullanılabilir hafızaya kaydetmek için) seferde yankı hatlarının miktarına sınırlama yoktur.

Kodunuzda hata dize görünür çıkmamış tek tırnak neden olur.

Bu satır göreceksiniz:

$('input_6').hint('ex: myname@example.com');

Bunu tek bir satır var olup olmadığını bir PHP string bu tek tırnak kaçmak gerekiyordu.

Orada olsa, büyük dizeleri yankı için iyi bir yoldur olduğunu ve PHP bloğu kapatın ve sonra tekrar açın bulunuyor:

if (is_single()) {
  ?>
<link type="text/css" rel="stylesheet" href="http://jotform.com/css/styles/form.css"/><style type="text/css"> 
.form-label{
width:150px !important;
}
.form-label-left{
width:150px !important;
}
.form-line{
padding:10px;
}
.form-label-right{
width:150px !important;
}
body, html{
margin:0;
padding:0;
background:false;
}

.form-all{
margin:0px auto;
padding-top:20px;
width:650px !important;
color:Black;
font-family:Verdana;
font-size:12px;
}
</style> 

<link href="http://jotform.com/css/calendarview.css" rel="stylesheet" type="text/css" /> 
<script src="http://jotform.com/js/prototype.js" type="text/javascript"></script> 
<script src="http://jotform.com/js/protoplus.js" type="text/javascript"></script> 
<script src="http://jotform.com/js/protoplus-ui.js" type="text/javascript"></script> 
<script src="http://jotform.com/js/jotform.js?v3" type="text/javascript"></script> 
<script src="http://jotform.com/js/location.js" type="text/javascript"></script> 
<script src="http://jotform.com/js/calendarview.js" type="text/javascript"></script> 
<script type="text/javascript"> 

JotForm.init(function(){
$('input_6').hint('ex: myname@example.com');
});
</script>
  <?php
}else {

}

Ya da muhtemelen okunabilmesi için daha iyi olan başka bir alternatif, başka bir sayfanın içine tüm bu statik HTML koymak ve () eklemektir.

Senin sorunun aslında neden olur:

$('input_6').hint('ex: myname@example.com');

Sen olmak için tek tırnak kaçmak gerekir \'

However: bir yorumlu metin kullanımı çok temiz genel olacak gibi, çok daha iyi bir fikir.

Man, PHP is not perl!
PHP can just escape from HTML :) http://www.php.net/manual/en/language.basic-syntax.phpmode.php

if (is_single()) {
//now we just close PHP tag
?>
</style> 
<script> 
<blah blah blah>
<?php
//open it back. here is your PHP again. easy!
}
?>

Böyle birçok insan çirkin yorumlu metin yapışmış merak ediyorum.

@ Hookedonwinter cevabı üzerine genişletmek için, burada bir alternatif (temizleyici, bence) sözdizimi:

<?php if (is_single()): ?>
    <p>This will be shown if "is_single()" is true.</p>
<?php else: ?>
    <p>This will be shown otherwise.</p>
<?php endif; ?>