Shrikant Dhayje
shriekdj

shriekdj

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

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

PHP Tutorials by shriekdj Series

Shrikant Dhayje's photo
Shrikant Dhayje
ยทApr 7, 2022ยท

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Preface
  • Hello World Program In PHP For CLI
  • Hello World Program In PHP For CLI with parsing php code under HTML
  • Hello World Program in PHP With Built-in Web Server.

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!";

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!

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>

Output Will Be Given Below On Terminal Not Browser

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

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 .

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.

Also Give you thoughts about this post in the comments and give reactions for Algorithm boost ๐Ÿ˜‰.

Thanks and Bye ๐Ÿ‘‹.

ย 
Share this