Writing your first PHP script is simple and requires just a few lines of code. Follow the steps below to get started.
Step 1: Create a PHP File
Step 1: Create a PHP File
Open a text editor and create a new file with a .php
extension, for example, first_script.php
.
Step 2: Write Your PHP Code
Add the following PHP code to your file:
<?php echo "Hello, World!"; ?>
Step 3: Run Your PHP Script
To execute the script, save the file in your web server’s root directory (e.g., htdocs
for XAMPP or www
for WAMP) and access it via your browser:
http://localhost/first_script.php
Expected Output
When you run the script in a browser, you should see the following output:
Hello, World!
Understanding the Code
<?php ... ?>
: This is the opening and closing tag for PHP code.echo "Hello, World!";
: This command outputs text to the browser.