Results 1 to 6 of 6
  1. #1
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    PHP and Quotes in HTML Tags
    What is the best way to handle quotes for attributes in HTML tags. I ask because I am working on a project and it just seems easier to have the entire file coded in PHP and define variable once rather then using PHP tags to do the work in areas of the page.

    What triggered the question what the head and metatags. Is is better or ok to include the quotes and escape them with a slash / or can I leave the quotes off the html tags?
    Thanks,
    Doug

  2. #2
    Grandma broke her coccyx! Uncle Rico's Avatar
    Join Date
    May 8th, 2007
    Location
    North Carolina
    Posts
    2,238
    I usually backslash the quotes....

    PHP Code:
    $tmp "some \"quoted\" goes here"

  3. #3
    ABW Veteran Mr. Sal's Avatar
    Join Date
    January 18th, 2005
    Posts
    6,795
    Quote Originally Posted by itsupportnotes
    What is the best way to handle quotes for attributes in HTML tags. I ask because I am working on a project and it just seems easier to have the entire file coded in PHP and define variable once rather then using PHP tags to do the work in areas of the page.

    What triggered the question what the head and metatags. Is is better or ok to include the quotes and escape them with a slash / or can I leave the quotes off the html tags?
    Doug, check this site out:
    Avoid Double Quotes Around Strings that Contain HTML.

    PHP offers a choice between using single or double quotes around strings (as well as perl's "heredoc"). The difference between the two methods is that variables are expanded within double quotes, but not within single quotes.

    To avoid variable concatenation, many people get in the habit of using double quotes for strings. But HTML also uses double quotes for attributes, and so lines of code can easily wind up looking like this: Source: PHP Coding Style and Organization

  4. #4
    Full Member
    Join Date
    October 22nd, 2006
    Posts
    200
    Alternatively you could use a mix of single and double quotes.

    PHP Code:
     $tmp "some 'quoted' goes here"

  5. #5
    Member
    Join Date
    September 5th, 2005
    Location
    Mansfield, TX
    Posts
    161
    As a programmer myself I always thought it best to escape them using thier entity equivalent. For quotes I use "

    In otherwords the syntax would be:

    $tmp = "some "quoted" goes here";

    This works regardless of where the text will be before being displayed in HTML such as a database or XML file.

  6. #6
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    The best solution is to keep your HTML files separate from your PHP files. Try not to echo any HTML with your PHP, but rather use a template class like Smarty. This allows you to insert variables into your HTML templates and have your PHP code fill it in.

    But if this is too much to handle right now, you should just end your PHP code and do your HTML as you would normally. For example:

    Code:
    <?php 
    php code here..
    ?>
    
    HTML code here with "normal" quotes.
    
    <?php
    finish your PHP
    ?>
    This way you don't need to waste your time backslashing quotes. It's absolutely unnecessary and is considered poor form to assign HTML to a variable (unless you leave it to your template class).

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  7. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. Problem with PHP code and quotes
    By hazlcha in forum WebMerge (Fourthworld.com)
    Replies: 5
    Last Post: August 21st, 2014, 06:17 PM
  2. I think I found the best "Free" HTML Editor on the planet- PHP & MySQL Tags; Plugins
    By HumbleFish in forum Programming / Datafeeds / Tools
    Replies: 17
    Last Post: April 15th, 2010, 02:43 AM
  3. Importing Imbedded HTML Tags
    By jpiper in forum WebMerge (Fourthworld.com)
    Replies: 2
    Last Post: October 13th, 2006, 06:19 PM
  4. html tags
    By Roland in forum Midnight Cafe'
    Replies: 1
    Last Post: July 21st, 2004, 12:38 AM
  5. HTML < table > tags in datafeed -
    By Serena in forum Programming / Datafeeds / Tools
    Replies: 1
    Last Post: June 13th, 2004, 07:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •