- PHP Basics
- PHP - Introduction
- PHP - Install
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo or Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP – Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Advanced
- PHP - Operators
- PHP - Control Statements
- PHP - Arrays
- PHP - Functions
- PHP - Superglobals
- PHP - File Handling
- PHP- Object Oriented Programming
- PHP - AJAX
- PHP - Web Development
- PHP - Enums
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
-
Create a PHP File:
- Open your text editor or IDE.
- Create a new file and save it with a
.phpextension, e.g.,hello.php.
-
Write the PHP Code:
- Add the following code to your
hello.phpfile:
- Add the following code to your
php<?php
echo "Hello, World!";
?>
- 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.phpfile in the web server's root directory (e.g.,htdocsfor 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:
-
Navigate to the Directory:
- Open a terminal or command prompt.
- Navigate to the directory where your
hello.phpfile is located.
-
Start the Built-in Server:
- Run the following command:
shphp -S localhost:8000
- Access the Script in a Browser:
- Open a web browser and navigate to
http://localhost:8000/hello.php.
- Open a web browser and navigate to
You should see the same "Hello, World!" message displayed in your browser.
Full Example
Here is the complete process in one place:
- Create a file named
hello.php. - Write the following code in
hello.php:
php<?php
echo "Hello, World!";
?>
- Run the PHP built-in server:
shphp -S localhost:8000
- 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.