Is it more efficient to echo out a segment of html code inside of your php code, or is it more efficient to end the php code segment and switch to html? Example: <?php //there would be a bunch of php code before this line echo '<form> First name: <input type="text" name="firstname"> <br> Last name: <input type="text" name="lastname"> </form>'; // there would be a bunch of php code after this line ?> VERSUS <?php //there would be a bunch of php code before this line ?> <form> First name: <input type="text" name="firstname"> <br> Last name: <input type="text" name="lastname"> </form> <?php //there would a bunch of php code after this line ?>
Yes the second approach puts lower load on the server because the HTML code is out of PHP blocks and does not need server side processing. But we are talking about very little difference especially on the high end servers used today. So choose what is more comfortable for you.
There is another merit for the second approach: my clients find it very easy to modify the design without having to worry about the code (more like template approach).
Does using '<?php' lose any efficiency though? If I use the second approach all the time, I often have to embed <?php and ?> within my form.
Plain HTML is much faster for blocks of text (in comparison), it means PHP doesn't have to parse it. It's faster. If however you're parsing variables within the HTML, it may be easier just to keep within PHP.
It may be easier to do otherwise, but is parsing variables within HTML more efficient than keeping the entire code in PHP?
As long as it's in a single call, and not multiple calls, and you ensure it's echo, and using commas to concatenate. It's faster, but remember. The difference is NANOseconds. Dan
I can't remember the last time I used <?php I always, without exception use <? especially when just echoing one variable. For instance, let's say you wanted to put a value into a text field. Here is how I would do it. <input type="text" name="name" value="<?=$variable?>" /> PHP: See? Quick and easy!
If you are using single quotes, php won't parse any of the text you are outputting, whereas with double quotes it will, which is slower. Eg 'blah $variable' will output blah $variable but "blah $variable" will output blah <whatever $variable is>
I wouldn't be much concerned for a small piece of code, I would say it's up to you, use what ever looks cleaner/easier to you. I use both ways, but prefer to use php for what it was made for.
To the person a few posts above, <?php is generally preferably to <? because <? requires the short tags setting to be on, or won't be parsed. Dan
Dan, you are right, it's best to use the full tag <?php. It doesn't hurt to know your php configs and how to change them though... (if you have access to)
Well, I like the second way. That way the text editor can still make out the syntax, plus you don't have to escape everything lol.
I think the first one is faster then the second one....lol This is my own idea but I would like to take a look....I think when everytime I open a php open tag (<?php) its like when I open a new connection to the database, so its gonna take a little bit amount of time to process that. Don't mock me if I'm wrong, I just only guess..lol
I think .php files are fully php executable anyways, so even if you don't open a tag it's still opening a php application because of the application/php5 that the server executes on. I think both methods are around the same speed though.
Ooops, no it doesn't. You need to open a php tag in order for the php engine combiler to recognize a php code.