Jump to content

Echoing html


TheFilmGod

Recommended Posts

I just finished building the front end for a very complex application and I'm about to start the programming aspect of it.

 

My question - what's the best way of echoing html? I usually just do echo '......................' but I heard there are more efficient ways?

 

What about variables? HOw do you do it:

 

1) echo '..html...' . $variable;

2) echo '...html..';

echo $variable;

Link to comment
Share on other sites

<?php
echo 'html'.$var;

echo 'html',$var; // I personally use this way. the tests I've run show its slightly faster because it is taking it is separate arguments instead of concatenate them

echo "html {$var}";

// or if you have a lot of html to display:
?>
<html stuff>
<?php 
echo $var;
//...

Link to comment
Share on other sites

Something I never understood is why are double quotes the ones that interpret variables? Taking into account html and attributes ... attribute="value"... it would make sense to make single quotes interpret variables.

 

echo "html {$var}"; - I have never seen this before. Can someone tell me what this means?

Link to comment
Share on other sites

Something I never understood is why are double quotes the ones that interpret variables? Taking into account html and attributes ... attribute="value"... it would make sense to make single quotes interpret variables.

But you can always echo the " by adding a slash like \" or you could use the ' for the attributes, as it works just as well such as echo "<a href='#'>Test</a>";

Link to comment
Share on other sites

Something I never understood is why are double quotes the ones that interpret variables? Taking into account html and attributes ... attribute="value"... it would make sense to make single quotes interpret variables.

 

echo "html {$var}"; - I have never seen this before. Can someone tell me what this means?

 

The use of the braces is simply to 'help' the PHP engine. In this case, they're not required as it can work out that you want to happen. But if you had this, for example:

 

<?php
class foo{
    private $var = 'some value';
    function __construct(){
        echo "html {$this->getVar()}" . "\n";
    }
    
    function getVar(){
        return $this->var;
    }
    
}

$foo = new foo();

 

Then it won't work without the braces -- you'll get "html ()" as the output.

Link to comment
Share on other sites

echo 'html',$var; // I personally use this way. the tests I've run show its slightly faster because it is taking it is separate arguments instead of concatenate them

 

That is incorrect. The way the engine handles multiple arguments to echo() means concatenation is faster. See: http://pastie.org/523020

 

Also see: http://groups.google.com/group/make-the-web-faster/browse_thread/thread/ddfbe82dd80408cc

 

These kinds of micro optimizations are mostly useless anyway. Don't optimize your code before you've profiled it to find out where the bottlenecks are. Otherwise you just risk ending up wasting time on premature optimization.

Link to comment
Share on other sites

Are the braces necessary for multidimensional arrays? Like so: {$big_array[small_array[0]]} or will the php engine know what you mean?

 

Which technique do the gurus on this forum use to echo html? Do you just do the single quote + concatenation type?

Link to comment
Share on other sites

Are the braces necessary for multidimensional arrays? Like so: {$big_array[small_array[0]]} or will the php engine know what you mean?

okay think if it this way

Your going to echo out a variable ($var) and some StaticText with NO space between them..

echo "$varStaticText";

and you get Nothing because their is no variable called $varStaticText

If a dollar sign ($) is encountered' date=' the parser will greedily take as many tokens as possible to form a valid variable name. Enclose the variable name in curly braces to explicitly specify the end of the name. [/quote']

 

so you have 2 choices

echo $var."StaticText";

or

echo "{$var}StaticText";

 

 

Which technique do the gurus on this forum use to echo html? Do you just do the single quote + concatenation type?

 

The forum was written by SMF.. but personally I use what ever suites at the time, by default when echoing html I'll use double quotes, but it really does depend on what I'm doing

Link to comment
Share on other sites

If I am echoing out plain HTML without any PHP variables I usually jump out of PHP, if I'm using variables in a small amount of HTML I use single quotes and concatenation.

 

If I am using alot of HTML with quoted elements I like to use a heredoc which uses the C++ cout syntax and you can use any quotes or variables you need,

 

$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

 

From http://uk2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.