Quick Start
Overview
Now that your environment is set up, let's write your first PHP script and understand the basic principles of how PHP works. This chapter will get you coding immediately with practical examples.
Your First PHP Script
Hello World
Create a file named hello.php:
Run it:
Output:
PHP in HTML
Create hello_web.php:
Run with built-in server:
Visit http://localhost:8000/hello_web.php
PHP Tags
Standard Tags (Recommended)
Short Tags (Not Recommended)
Echo Tags (PHP 5.4+)
Basic Syntax Rules
Statements and Semicolons
Case Sensitivity
Comments
Variables and Basic Data Types
Variables
Variable Rules
- Must start with
$sign - Name must begin with a letter or underscore
- Can contain letters, numbers, and underscores
- Case-sensitive
String Operations
Creating Strings
String Concatenation
Common String Functions
Number Operations
Basic Arithmetic
Number Functions
Arrays (Quick Introduction)
Indexed Arrays
Associative Arrays
Control Structures (Quick Preview)
If Statements
Loops
Functions (Quick Preview)
Built-in Functions
Custom Functions
Practical Examples
Simple Calculator
Dynamic Web Page
Common Beginner Mistakes
1. Forgetting PHP Tags
2. Missing Semicolons
3. Variable Scope Issues
4. Mixing Single and Double Quotes
Development Tips
1. Use Error Reporting
2. Use var_dump() for Debugging
3. Keep Code Organized
Next Steps
You've now written your first PHP scripts and learned the basic syntax. In the next chapter Basic Syntax, we'll dive deeper into PHP's syntax rules and conventions.
Practice Exercises
- Create a PHP script that displays your name, age, and favorite color
- Build a simple "About Me" web page using PHP variables
- Create a script that calculates and displays the area of a rectangle
- Make a dynamic page that shows different content based on the time of day
Try these exercises to reinforce your understanding before moving on!