PHP Hello World: Write Your First Program with Code Example

php hello world

You always start with “Hello World” when you learn a new language. It keeps things simple. You see right away how PHP works with this example.

You need PHP installed and a server like Apache or Nginx. Or you can use the PHP built-in server.

PHP Hello World Code Example

Start with a basic text editor like Notepad or VS Code. Save your file with a .php extension—for example, hello.php.

You only need two lines to write your first PHP program. Here is the full code:

echo "Hello World!";

echo prints text to the screen. It prints Hello World! in this case.

So how do you run it in the Browser?

  1. Put your hello.php file in your web server’s root folder (like htdocs in XAMPP or www in WAMP).
  2. Open your browser and go to http://localhost/hello.php.

You will see the text Hello World! on the screen.

Run PHP Hello World in the Command Line with Example

  1. Open your terminal.
  2. Go to the folder where your hello.php file is.
  3. Run this command:
php hello.php

The terminal will print: Hello World!

PHP Hello World Program Embedded in HTM

You can mix PHP with HTML. Here is an example:

<!DOCTYPE html>
<html>
  <head>
    <title>Hello PHP</title>
  </head>
  <body>
    <h1>
      <?php echo "Hello World!"; ?>
    </h1>
  </body>
</html>

You will see the headline with the text “Hello World!” when you open this in a browser

Wrapping Up

You learned how to write and run your first PHP program. You saw how to use echo and where to place your file.

Here is a quick recap:

  • Use <?php to start PHP code.
  • Use echo to print output.
  • Save the file with a .php extension.
  • You can run it in a browser or terminal.
  • You can also embed it in HTML.

FAQs

Do I need to install a server to run PHP?
Yes, you need a web server like Apache or Nginx. You can also use PHP’s built-in server.

Can I run PHP without a browser?
Yes, you can run PHP in the command line with php filename.php.

What is the use of echo in PHP?
It prints output to the browser or terminal.

Can PHP run without HTML?
Yes, PHP can run alone. But on websites, you often mix PHP with HTML.

What file extension should I use for PHP?
Use .php. Files with this extension can include and run PHP code.

What if I see the PHP code in the browser instead of the output?
It means the server is not processing PHP. Make sure you run the file on a server.

Similar Reads

PHP htmlspecialchars Function: Prevent XSS in HTML Output

A PHP script can break the page or allow code injection if it outputs user input directly into HTML. The…

PHP OR Operator: Using || and or in Conditional Statements

The PHP OR operator has two renditions: either || or or. Both of these are useful logical operators when you want to introduce…

Concatenating Strings in PHP: Tips and Examples

In PHP, string operators, such as the concatenation operator (.) and its assignment variant (.=), are employed for manipulating and…

PHP array_intersect_ukey Function: How it Works with Examples

The array_intersect_ukey is used to compare arrays by keys with a custom function in PHP. It returns matches by keys…

PHP array_diff_uassoc: How to Compare Arrays with Keys

PHP array_diff_uassoc compares arrays by values and keys. You can also pass a custom function to compare keys in your…

Get Last Insert ID in PHP with MySQL PDO and MySQLi

Keeping track of the last record inserted is often important. Whether you are managing user registrations, processing orders, or handling…

How to Update Documents in MongoDB Using PHP

Updating documents in MongoDB with PHP involves using the updateOne or updateMany methods from the MongoDB PHP library. These methods allow for precise updates,…

PHP Variable Scope: Local, Global & Static

The variable scope in PHP refers to the variables, functions, and classes that can be accessed within different parts of…

Traits in PHP: What They Are & Examples

PHP introduced traits to solve a problem with code reuse. Before traits, developers used inheritance and interfaces to share functionality…

PHP Class Destructor: How It Works with Examples

A destructor in a PHP class runs when an object is no longer needed. It frees resources and cleans up…

Previous Article

How to Remove the Last Character from a String in PHP

Next Article

PHP strpos Function: How it Works with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.