Look at the code below
<?php
echo "$_SERVER[HTTP_HOST] <br />";
echo $_SERVER['HTTP_HOST'], "\n\n";
?>
both of the echo statements return the value of HTTP_HOST index from the superglobal array $_SERVER using the same technique? My question is what caused the difference of the syntax? I noticed the following differences:
- HTTP_HOST in the first echo statement is not encased in single quotes, it is contrary to the syntax I used in the second echo statement. I get the following error if I encase HTTP_HOST in single quotes for the first echo statement
Parse error: syntax error, unexpected ” (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\php-blog\simple-blog\server.php on line 2
- Why a comma is needed after [‘HTTP_HOST’] in the second echo statement while it is not needed in the first echo statement? I get the following error if I discard this comma.
Parse error: syntax error, unexpected ‘”\n\n”‘ (T_CONSTANT_ENCAPSED_STRING), expecting ‘,’ or ‘;’ in C:\xampp\htdocs\php-blog\simple-blog\server.php on line 3
I am new to programming, need guidance, help please.
Thank you!
Your first statement calls echo with only one argument.
The argument is a string which includes a variable.
When doing this you should use brackets to make sure php understands were your variable starts:
You could also concatenate strings with a dot:
The comma is just another way to write several echos at once:
is the same as
http://php.net/manual/en/function.echo.php