Comprehensive PHP Code Examples
Browse our complete collection of PHP examples organized by category. Each example includes runnable code, expected output, and detailed explanations. Click any example to run it in your browser, modify the code, and see instant results.
Basics
Start with fundamental PHP concepts including variables, control structures, and basic syntax.
- Hello World - Basic PHP hello world program
- Variables - Working with PHP variables and data types
- If Else - Conditional statements in PHP
- Switch Case - Switch case statements in PHP
- For Loop - For loop iteration in PHP
- While Loop - While loop in PHP
- Foreach Loop - Foreach loop for arrays in PHP
- Functions - Creating and using PHP functions
Arrays
Master array manipulation with examples covering indexed arrays, associative arrays, and array functions.
- Indexed Arrays - Working with indexed arrays
- Associative Arrays - Working with associative arrays
- Multidimensional Arrays - Working with multidimensional arrays
- Sort Array - Sorting arrays in PHP
- Remove Duplicates - Remove duplicate values from array
Strings
Learn string manipulation techniques with practical examples of PHP string functions.
- String Length - Get length of a string
- Reverse String - Reverse a string in PHP
- Substring - Extract substring from string
JSON
Work with JSON data in PHP including encoding and decoding operations.
- JSON Encode - Convert PHP array to JSON
- JSON Decode - Convert JSON to PHP array
Dates
Format and manipulate dates using PHP's date functions.
- Date Format - Format dates in PHP
Regular Expressions
Pattern matching and text processing using regular expressions.
- Regex Match - Pattern matching with regular expressions
How to Use These Examples
Each example page includes a runnable code editor where you can modify the code and execute it instantly. The expected output is displayed alongside the code, helping you understand what the code does before running it. Use these examples as:
- Learning Resources: Study the code and explanations to understand how PHP features work
- Quick Reference: Copy working code snippets for your own projects
- Experimentation: Modify the examples to test different approaches and learn through practice
- Interview Prep: Practice common programming patterns and problems