Basic PHP Syntax: Free PHP & MySQL Tutorial
Master PHP fundamentals with hands-on syntax examples
Core PHP Concepts You'll Master
Variables & Echo
Learn to create PHP variables and output content to web pages using the echo command.
String Handling
Master single vs double quotes, escaping characters, and string concatenation techniques.
Development Environment
Set up and use MAMP PRO or XAMPP for local PHP development on Mac and Windows.
If you're familiar with JavaScript, much of PHP syntax will look familiar. Even without previous programming experience, PHP is designed to be simple to learn.
Starting MAMP PRO on Mac
Launch Application
Navigate to Hard Drive > Applications > MAMP PRO and double-click MAMP PRO.app
Access Web Interface
Click WebStart button or navigate to localhost:8888/MAMP in your browser
Starting XAMPP on Windows
Open Control Panel
Navigate to C: > xampp folder and double-click xampp-control.exe
Start Services
Click Start next to Apache, then click Start next to MySQL
Always end PHP statements with a semicolon (;). Forgetting semicolons will cause errors in your code.
PHP Code vs HTML Output
| Feature | PHP Code | Browser Output |
|---|---|---|
| String | echo "Hello Universe"; | Hello Universe |
| Math Expression | echo 2 + 2; | 4 |
| Math as String | echo "2 + 2"; | 2 + 2 |
Quote Types in PHP
| Feature | Single Quotes | Double Quotes |
|---|---|---|
| Variable Processing | Literal text only | Evaluates variables |
| Example Output | $myMessage | Hello Universe |
| Use Case | Static strings | Dynamic content |
Common Escape Characters
Quotes
Use backslash before quotes: \'It\'s\' for single quotes, \"Hi\" for double quotes.
Special Characters
Use \\ for backslash, \t for tab, \n for new line, \r for carriage return.
PHP interprets quotes as string delimiters. Without escaping, quotes inside strings confuse the parser and cause errors.
Concatenation Methods
| Feature | Method | Example | Result |
|---|---|---|---|
| Dot Operator | "This " . "is " . "text" | This is text | |
| Append Operator | $msg .= " more text" | Adds to existing variable |
PHP Comment Syntax
Single Line Comments
Use // at the beginning of a line to comment out individual statements.
Multi-line Comments
Use /* to start and */ to end block comments spanning multiple lines.
Key Takeaways