Question on $_SERVER['PHP_SELF']

Discussion in 'PHP' started by cpthk, Dec 26, 2007.

  1. #1
    I am trying to have this php code post variable to itself.
    Here is my code:
    <?
    	echo "<form action=\"$_SERVER['PHP_SELF']\" method=\"post\"></form>";
    	
    ?>
    Code (markup):
    I got a error from that: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/cpthk/public_html/project/test.php on line 2

    And if I change to:
    <?
    	$self = $_SERVER['PHP_SELF'];
    	echo "<form action=\"$self\" method=\"post\"></form>";
    	
    ?>
    Code (markup):
    Then if works fine. I realized everytime I have $_SERVER['PHP_SELF'] with the double quote"" at the sides, it will have problem. Even if I do <? echo "$_SERVER['PHP_SELF']"; ?> What wrong with my coding?

    Thanks.
     
    cpthk, Dec 26, 2007 IP
  2. coches

    coches Peon

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?
    echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post"></form>';

    ?>
    your error is that u didnt outquote the variable , also i have validated the html:)
     
    coches, Dec 26, 2007 IP
  3. cpthk

    cpthk Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    But why do I need to outquote the variable?

    For example:
    <? $i = 10;
    echo "i equals to $i"; ?>
    Code (markup):
    In this example, I didn't outquote the variable either. But this one works fine. Why? Isn't the double quote for the whole line works?
     
    cpthk, Dec 26, 2007 IP
  4. harsh789

    harsh789 Member

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #4
    This is coz the way PHP parse variable inside the double quoted string. If a dollar sign ($) is encountered in the string, the parser will greedily take as many tokens as possible to form a valid variable name.

    So if you remove single quotes(') around PHP_SELF then it will work fine. for example below code will work.
    echo "<form action=\"$_SERVER[PHP_SELF]\" method=\"post\"></form>";
    PHP:
    And if you use following code it will give parser error.
    echo "<form action=\"$_SERVER['PHP_SELF']\" method=\"post\"></form>";
    PHP:
     
    harsh789, Dec 26, 2007 IP
  5. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #5
    u r using single quotes in double quotes

    I prefer this ways allways

    <?
    echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post"></form>';

    ?>


    Regards

    Alex
     
    kmap, Dec 26, 2007 IP
  6. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #6
    Agreed, always use singles when you can.
     
    HuggyStudios, Dec 26, 2007 IP
  7. kendo1979

    kendo1979 Peon

    Messages:
    208
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    what's the reason behind this? can someone explain?
     
    kendo1979, Dec 26, 2007 IP
  8. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It's mostly for perfomance reasons, the content between single quotes is not being parsed at all, it's being echoed as it is. Another small benefit is that you don't need to escape double quotes, when inside single quotes statement, this is useful when echoing html text, etc...
     
    hogan_h, Dec 26, 2007 IP
  9. cpthk

    cpthk Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for helping.
    Now I figured out:

    
    $i['test'] = 'cool';
    
    echo 'I am '.$i['test'];//ok
    echo "I am $i[test]";//ok
    echo "I am $i['test']";//error
    echo "I am 'cool' ";//ok
    
    Code (markup):
     
    cpthk, Dec 26, 2007 IP
  10. clinton

    clinton Well-Known Member

    Messages:
    2,166
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    110
    #10
    try this
    use the { and }'s

    echo "blah blah {$_SERVER['xxx']}blah blah blah";
    Code (markup):
     
    clinton, Dec 26, 2007 IP
  11. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #11
    Took the words from my mouth.

     
    HuggyStudios, Dec 27, 2007 IP