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.
<? echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post"></form>'; ?> your error is that u didnt outquote the variable , also i have validated the html
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?
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:
u r using single quotes in double quotes I prefer this ways allways <? echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post"></form>'; ?> Regards Alex
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...
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):