Your First PHP Script: Hello, World!
- Published on
Introduction
The traditional 'Hello, World!' program serves as a universal introduction when learning a new programming language. In PHP, creating a 'Hello, World!' script not only introduces you to the syntax but also the process of writing, saving, and executing PHP scripts on a server.
Writing Your First PHP Script
PHP scripts are generally saved with a .php
extension and can be embedded within HTML. Here's how a simple 'Hello, World!' script looks in PHP:
<?php
echo "Hello, World!";
?>
In this script:
<?php
: Indicates the start of PHP code.echo
: Is used to output data."Hello, World!"
: Is the string that will be output to the browser.?>
: Indicates the end of PHP code.
Setting Up a Local Server
To run your PHP script, you need a server environment. For beginners, using software like XAMPP, MAMP, or WampServer is recommended, as they provide an easy-to-use local server environment.
Steps to Run Your PHP Script using XAMPP:
- Install XAMPP: Download and install XAMPP from the official website.
- Start the Servers: Open XAMPP and start the Apache server.
- Save Your Script: Save your PHP script in the
htdocs
folder inside the directory where XAMPP is installed. For instance, if your script is namedhello.php
, save it asyour-xampp-directory/htdocs/hello.php
. - Access Through Browser: Open your preferred web browser and type
localhost/hello.php
in the address bar.
Viewing the Output
Upon executing the above steps, you should see "Hello, World!" displayed on your web browser, indicating that your PHP script has successfully run.
Moving Forward
With your first PHP script under your belt, you can progress to more complex PHP code, exploring variables, operators, loops, functions, and eventually creating dynamic web pages. Remember, practice is key to mastering programming, so continue to write and experiment with various PHP scripts.