Writing First Script Estimated reading: 4 minutes 4 views Contributors After getting familiar with the core syntax rules of PHP, the next logical step is to write your very first PHP script. Creating your initial script allows you to understand how PHP code is structured, executed, and displayed in a browser or terminal. This lesson guides you through the process of writing, saving, and running a simple PHP script from scratch. Setting Up Your First PHP Script Before you start coding, ensure that PHP is correctly installed and configured on your system. If you haven’t set up PHP yet, refer to the PHP installation guide for step-by-step instructions. Once PHP is installed, you are ready to begin scripting. Understanding the PHP File Structure PHP scripts are plain text files saved with the .php extension. Every PHP script begins with a specific tag that tells the server to process the enclosed code as PHP. The most common way to start and end a PHP script is as follows: <?php // Your PHP code goes here ?> All executable PHP code must reside within these tags. Any code outside them will be treated as plain HTML or ignored. Writing a Basic “Hello, World!” Script The traditional way to learn a new programming language is by printing “Hello, World!” to the output. Let’s create this classic example using PHP: <?php echo "Hello, World!"; ?> Here’s what each part does: <?php … ?>: Marks the start and end of PHP code. echo: A statement used to output text to the browser. “Hello, World!”: The string that will be displayed. ;: Every PHP statement must end with a semicolon. Saving and Running Your Script To see your script in action, follow these steps: Open a text editor and paste the PHP code into a new file. Save the file as hello.php in your web server’s document root or a suitable folder. If you are using a web server like Apache or Nginx, ensure it’s running and supports PHP. Open your web browser and navigate to http://localhost/hello.php (adjust the path as necessary). You should see the phrase Hello, World! displayed on the page. If using the command line, you can run: php hello.php This will output Hello, World! in the terminal window. Common Mistakes to Avoid Omitting the semicolon at the end of PHP statements. Saving the file with an incorrect extension (such as .txt instead of .php). Missing the PHP opening <?php or closing ?> tags. Placing the PHP file outside the web server’s document directory, which leads to an HTTP 404 error. Expanding Your Script: Adding HTML PHP scripts can be combined with HTML to create dynamic web pages. Here’s an example that outputs HTML alongside PHP: <!DOCTYPE html> <html> <head> <title>My First PHP Script</title> </head> <body> <h1>Welcome!</h1> <p><?php echo "This page is generated by PHP."; ?></p> </body> </html> In this example, the HTML and PHP coexist in a single file, allowing you to create interactive or data-driven websites. Key Takeaways PHP scripts must use .php as their file extension. All PHP code is enclosed between <?php ... ?> tags. You can output text or HTML using statements like echo. Running your script through a web server or terminal reveals the output. Combining PHP and HTML unlocks the power of dynamic content. Next Steps Having successfully written and executed your first script, you are now ready to explore more PHP features. Continue with the next lessons to learn about PHP variables, data types, and how to handle user input. Practice writing simple scripts to reinforce your understanding and build a strong foundation in PHP programming. Writing First Script - Previous PHP Syntax Rules Next - Writing First Script PHP Variables Usage