Modify PHP. Customize how PHP calls a function

前端 未结 4 1081
终归单人心
终归单人心 2020-12-22 07:10

Can php be modified to create a custom way to call a php function without opening and closing php tags? For example, given an example function like this that is incl

4条回答
  •  离开以前
    2020-12-22 07:25

    A similar effect is achievable using short_tags. Instead of doing

    
    

    do

    
    

    or

    
    

    If you have php version 5.4 or greater, these tags will be supported. Prior to that, short_tags are disabled by defaut. Even in 5.4+, they can be disabled by the server so use it wiseley.

    Another way is to echo html in a string so you don't have to switch in and out of php. Just remember to keep it readable for yourself and others:

    
              
              
              
                  " . $text . "
              
          ";
    

    You can also use heredocs:

    $html = <<
            
            
            
                $text
            
        
    HTML;
    
    echo $html;
    

    Edit: I also wanted to point out ob_start and ob_get_clean. What this will do is "record" everything that should be printed out on the screen without printing it out. This allows you to complete all of the logic in your code before the contents are displayed on the screen. You still need to either open and close php tags or echo strings containing html.

    
    
    
        
        
        
            
        
    
    
    

    This may not be helpful for small webpages that don't need to process a lot of information. But if you have a lot of information to be displayed. It's beneficial to send large chunks of data to HTML over sending lots of smaller bits and can increase performance.

提交回复
热议问题