How can I echo HTML in PHP?

后端 未结 13 2226
耶瑟儿~
耶瑟儿~ 2020-11-22 06:40

I want to conditionally output HTML to generate a page, so what\'s the easiest way to echo multiline snippets of HTML in PHP 4+? Would I need to use a template framework lik

相关标签:
13条回答
  • 2020-11-22 07:13

    This is how I do it:

    <?php if($contition == true){ ?>
             <input type="text" value="<?php echo $value_stored_in_php_variable; ?>" />
    <?php }else{ ?>
             <p>No input here </p>
    <?php } ?>
    
    0 讨论(0)
  • 2020-11-22 07:14

    Simply use the print function to echo text in the PHP file as follows:

    <?php
    
      print('
        <div class="wrap">
    
          <span class="textClass">TESTING</span>
    
        </div>
      ')
    ?>
    
    0 讨论(0)
  • 2020-11-22 07:16

    You could use the alternative syntax alternative syntax for control structures and break out of PHP:

    <?php if ($something): ?>
        <some /> <tags /> <etc />
        <?=$shortButControversialWayOfPrintingAVariable ?>
        <?php /* A comment not visible in the HTML, but it is a bit of a pain to write */ ?>
    <?php else: ?>
        <!-- else -->
    <?php endif; ?>
    
    0 讨论(0)
  • 2020-11-22 07:19

    Don't echo out HTML.

    If you want to use

    <?php echo "<h1>  $title; </h1>"; ?>
    

    you should be doing this:

    <h1><?= $title;?></h1>
    
    0 讨论(0)
  • 2020-11-22 07:22

    There are a few ways to echo HTML in PHP.

    1. In between PHP tags

    <?php if(condition){ ?>
         <!-- HTML here -->
    <?php } ?>
    

    2. In an echo

    if(condition){
         echo "HTML here";
    }
    

    With echos, if you wish to use double quotes in your HTML you must use single quote echos like so:

    echo '<input type="text">';
    

    Or you can escape them like so:

    echo "<input type=\"text\">";
    

    3. Heredocs

    4. Nowdocs (as of PHP 5.3.0)

    Template engines are used for using PHP in documents that contain mostly HTML. In fact, PHP's original purpose was to be a templating language. That's why with PHP you can use things like short tags to echo variables (e.g. <?=$someVariable?>).

    There are other template engines (such as Smarty, Twig, etc.) that make the syntax even more concise (e.g. {{someVariable}}).

    The primary benefit of using a template engine is keeping the design (presentation logic) separate from the coding (business logic). It also makes the code cleaner and easier to maintain in the long run.

    If you have any more questions feel free to leave a comment.

    Further reading is available on these things in the PHP documentation.


    NOTE: PHP short tags <? and ?> are discouraged because they are only available if enabled with short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option. They are available, regardless of settings from 5.4 onwards.

    0 讨论(0)
  • 2020-11-22 07:22

    Try it like this (heredoc syntax):

    $variable = <<<XYZ
    <html>
    <body>
    
    </body>
    </html>
    XYZ;
    echo $variable;
    
    0 讨论(0)
提交回复
热议问题