Beginner php PHP 15 min read

A Local PHP Installation

A Local PHP Installation

Getting Started: Your First PHP Output

Online coding environments like Replit offer a quick way to experiment with PHP. When you run a PHP file in Replit, the code within special tags – <?php and ?> – is treated as PHP instructions, distinct from any surrounding HTML. Clicking the "Run" button initiates a web server within Replit that processes your PHP code and displays the result. Instead of seeing output in the console, you’re now likely to see "Hello World" rendered as a simple webpage within the Webview tab.

![Viewing the index .php script output in the Replit Webview panel](image_placeholder.png) A visual representation of the output in the Replit Webview panel

Replit temporarily publishes your code to a *.replit.dev address, allowing you to view your webpage outside of the Replit environment. You can access this by clicking the address bar at the top of the Webview panel, copying the provided URL, and pasting it into a new browser tab. This demonstrates a basic PHP webpage hosted independently. Consider this your first step towards building PHP-powered websites!

Important Note: While Replit is convenient for initial learning, more advanced projects later in this guide might require additional configuration within Replit. Details on these configurations can be found in Appendix C.

Why Work Locally? Setting Up Your Own Environment

While cloud-based editors like Replit are helpful, they can be slow, have limitations on free tiers, and depend on a stable internet connection. Many developers prefer the flexibility and speed of working locally – directly on their own computers.

The first step towards local development is installing PHP itself. If you haven't already, consult Appendix A for detailed instructions on downloading and installing the latest version of PHP appropriate for your operating system (Windows, macOS, or Linux).

Once PHP is installed, you're ready to set up a development environment. A crucial part of this is using an Integrated Development Environment (IDE). An IDE is more than just a text editor; it’s a powerful tool that provides features like a built-in terminal, advanced search and replace capabilities, automated code checking, and even suggestions for common coding patterns. Think of it as your coding command center! There are many excellent IDEs available, so explore and find one that suits your workflow.

Setting Up Your Local PHP Development Environment with PhpStorm

To effectively learn and build PHP applications, it's crucial to have a development environment on your computer. This section will guide you through setting up a local environment using PhpStorm, a powerful and widely-used Integrated Development Environment (IDE) from JetBrains. PhpStorm offers a 30-day free trial, and various groups, including students, educators, and participants in coding bootcamps, can often qualify for free licenses. You can download PhpStorm from [https://www.jetbrains.com/phpstorm/](https://www.jetbrains.com/phpstorm/) and follow the provided installation instructions.

While PhpStorm is highly recommended, alternative free IDEs like Visual Studio Code, Eclipse, and Apache NetBeans are also viable options and offer plugins to support PHP development.

Your First PHP Script: "Hello, world!"

Let's create a basic "Hello, world!" PHP script using PhpStorm. This example mirrors the functionality of the default PHP CLI template found on platforms like Replit.

  1. Launch PhpStorm.
  2. Select "New Project."
  3. Choose "PHP Empty Project" as the project template.
  4. Specify a location on your computer to store the project files, and give your project a descriptive name. It's good practice to include a forward slash ( / ) before the project name in the location path (e.g., /my_php_project).
  5. Click "Create."

PhpStorm will then generate a new folder with your chosen project name in the designated location. This folder will house all project files. As your projects grow in complexity, you'll likely use subfolders to organize different aspects of your application, such as source code, data files, and configuration settings.

After creation, PhpStorm will display the project editing view, providing a workspace for your code. This view typically consists of three main panels:

  • Project Contents Panel: Located in the top-left, this panel displays the folder structure of your project, allowing you to navigate and manage files.
  • Code/File Editing Panel: Situated in the top-right, this is where you’ll write and edit your PHP code and other data files.
  • Command Line Interface (CLI) Terminal: To access a command line terminal, click the Terminal icon (often represented as >_) in the left-hand column of the PhpStorm window. This terminal will appear at the bottom of the application window, allowing you to execute commands directly within your development environment.

Running Your First PHP Script

Now that your local PHP environment is set up, let’s create and execute a simple program. This will verify that everything is working correctly and introduce you to the basic workflow.

First, create a new PHP file within your project. In your IDE (like PhpStorm), navigate to your project folder in the file explorer panel. Then, select File > New > PHP File from the menu bar. Name the file hello – the IDE will automatically add the .php extension, so the full filename will be hello.php. Once created, the file should appear in the project's file listing and open for editing. You're likely to find that the editor has already populated the file with the opening PHP tag: <?php.

Now, add the following code into the hello.php file:

<?php
print "Hello, world!\n";

This code is straightforward: it prints the phrase "Hello, world!" followed by a newline character. The newline ensures that the output appears on its own line in the terminal.

print vs. echo

You may have noticed that the code uses print instead of echo to display the text. While both print and echo are used to output text in PHP, they are largely interchangeable for beginners. Think of them as functionally equivalent for now. Some older PHP codebases, or those written by programmers familiar with older PHP versions, might favor echo. However, print arguably better reflects the action of displaying text. Ultimately, the choice between them is a matter of personal preference.

A key difference (though not crucial for now) is that in many programming languages, output functions require parentheses around the text to be displayed. For example, in Python, you might see print("Hello"). In PHP, however, print and echo are language constructs, not functions, so parentheses are optional. You can use them, but you don’t need to.

Executing Your Script

To run your newly created script, open the terminal panel within your IDE. If the terminal isn’t already open, you may need to explicitly open it from the IDE's menu. In the command line, type the following and press Enter:

php hello.php

This command tells the PHP interpreter to execute the hello.php file. You should then see the output, "Hello, world!", displayed in the terminal window. Congratulations, you've just run your first PHP program!

Running Your PHP Code: Multiple Methods & a Local Web Server

PhpStorm offers a couple of convenient ways to run your PHP scripts directly within the development environment. Besides the debug functionality, you can execute a script by clicking the "Run" button, represented by a green arrow, located near the top-right corner of the application window. When you do this, PhpStorm will attempt to execute the currently open file. If a menu appears presenting options for running the script with either PHP or JavaScript, make sure to select the PHP option.

Executing a script in this manner will typically display a "Run" panel at the bottom of the screen. This panel provides valuable information, including the specific PHP engine being used and the full path to the executed script. This is particularly helpful if you have multiple PHP versions installed on your system, allowing you to verify compatibility across different environments. The panel will also show the program's output and a final exit code. An exit code of 0 generally signifies that the script completed successfully.

Setting Up a Local Web Server with PHP

When you install PHP, you also receive a built-in web server, designed primarily for local development and testing. A simple way to confirm that this web server is functioning and to gather details about your PHP installation is to utilize the phpinfo() function. This function generates a detailed HTML report containing a wealth of information about the PHP environment, such as the version number, configured extensions, and server settings.

To get started, create a new project within PhpStorm (or your preferred IDE). A suitable folder name for this project could be web_project_1. Within this project, create a new file named index.php. The name index.php is significant because it designates this file as the default document served by the web server when no specific file is requested.

Here's the code you'll place inside index.php:

<?php
phpinfo();
?>

This simple script calls the phpinfo() function and displays its output. You can view this information as a formatted web page by executing the script through your web browser. Within PhpStorm, you can use the "View" menu, selecting "Open in Browser" and then "Built-in Preview," or you can click the dedicated icon that appears when your cursor is positioned within the file editor. This will automatically launch the index.php file in your default web browser, showing the comprehensive phpinfo() report.

Running Your PHP Code with the Built-in Web Server

One of the simplest ways to test your PHP code is by using PHP’s built-in web server. This method is particularly useful when you're just starting out or don't have a more complex web server environment set up. Integrated development environments (IDEs) like PhpStorm often provide a convenient preview feature to display your PHP output directly within the IDE. If you’ve launched the preview, you should see the results of your index.php script rendered in a browser window.

You can explore the information displayed by running phpinfo(). This function provides a wealth of details about your PHP installation, including the PHP version, the location of the configuration file (php.ini), which database extensions are enabled, and the names of key contributors to the PHP project. It's a great way to get acquainted with your system’s PHP setup.

While the IDE preview is handy, you can also view your script's output in a standard web browser like Chrome or Firefox. This is often the only option if you’re using a different development environment. To do this, open the IDE's terminal (or a terminal on your system) and execute the following command:

php -S localhost:8000

Let’s break down what this command does. php invokes the PHP interpreter. The -S flag tells PHP to start its built-in web server. localhost:8000 specifies the address and port where the server will be accessible. localhost refers to your own computer, and 8000 is the port number. Every application that needs to communicate over a network requires a unique port number—think of them as specific mailboxes within a larger post office. For local development, ports 8000 and 8080 are common choices; production web servers typically use port 80. I personally prefer using port 8000 for local development.

Once the server is running, simply open your web browser and enter localhost:8000 into the address bar. You'll see the output of your PHP script, just as you would if you were using a full-fledged web server.

To stop the built-in web server, return to the terminal window where you launched it and press Ctrl+C. This gracefully terminates the server process.

And that’s it! You’ve successfully launched and used PHP’s built-in web server to test your code. This is a fundamental skill for any PHP developer.