PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP Object Oriented ProgrammingPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP Interfaces

PHP Hello World

Default Extension

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


<?php 
echo 'Hello World'; 

Run Example ››
Default root directories:
  • WAMP Server - C:\wamp\www\
  • XAMPP Server - C:\xampp\htdocs\
  • Linux OS - /var/www/
  • Mac OS - /htdocs/
  • 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!

    No Message?

    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.

    What should you do now?

    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.

    How does localhost work?
    How does localhost work?

    If you didn't get everything mentioned here, don't worry. You will learn everything gradually.

    Facebook Twitter