DEV Community

Cover image for PHP - Introduction by shriekdj | How to write Hello World Program in PHP by shriekdj
Shrikant Dhayje
Shrikant Dhayje

Posted on • Originally published at codewithshriekdj.netlify.app on

PHP - Introduction by shriekdj | How to write Hello World Program in PHP by shriekdj

Preface

For Becoming Back-end Web Developer in PHP You Should At least Know How to Create HTML Files and also know what are the HTML files are.

Also PHP Should Be Already Installed on your PC, Device, Server, Sandbox or Virtual Machine ( Whatever You are using Maybe Toaster Also 😅 Don't Know What will Be Machine ).

The Given Post is about PHP 8 and Above.

PHP Code is created under an HTML Template. Actually PHP is Used like additional element of HTML.

In Short PHP Code Embeded Under HTML Code.

Creating Hello World Program In PHP.

There are many ways of Running PHP At Many Places As of Now.


Hello World Program In PHP For CLI

First Create an Text File index.php with any text edito and Type the Text Given Below.

<?php
  echo "Hello, World!";

Enter fullscreen mode Exit fullscreen mode

PHP Code Start With <?php and Ends With ?> .

But At here we don't need and shouldn't add the Closing Tag of PHP, Because It is an Pure PHP without embedding PHP into HTML Tags otherwise it may give you an Error.

For Running PHP Code In Terminal

user123@ubuntu:~/ php index.php
Hello, World!
Enter fullscreen mode Exit fullscreen mode

As you can see it prints Hello World On Command Line, Shell or Terminal


Hello World Program In PHP For CLI with parsing php code under HTML

Just Change Your index.php file's content to Given Below

<html>
<body>
  <?php echo "<p> Hello, World! </p>"; ?>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output Will Be Given Below On Terminal Not Browser

user123@ubuntu:~/ php index.php
<html>
<body>
  <p> Hello, World! </p>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Here you can see there is an ?> for closing the PHP Code, Because it is not an Pure PHP file and it Contains Mixture of HTML and PHP.

But PHP part is Parsed as per the HTML File it Needed.

Closing Tag of PHP is required in Non Pure PHP File.


Hello World Program in PHP With Built-in Web Server.

For running under the built-in server we just have add some flags to our php commands.

  • -S <addr>:<port> : Run with built-in web server. ( It's Capital S not Small s. ).
  • -t <docroot> : Specify document root <docroot> directory for built-in web server. For Current directory use . as <docroot>

Run the Given Code With Following Commands.

php index.php -S 127.0.0.1:8000 -t .
Enter fullscreen mode Exit fullscreen mode

Output wil be given below if we open http://127.0.0.1:8000 or http://localhost:8000 in our web browser.

Hello World Program Running on localhost:8000 under PHP

In PHP You don't need to Specify the file name if it is index.php, because if you don't specify the filename it will by default check for the index.php file in the directory.


This built in server is for only running php code, so if you want to run something like mysql, apache with it then you will need LAMP or XAMP server on PC or Web server

In Short this built-in server should be used for development environments only.

After a long I Wrote the Blogpost this Long Size, But I Just Wanted to share the details.


Conclusion

If you found this useful then please share this and follow me! Also check out Buy Me A Coffee if you want to support me on a new level!

Buy me a coffee

Also Give you thoughts about this post in the comments and give reactions for Algorithm boost 😉.

Thanks and Bye 👋.

Top comments (0)