Hello World Master

Tutorials, articles and quizzes on Software Development

PHP > Articles

Hello world in PHP

Setting up a PHP environment

There are many ways to set up a PHP environment. You could use WAMP/MAMP, but there are other ways to set up a PHP environment.

Boilerplate for Hello world

To run our hello world in PHP, we’ll need to set up an index.html file. We could always run PHP through the command line, but having the results of our PHP code through the browser better demonstrates its capabilities as a server side programming language used in Web development.

<!-- index.html -->
<html>
 <head>
 </head>
 <body>
 </body>
</html>

Change index.html to index.php

We’ll need to change the extension of our index file to .php. This will allow whichever server we’re on to recognize the PHP inside our PHP and render it on its server before its served to us.

Adding our inline PHP in our HTML

Normally, we want to avoid inlining PHP as much as we can for code cleanlieness. We do it here for learning purposes.

PHP is a tempting language, which means you can use it to print out information in your markup. Lets add a PHP tag inside our body tag

<!-- index.html -->
<html>
 <head>
 </head>
 <body>
  <?php
  ?>
 </body>
</html>

Now we can write PHP code inside our PHP tag.

Printing text on the screen with PHP

Now that we’ve set our PHP tag, we can print out text using the echo function.

<?php
  echo "Hello world";
?>

lets add this into the PHP tag inside our index.php file

<!-- index.html -->
<html>
 <head>
 </head>
 <body>
  <?php
    echo "Hello world";
  ?>
 </body>
</html>

Now you should see hello world print on the screen!

But what if we want to put our hello world text inside of an html tag like h1? We would just need to add h1 tags before and after “Hello world”

<?php
    echo "<h1>Hello world</h1";
?>