In this lesson, we will write our first PHP script. This will help you understand the basic structure of a PHP file and how to execute it in your development environment.
Prerequisites
Before you start, ensure you have:
- A web server with PHP installed (e.g., XAMPP, WAMP, MAMP, or a live server).
- A text editor or an Integrated Development Environment (IDE) like VS Code, Sublime Text, or PHPStorm.
Writing Your First PHP Script
Step 1: Create a PHP File
- Open your text editor or IDE.
- Create a new file and save it as
first_script.php.
Step 2: Write the PHP Code
In your first_script.php file, type the following code:
Explanation
<?php: This tag opens a PHP block. All PHP code must be written inside these tags.echo "Hello, World!";: This statement outputs the text "Hello, World!" to the browser. Theechocommand is used to display text or variables.// This is a single-line comment: Comments are used to explain the code and are ignored by the PHP engine. Single-line comments start with//.?>: This tag closes the PHP block. It is optional if the PHP block is at the end of the file.
Step 3: Run Your PHP Script
- Save the
first_script.phpfile in the root directory of your web server (e.g.,htdocsfor XAMPP). - Open your web browser and navigate to
http://localhost/first_script.php.
You should see the text "Hello, World!" displayed on the page.
Practical Exercise
Task
Create a PHP script that displays your name and the current date.
Solution
- Create a new file and save it as
display_name_date.php. - Write the following code:
<?php
// Displaying name
echo "My name is John Doe.<br>";
// Displaying current date
echo "Today's date is " . date("Y-m-d") . ".";
?>Explanation
echo "My name is John Doe.<br>";: This statement outputs the text "My name is John Doe." followed by an HTML line break (<br>).date("Y-m-d"): Thedatefunction returns the current date in the formatYYYY-MM-DD.
Running the Script
- Save the
display_name_date.phpfile in the root directory of your web server. - Open your web browser and navigate to
http://localhost/display_name_date.php.
You should see the text "My name is John Doe." followed by the current date.
Common Mistakes and Tips
- Forgetting the PHP tags: Ensure your PHP code is enclosed within
<?phpand?>tags. - Syntax errors: PHP statements end with a semicolon (
;). Missing semicolons can cause errors. - File location: Make sure your PHP file is saved in the correct directory of your web server.
Summary
In this lesson, you learned how to:
- Create a PHP file.
- Write and run a simple PHP script.
- Use the
echostatement to display text. - Use the
datefunction to display the current date.
Next, we will dive deeper into PHP syntax and variables, which are fundamental for writing more complex scripts.
PHP Programming Course
Module 1: Introduction to PHP
- What is PHP?
- Setting Up the Development Environment
- Your First PHP Script
- PHP Syntax and Variables
- Data Types in PHP
Module 2: Control Structures
Module 3: Functions
- Defining and Calling Functions
- Function Parameters and Return Values
- Variable Scope
- Anonymous Functions and Closures
Module 4: Arrays
Module 5: Working with Forms
Module 6: Working with Files
Module 7: Object-Oriented Programming (OOP)
- Introduction to OOP
- Classes and Objects
- Properties and Methods
- Inheritance
- Interfaces and Abstract Classes
- Traits
Module 8: Working with Databases
- Introduction to Databases
- Connecting to a MySQL Database
- Performing CRUD Operations
- Using PDO for Database Interaction
- Database Security
Module 9: Advanced PHP Techniques
- Error and Exception Handling
- Sessions and Cookies
- Regular Expressions
- Working with JSON and XML
- PHP and Web Services
Module 10: PHP Frameworks and Best Practices
- Introduction to PHP Frameworks
- Getting Started with Laravel
- MVC Architecture
- Best Practices in PHP Development
- Testing and Debugging
