1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Alternative to using echo in php?

Discussion in 'PHP' started by Johnburk, Feb 19, 2006.

  1. #1
    I am doing some php programming (so I can learn it)
    SEMrush
    One methode to use html in php is with echo.

    For one or two lines, its a good way. But what about a whole page?

    Is there a alternative to using echo?
     
    Johnburk, Feb 19, 2006 IP
    SEMrush
  2. fsmedia

    fsmedia Prominent Member

    Messages:
    5,163
    Likes Received:
    262
    Best Answers:
    0
    Trophy Points:
    390
    #2
    Yes, similarly you could use print_r('STUFF HERE'); for a whole page though, I would just escape the PHP and use this:

    <?php echo " ?>
    all your HTML stuff goes here
    <?php "; ?>
    Code (markup):
    Some of that nature.
     
    fsmedia, Feb 19, 2006 IP
    Johnburk likes this.
  3. Johnburk

    Johnburk Peon

    Messages:
    777
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3

    Its not working. I get

    Parse error: parse error, unexpected on that line
     
    Johnburk, Feb 19, 2006 IP
  4. fsmedia

    fsmedia Prominent Member

    Messages:
    5,163
    Likes Received:
    262
    Best Answers:
    0
    Trophy Points:
    390
    #4
    fsmedia, Feb 19, 2006 IP
  5. themole

    themole Peon

    Messages:
    82
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    All you need is

    
    <?php 
    
    //here's my fun php code, where i'm doing lots of stuff
    
    ?>
    
    Now just plain jane html here, but i wanted to display a variable, or some other php code, i could do <?php echo substr($myvar, 0, 6) ?> right in the middle of my html.
    
    <?php
    
    //some more php stuff...
    
    ?>
    
    Code (markup):
    You can also include line returns in your echo statements so you could do this (which is what I think you're asking)

    
    echo "<Table>
    <tr>
      <td>
        Title
      </td>
      <td>
         Descrip.
      </td>
      <td>
         Price
      </td>
    </tr>
    </table>";
    
    Code (markup):
    You should only use print_r for testing (seeing what's stored in an array for example). Use echo or print to display anything else.

    -the mole
     
    themole, Feb 19, 2006 IP
    Johnburk likes this.
  6. sarahk

    sarahk iTamer Staff

    Messages:
    26,875
    Likes Received:
    4,093
    Best Answers:
    116
    Trophy Points:
    665
    #6
    For a whole page there's no problem using print and echo with breaks back into pure html (as discussed above). I have some very old php speed tests you may want to check out but in reality the real impact will be in the image sizes, overall page size, server performance and the users dialup/broadband connection.

    For larger systems where there's alot of logic and alot going on consider the following strategies:
    • Using a templating system like smarty - this makes it easier to manage but ultimately smarty uses echo too :)
    • having the logic at the top of the script and all presentation work at the bottom of the script
    • having 2 scripts, one includes files, gets all the data, copes with permissions etc, the other turns them into pages - this approach is used by Mambo/Joomla
     
    sarahk, Feb 20, 2006 IP
  7. wwm

    wwm Peon

    Messages:
    308
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    print 'hello world';
     
    wwm, Feb 20, 2006 IP