Click to See Complete Forum and Search --> : <<<FORM declaration
ilProphet
09-24-2007, 04:25 PM
Can anyone explain what the "<<<FORM" and "FORM;" signify in"
echo <<<FORM
<form name="form1" method="post" action="index.php?id=upload&do=verify&session_code=$session_code">
<p><img src="scripts/captcha_image.php?img=$pic_url"></p>
<p>Displayed Code? <input type="text" name="password"></p>
<p><input type="submit" name="Submit" value="Überprüfen"></p>
</form>
FORM;
I've never seen this sort of declaration before
ray326
09-24-2007, 09:17 PM
It's called a "here document". It means copy the text between this point and the (defined) label below to standard output. It's often used when producing an HTML document on the fly with Perl. It can be found in various syntaxes on other platforms.
ilProphet
09-26-2007, 01:51 PM
standard output in this case being html?
NogDog
09-26-2007, 09:55 PM
Presumably this is part of a PHP or Perl script, or some other language that supports a similar C-like syntax. The "echo" means to output what follows to the standard output, which in a web application means to the buffer that the web server will use to output to the client via HTTP (or simply put: it outputs it to the browser). The "<<<FORM" means that the argument to "echo" (i.e., the text to be output) is all of the following lines until the string "FORM;" is encountered on a line by itself (with no leading spaces/tabs, by the way).
This has nothing to do with HTML syntax; it has to do with the scripting language being used to generate the HTML output.
felgall
09-27-2007, 01:25 AM
There are a number of languages that support the
echo <<<whatever you like
some content here
whateveryoulike
format for outputting a large volume of data
I think it is more useful in Perl than PHP since in PHP it is easier to do
?>
some content here
<?php
ilProphet
09-27-2007, 09:30 AM
so standard output means output that the browser can understand or display
TJ111
09-27-2007, 09:44 AM
Php or Perl scripts are performed on the server, and their output is passed to the client, or browser(usually). So standard output would be the information passed from server to client, whether it be html or otherwise. So in this case, it would be output that the browser can understand and display (html).
ilProphet
09-27-2007, 11:01 AM
thanks that makes more sense now:)
ray326
09-27-2007, 10:31 PM
In the Unix process model each program by default reads its input from a stream of characters called stdin (standard in) and writes to a similar stream called stdout (standard out). A CGI program is spawned with stdin connected to the incoming HTTP request and stdout connected to the stream the web server will send back to the browser (per tj111 except it isn't really just HTML, it's the HTTP response which almost always includes an HTML document).
The beauty of a here document is that it tends to make the source and the intent of the source a lot more readable compared to most alternatives.
NogDog
09-27-2007, 11:54 PM
Perhaps it's easiest to just think of it as another way of quoting a string literal. The following are all functionally equivalent in PHP (though there will be some minor performance differences):
echo "The result is: $result";
echo 'The result is: ' . $result;
echo <<<QUOTE
The result is: $result
QUOTE;
printf('The result is: %s', $result);
Which to use comes down to which makes the code more readable in a given situation for you, influenced by your personal preferences and coding habits, and possibly modified by performance considerations in situations where saving microseconds is more important than readability.
ray326
09-28-2007, 03:20 PM
Yes, it needs to be used where it works best. One liners are definitely not the best use. Typically it will be used for things like a couple of dozen lines that make up the start of a page and the same for the bottom with the onezie-twozie stuff in between handled in another way.