🏆 OSM

Getting Started with PHP: From Installation to Your First Script

1 week ago
2025-08-18 11:08:22

If you're looking to dive into web development, PHP (Hypertext Preprocessor) is a great language to start with. It's a powerful, server-side scripting language that's used to create dynamic and interactive web pages. Unlike client-side languages like JavaScript, which run in the user's browser, PHP code is executed on the web server. The server then sends the resulting HTML to the user's browser. This is why PHP is so widely used for building everything from simple websites to complex applications like Facebook and WordPress.

Setting Up a Local Development Environment

Before you can write and run PHP code, you need a local web server. This allows you to test your scripts on your own computer without needing to upload them to a live server. The easiest way to do this is by installing a package that bundles a web server (like Apache), a database (like MySQL), and PHP. XAMPP (for Windows, macOS, and Linux) and MAMP (for macOS and Windows) are two of the most popular options.

  1. Download and Install: Visit the official website for XAMPP or MAMP and download the version for your operating system. The installation process is straightforward—just follow the on-screen instructions.

  2. Start the Servers: Once installed, open the control panel or application. You'll need to start the Apache and MySQL services. This will make your computer function as a local web server.

  3. Find Your Web Root: The web root directory is where you'll save your PHP files. In XAMPP, this is typically the htdocs folder inside your installation directory. In MAMP, it's usually the htdocs folder.

The Basic Syntax: Writing Your First Script

Every PHP script must be enclosed within PHP tags. This tells the server to interpret the code inside the tags as PHP and not as regular HTML. The most common tags are <?php and ?>.

A simple PHP script looks like this:

<?php
    // Your PHP code goes here
?>

The server will ignore everything outside of these tags and treat it as standard HTML. This allows you to embed PHP code directly within an HTML file.

The echo and print Statements

To display output from your PHP script, you'll use the echo or print statements. While there are subtle differences in their use cases, for a beginner, they are functionally the same. They both output data to the browser. The echo statement is a bit faster and more commonly used.

Here's how you'd use the echo statement to display text:

<?php
    echo "Hello, World!";
?>

Running Your First "Hello, World!" Script

Now that your local server is set up and you know the basic syntax, let's create your first PHP file.

  1. Create a New File: Open a text editor (like VS Code or Sublime Text) and create a new file.

  2. Write the Code: Type the following code into the new file:

    <!DOCTYPE html>
    <html>
    <head>
        <title>My First PHP Page</title>
    </head>
    <body>
    
    <h1>Welcome!</h1>
    
    <?php
        echo "Hello, World!";
    ?>
    
    </body>
    </html>

  3. Save the File: Save the file as index.php inside your web root directory (the htdocs folder).

  4. View in Browser: Open your web browser and navigate to http://localhost/ or http://localhost:8888/ (if you're using MAMP). You should see the "Welcome!" heading followed by "Hello, World!" displayed on the page.

Congratulations! You've successfully set up your environment and run your first PHP script. From here, you can start exploring variables, data types, and more advanced concepts to build more complex applications.

© 2025 Oscar Myo Min

Design by Kai

𝕏 GitHub