what is difference btw this two string??

Discussion in 'PHP' started by mahdi_fci3, Jul 28, 2010.

  1. #1
    what is difference between this two echo ??
    
    ////////////////string 1/////
    $name='Mahdi';
    echo " hello Mr".$name
    
    ////////////////string 2/////
    $name='Mahdi';
    echo " hello Mr $name ";
    
    PHP:
    and what is difference also

    
    $_POST['Username'];
    $_POST[Username];
    // where Username is text feild
    
    PHP:
     
    mahdi_fci3, Jul 28, 2010 IP
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Double quoted strings will expand variable names.

    In your first, you are concatenating the varaible onto the end of the string. In the second, the variable is expanded.

    See here for more details on strings / single and double quotes.

    http://php.net/manual/en/language.types.string.php


    In what context are you using the latter? I assume it should be double quoted?
     
    lukeg32, Jul 28, 2010 IP
  3. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #3
    string 1 should be
    echo " hello Mr".$name;
    PHP:
    missing the ; and would be the same as string 2 with a space after the Mr

    second one depends where it is

    if you are setting $blah = $_POST[username]; will give an error, however will be ok with echo "$_POST[username]";
     
    themullet, Jul 28, 2010 IP
  4. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Any variable that sits between double quotes will be interpolated with the value of said variable. If you have the variable name outside of the double quotes - you can append it onto the end of that string by using the concatenation operator. Like you have done in your example.

    My preferred method of coding is to always use single quotes in combination with the concatenation operator.
    My reasons for this is:
    • It executes faster, the interpreter doesn't have to check the string to see if any variables exist
    • It looks a lot cleaner and you can easily distinguish where the variables are in large strings
    • It's better for outputting HTML. Since it's written using double quotes when following W3C specifications
     
    Deacalion, Jul 28, 2010 IP
  5. mahdi_fci3

    mahdi_fci3 Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks all
    I asked this question only to know if one best than other because I see poeple who use single quoted and other poeple who use double quoted in echo "My name is $name" or echo "My name is ".$name" & $_POST["Name"] or $_POST['Name'] or $_POST[Name] , so I see there is no difference even in eficiency and speed
     
    mahdi_fci3, Jul 28, 2010 IP
  6. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Which one you use is down to preference really; if you are concerned about speed and efficiency (which, in most scenarios, will be negligable) then you will want to use a third way of doing it (as 'arguments', comma seperated) or use print with expandable variables. Otherwise, use whichever you feel more comfortable doing.

    I would still refer you here for further reading however and make your own mind up;
    http://php.net/manual/en/language.types.string.php
     
    lukeg32, Jul 28, 2010 IP
  7. mahdi_fci3

    mahdi_fci3 Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thank u lukeg32 for ur time
     
    mahdi_fci3, Jul 28, 2010 IP