🐛 PHP — Learn From Zero
Playground: Replit PHP | JDoodle — click, type, run.
What is PHP?
PHP is a server-side scripting language built for the web. It powers WordPress, Laravel, and ~75% of websites with a backend.
- Web backends & CMS (WordPress)
- APIs (Laravel, Symfony)
- Dynamic page generation
- Quick prototyping
Run Online — No Install
Use the playground links above.
🟢 Beginner — Core Basics
Your First Program
php
PHP code goes inside <?php ?>.
Variables
php
$name = "Ada";
$age = 36;
$height = 1.78;
$isStudent = true;Variables start with $.
If / Else
php
$age = 18;
if ($age >= 18) echo "Vote";
else echo "Too young";Use elseif for chains.
Loops
php
for ($i = 0; $i < 5; $i++) echo $i;
foreach ([1,2,3] as $n) echo $n;foreach is great for arrays.
Functions
php
function greet($name) {
return "Hello, $name!";
}Strings interpolate variables directly.
Arrays
php
$fruits = ["apple", "banana"];
$fruits[] = "cherry";
$person = ["name" => "Ada", "age" => 36];Arrays are ordered maps in PHP.
🟡 Intermediate — Building Real Programs
Forms & GET/POST
php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
echo $_POST["name"];
}Read input via $_GET/$_POST.
Classes
php
class Person {
public $name;
function __construct($n) { $this->name = $n; }
}Use $this for instance.
Database (PDO)
php
$pdo = new PDO($dsn, $u, $p);
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([1]);Always prepare statements (SQL injection).
Namespaces
php
namespace App\Models;
class User {}Organize code in namespaces.
Composer
php
{
"require": { "monolog/monolog": "^3.0" }
}PHP dependency manager.
🔴 Advanced — Pro Concepts
Traits
php
trait Logger { function log($m) { echo $m; } }
class A { use Logger; }Reuse methods across classes.
Closures
php
$fn = function($x) { return $x * 2; };
echo $fn(5);Anonymous functions with use.
Generators
php
function nums() { for ($i=0;;$i++) yield $i; }Lazy iteration with yield.
Attributes (PHP 8)
php
#[Route("/home")]
class HomeController {}Native annotations via attributes.
What's Next?
- Build a tiny project: Calculator, To-Do App, Weather Widget
- Read the official docs and a good book
- Solve Exercism exercises
- Join a community & share your code
Practice daily for 20 minutes. Consistency > intensity.