tags:

views:

264

answers:

5

I do not want to display all the slashes when displaying the html

What I have is

echo "<input type=\"text\" name=\"myname\" value=\"myvalue\"";

What I want to do is:

echo '<input type="text" name="myname" value="myvalue">';

I am looking to save myself from typing all the slashes. Is there a way around that?

+3  A: 

Your second example works (although it is ugly), I assume you want a way to be able to print variables while printing the HTML with double quotes. If that's the case, you could use Heredoc syntax:

echo <<<DOC
<input type="text" name="myname" value="myvalue">
DOC;

Or, better, yet, you could use a templating system (which PHP kind of is) or a MVC framework to separate your business and presentational logic so you don't have to go around printing stuff like input fields in the first place. :)

Paolo Bergantino
I don't really see that second example as being ugly. While it's probably better to separate logic and presentation I would say that it's fine for quickly outputting something. Especially if you're the only person working on the project.
David Weitz
It is ugly. It is painfully simple to set up your project in such a way that you don't have go around printing HTML in the middle of a PHP file like that. Eventually someone is going to take over your project and they will wish that you had better sense.
Paolo Bergantino
+1  A: 

Your second example works just fine. However, if you want you can use single slashes to quote the HTML, it will still come out valid. This would also allow you to quote variables:

echo "<input type='text' name='myname' value='$value' />";
PatrikAkerstrand
Won't that result in single quotes in the HTML code? Not that it's bad or anything, but I find it weird seeing single quotes in markup.
Jesper Karsrud
+2  A: 

You probably don't want to echo stuff really, and Paolo explained that quite well, but in general the best practice regarding apostrophes and quotation marks is as follows:

When you have apostrophes ' in the text, enclose it in double quotes "

echo "foo bar 'baz'";

When you have double quotes, enclose in apostrophes

echo 'foo bar "baz"';

This way you don't have to clutter it with backslashes. If you have both kinds, consider heredoc as per Paolo's example, or just stick to the style the rest of the code usually uses.

As to what comes to using apostrophes in HTML instead of double quotes.. While swapping them might be useful when you want to include variables, it would be more beneficial to always keep the same style - always apostrophes, or always double quotes.

You can also use printf (or sprintf), a function which often seems to be forgotten by PHP programmers:

printf('<input type="text" name="myname" value="%s" />', $value);
Jani Hartikainen
i think you mean apostrophe's not hyphens. On point, though.
Chris B.
Oops ;) Fixed now
Jani Hartikainen
+1  A: 

Do not forget you can also do something along the lines of:

/* PHP CODE */
?>

--- HTML HERE ---
<input type="text" name="myname" value="myvalue">

<?php
/*PHP CODE */

Typically I use the second example with apostrophes.

<?php

echo '<input type="text" name="myname" value="myvalue">';

?>

As Paolo has mentioned, you can also look into an MVC based framework. Which is Model-View-Controller. It is a very nice way to separate your Display Code (Presentation Logic) from your Functional Code (Business Logic).

A good starter MVC Framework is CodeIgniter. Check out their video tutorials to get a good idea of how these frameworks operate. There is somewhat of a learning curve but it will help you out in the long run!

Other Frameworks and Template Systems:

Best of Luck!

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

Chris B.
A: 
echo '<img src="'.$files[$i].'">';