PHP Echo a large block of text

后端 未结 11 877
南方客
南方客 2021-01-30 21:00

Im new to PHP and I can\'t figure out what the rules are for using the echo function. For example, if I need to echo a large block of css/js, do I need to add echo to each line

相关标签:
11条回答
  • 2021-01-30 21:29

    Echoing text that contains line breaks is fine, and there's no limit on the amount of text or lines you can echo at once (save for available memory).

    The error in your code is caused by the unescaped single quotes which appear in the string.

    See this line:

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

    You'd need to escape those single quotes in a PHP string whether it's a single line or not.

    There is another good way to echo large strings, though, and that's to close the PHP block and open it again later:

    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 {
    
    }
    

    Or another alternative, which is probably better for readability, is to put all that static HTML into another page and include() it.

    0 讨论(0)
  • 2021-01-30 21:30
    $num = 5;
    $location = 'tree';
    
    $format = 'There are %d monkeys in the %s';
    echo sprintf($format, $num, $location);
    
    0 讨论(0)
  • 2021-01-30 21:33

    You can achieve that by printing your string like:

    <?php $string ='here is your string.'; print_r($string); ?>
    
    0 讨论(0)
  • 2021-01-30 21:34

    One option is to get out of the php block and just write HTML.

    With your code, after the opening curly brace of your if statement, end the PHP:

    if (is_single()) { ?>
    

    Then remove the echo ' and the ';

    After all your html and css, before the closing }, write:

    <? } else {
    

    If the text you want to write to the page is dynamic, it gets a little trickier, but for now this should work fine.

    0 讨论(0)
  • 2021-01-30 21:37

    Just break out where you need to.

    <html>
    (html code)
    <?php
    (php code)
    ?>
    (html code)
    </html>
    

    Do not use shortened-form. <? conflicts with XML and is disabled by default on most servers.

    0 讨论(0)
提交回复
热议问题