PHP Echo a large block of text

后端 未结 11 876
南方客
南方客 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:14

    Your problem is actually caused by:

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

    You need to escape the single quotes to be \'

    However: Using a Heredoc is a much better idea, as it will be much cleaner overall.

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

    To expand on @hookedonwinter's answer, here's an alternate (cleaner, in my opinion) syntax:

    <?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; ?>
    
    0 讨论(0)
  • 2021-01-30 21:17

    I prefer to concatenate multiple Strings together. This works either for echo AND for variables. Also some IDEs auto-initialize new lines if you hit enter. This Syntax also generate small output because there are much less whitespaces in the strings.

    echo ''
        .'one {'
        .'    color: red;'
        .'}'
        ;
    
    $foo = ''
        .'<h1>' . $bar . '</h1>'  // insert value of bar
        .$bar                     // insert value of bar again
        ."<p>$bar</p>"            // and again
        ."<p>You can also use Double-Quoted \t Strings for single lines. \n To use Escape Sequences.</p>"
        // also you can insert comments in middle, which aren't in the string.
        .'<p>Or to insert Escape Sequences in middle '."\n".' of a string</p>'
        ;
    

    Normally i start with an empty string and then append bit by bit to it:

    $foo = '';
    
    $foo .= 'function sayHello()'
        .'    alert( "Hello" );'
        ."}\n";
    
    $foo .= 'function sum( a , b )'
        .'{'
        .'    return a + b ;'
        ."}\n";
    

    (Please stop Posts like "uh. You answer to an five jears old Question." Why not? There are much people searching for an answer. And what's wrong to use five year old ideas? If they don't find "their" solution they would open a new Question. Then the first five answers are only "use the search function before you ask!" So. I give you another solution to solve problems like this.)

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

    Check out heredoc. Example:

    echo <<<EOD
    Example of string
    spanning multiple lines
    using heredoc syntax.
    EOD;
    
    echo <<<"FOOBAR"
    Hello World!
    FOOBAR;
    

    The is also nowdoc but no parsing is done inside the block.

    echo <<<'EOD'
    Example of string
    spanning multiple lines
    using nowdoc syntax.
    EOD;
    
    0 讨论(0)
  • 2021-01-30 21:23

    Heredoc syntax can be very useful:

    // 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;
    
    0 讨论(0)
  • 2021-01-30 21:27

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

    I wonder why such many people stuck to ugly heredoc.

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