Day 2: Setting Up the Environment
Installing a Local Server (XAMPP)
To get started with PHP development, we’ll set up a local server environment using XAMPP. XAMPP is a popular choice because it includes Apache (web server), MySQL (database), PHP, and Perl, making it easy to develop PHP applications locally.
Steps to Install XAMPP:
- Download XAMPP:
- Visit the Apache Friends website and download the latest version of XAMPP suitable for your operating system (Windows, macOS, Linux).
- Install XAMPP:
- Run the downloaded installer and follow the on-screen instructions. You may need administrative privileges to install XAMPP on your computer.
- Start the XAMPP Control Panel:
- After installation, open the XAMPP Control Panel. On Windows, you can usually find it in the Start Menu under XAMPP or by searching for “XAMPP Control Panel.”
- Start Apache and MySQL:
- In the XAMPP Control Panel, start the Apache and MySQL services by clicking the “Start” button next to each. Apache is the web server that will process your PHP scripts, and MySQL is the database server.
- Verify Installation:
- Open your web browser and type
http://localhost
in the address bar. You should see the XAMPP dashboard indicating that Apache and MySQL are running.
Writing and Running Your First PHP Script
Now that XAMPP is installed and running, let’s create a simple PHP script to verify everything is set up correctly.
- Create a New PHP File:
- Navigate to the
htdocs
directory inside your XAMPP installation folder. This is where you’ll store your PHP files.
- Open a Text Editor:
- Use a text editor like Notepad (Windows), TextEdit (macOS), or any code editor of your choice (Visual Studio Code, Sublime Text, etc.).
- Write Your First PHP Script:
- Create a new file named
index.php
(or any name ending with.php
) in thehtdocs
directory. - Add the following code:
php <?php echo "Hello, World!"; ?>
- Save the File:
- Save the
index.php
file in thehtdocs
directory.
- Run Your PHP Script:
- Open your web browser.
- Type
http://localhost/index.php
in the address bar. - Press Enter. You should see the output
Hello, World!
displayed on the page.
PHP Syntax and Basic Commands
- PHP Tags: PHP code is enclosed within
<?php
and?>
tags. - Statements: End each statement with a semicolon
;
. - Comments: Use
//
for single-line comments or/* */
for multi-line comments.
Example:
[mycode]
<?php
// This is a single-line comment
/* This is a multi-line comment */
echo “Hello, World!”; // Output: Hello, World!
?>
[/mycode]
Next Steps
Once you’ve verified that your PHP script runs successfully, you’re ready to move on to Day 3 where we’ll dive into the basics of PHP variables and data types. Let me know if you have any questions or if everything is clear so far!