techmore.in

PHP Hello World

Creating a "Hello, World!" script in PHP is simple and a great way to get started with PHP programming. Here’s how you can do it:

Basic "Hello, World!" Script

  1. Create a PHP File:

    • Open your text editor or IDE.
    • Create a new file and save it with a .php extension, e.g., hello.php.
  2. Write the PHP Code:

    • Add the following code to your hello.php file:
php
<?php echo "Hello, World!"; ?>
  1. Run the PHP Script:
    • You need a web server with PHP installed to run your PHP script. You can use Apache with PHP (such as XAMPP, WAMP, MAMP, or a LAMP stack) or a built-in server for testing.
    • Place your hello.php file in the web server's root directory (e.g., htdocs for XAMPP).
    • Open a web browser and navigate to http://localhost/hello.php.

You should see the output:

Hello, World!

Using PHP Built-in Server

PHP comes with a built-in web server that you can use for development purposes. Here's how to run your script using the built-in server:

  1. Navigate to the Directory:

    • Open a terminal or command prompt.
    • Navigate to the directory where your hello.php file is located.
  2. Start the Built-in Server:

    • Run the following command:
sh
php -S localhost:8000
  1. Access the Script in a Browser:
    • Open a web browser and navigate to http://localhost:8000/hello.php.

You should see the same "Hello, World!" message displayed in your browser.

Full Example

Here is the complete process in one place:

  1. Create a file named hello.php.
  2. Write the following code in hello.php:
php
<?php echo "Hello, World!"; ?>
  1. Run the PHP built-in server:
sh
php -S localhost:8000
  1. Open a web browser and go to http://localhost:8000/hello.php.

This will display:

Hello, World!

This simple example demonstrates how to create and run a basic PHP script. From here, you can explore more complex PHP functionality and start building dynamic web applications.