The following code shows how to display the text "Hello, world!".
<?php echo "Hello, world!"; ?>/*from w w w. j a v a 2s . co m*/
Save this file as hello.php in your document root folder, and view the results in your browser by visiting http://localhost/hello.php.
The first line tells the PHP engine to expect some PHP code to follow:
PHP can be embedded within HTML Web pages.
When the PHP engine first starts processing the page, it assumes it's dealing with plain old HTML until told otherwise.
By using the PHP delimiter, <?php, you're telling the PHP engine to treat anything following the <?php as PHP code, rather than as HTML.
The next line displays the message " Hello, world! " :
echo "Hello, world!";
PHP's echo() statement takes a string of text - "Hello, world!" - and sends it as part of the Web page to the browser.
The semicolon ; at the end of the line tells PHP that you've reached the end of the current statement.
echo() can display anything that can be displayed, such as numbers or the results of expressions.
An alternative to echo() is the print() statement.
The final line of your simple script tells the PHP engine that it's reached the end of the current section of PHP code.
The lines followed (if any) contain plain HTML again: