PHP best practice question

Discussion in 'PHP' started by ChaosFoo, Jan 28, 2008.

  1. #1
    I'm using PHP to dynamically create web form to add products to a shopping cart. Here is an example line of code:

    
    $pCRV .= "<input type=\"hidden\" name=\"add\" value=\"1\">\n";
    Code (markup):
    The code also works when written like this:

    
    $pCRV .= "<input type='hidden' name='add' value='1'>\n";
    Code (markup):
    I like the second option because it is much cleaner and easier to read. Is there any reason I should be using one method or the other?

    I'm looking for best practices.

    Thanks. :)
     
    ChaosFoo, Jan 28, 2008 IP
  2. kreoton

    kreoton Peon

    Messages:
    229
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    2nd method is good also but in HTML standard tag attributes should be quoted by double quotes...
    try this method:
    $pCRV .= '<input type="hidden" name="add" value="1">'."\n";
    PHP:
     
    kreoton, Jan 28, 2008 IP
  3. ChaosFoo

    ChaosFoo Peon

    Messages:
    232
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for your help. This brings up a slightly different question. Can't you use escape characters "\n" inside the statement like this:

    $pCRV .= '<input type="hidden" name="add" value="1">\n';
    Code (markup):
    Or is there some specific reason you placed it on the outside of the original statement?

    I'm just trying to make my code as clean and easy to read as possible, or follow the generally accepted best practices.

    Thanks.
     
    ChaosFoo, Jan 28, 2008 IP
  4. mrmaypole

    mrmaypole Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No, you cannot use \n inside single quotes. Well, you *can* but you'll get a literal \n.
    There is an argument that you should use single quotes wherever possible; PHP will evaluate any string delimited with double quotes to see if there are variables in it. With single quotes PHP doesn't have to do this. So '<input type="hidden" ...>'."\n"; is maybe the best solution there. Arguable.
     
    mrmaypole, Jan 28, 2008 IP
  5. walkere

    walkere Active Member

    Messages:
    112
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Why? The standards only state that attributes need to be quoted. It makes no distinction between single and double quotes.

    I personally use single or double quotes in php depending on whether or not I want it to look for a variable. In a short string, I'll use double quotes and include the variable(s) directly, rather than use a bunch of string concatenations.

    Otherwise I used single quotes, since it saves php the trouble (and performance hit) of scanning the string for variables to insert.

    If I'm echoing html, I always use the opposite type of quote for the html attributes. So if my php string is double quoted, I'll use single quotes for html. If my php is single quoted, I'll use double quotes for html.

    It makes the code cleaner and easier to read. I hate using slashes to escape things if I don't have to.

    - Walkere
     
    walkere, Jan 28, 2008 IP
  6. ChaosFoo

    ChaosFoo Peon

    Messages:
    232
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #6
    So, let me make sure that I completely understand this. If I use double quotes, I don't have to use concatenations to include variables? I could do something like this correct?

    
    echo "I am $age years old.";
    
    Code (markup):
    It would then put the variable value in the string.

    If so, this will make my life a lot easier.

    EDIT: I just tried it out, and yes it does work. Thanks for all your help. My code will be a lot cleaner now.
     
    ChaosFoo, Jan 29, 2008 IP
  7. fairuz.ismail

    fairuz.ismail Peon

    Messages:
    232
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    yes if you use double quotes, you can put directly the variables inside the quotes.

    echo "My name is $name !!!";

    but if you use single quotes, you must do this;

    echo 'My name is' . $name . '!!!';

    goodluck
     
    fairuz.ismail, Jan 29, 2008 IP