PHP Hello World
The default file extension for PHP is .php. The web server will recognize and execute the files with the .php extension as PHP files by default.
Hello World Script
Create a file named hello.php and save it in the root directory of your web server with the following content.
PHP Hello World Example
Run Example ››
<?php echo 'Hello World';
Now, enter your web server's URL in your browser, ending with /hello.php. As you are using a web server installed on PC, the full URL might be http://localhost/hello.php or http://127.0.0.1/hello.php.
If everything configures correctly, you will see "Hello World" message in your browser. Great! you have just created your first PHP web page!
If you do not see any output, browser prompt for a download, or you see PHP file as text, then one (or more) of the following could be the reason.
- PHP is not enabled in your server
- Something went wrong on configuring
- You haven't saved the file with the extension .php
What should you do now?
- Make sure your file is named as hello.php and saved in the root directory of your web server.
- Follow our installation guide again.
- Find an answer on the internet. Type your question in a search engine and find an answer in the websites that the search engine suggests.
- StackOverflow! It is a great community of developers where you can get help from. Before you ask a question there, mind searching if your question is already answered. If you find any answer that is written for a question similar to your one, try to follow the instructions given in the answer. If you don't find any related question, ask your own question adding the appropriate tags to it (ex: PHP, MYSQL, Apache, WAMP, etc.)
The Logic of the Localhost
After you install a server in your computer, it reserves the address http://localhost for itself. When you enter the address http://localhost/hello.php, the browser will send an HTTP request to the server in your computer. The server will find hello.php in its root directory. Then the server will send back a response after executing the PHP file.
If you didn't get everything mentioned here, don't worry. You will learn everything gradually.