We support #BlackLivesMatters !
If you support us then follow us by clicking here and following us!

Hello World Program in PHP - Detailed Explaination

For all PHP related discussion, one of the most popular programming languages on the web.
Post Reply
admin
Site Admin
Posts: 44

Hello World Program in PHP - Detailed Explaination

Post by admin » Sun Nov 03, 2019 5:04 pm

The most widely used language construct to print output in PHP is echo:

Code: Select all

echo "Hello, World!\n";
Alternatively, you can also use print:

Code: Select all

print "Hello, World!\n";
Both statements perform the same function, with minor differences:
  • echo has a void return, whereas print returns an int with a value of 1
  • echo can take multiple arguments (without parentheses only), whereas print only takes one argument
  • echo is slightly faster than print
Both echo and print are language constructs, not functions. That means they do not require parentheses around their arguments. For cosmetic consistency with functions, parentheses can be included. Extensive examples of the use of echo and print are available elsewhere.

C-style printf and related functions are available as well, as in the following example:

Code: Select all

printf("%s\n", "Hello, World!");
PHP can be used to add content to HTML files. While HTML is processed directly by a web browser, PHP scripts are executed by a web server and the resulting HTML is sent to the browser.
The following HTML markup contains a PHP statement that will add Hello World! to the output:

Code: Select all

<!DOCTYPE html>
<html>
 <head>
 <title>PHP!</title>
 </head>
 <body>
 <p><?php echo "Hello world!"; ?></p>
 </body>
</html>
When this is saved as a PHP script and executed by a web server, the following HTML will be sent to the user's browser:

Code: Select all

<!DOCTYPE html>
<html>
 <head>
 <title>PHP!</title>
 </head>
 <body>
 <p>Hello world!</p>
 </body>
</html>

Post Reply