Getting Started
5 snippetsYour first steps with PHP basics
Hello World
<?php
echo "Hello, World!"; Comments
// Single line comment
# Also single line
/* Multi-line
comment */ Variables
$name = "John"; // No type declaration
$age = 25; // Dynamically typed Type Casting
(int) "42" // string to int
(string) 42 // int to string
(float) "3.14" // to float Check Type
gettype($var) // "string", "integer"
is_string($var) // true/false
is_array($var) // true/false Variables & Data Types
8 snippetsStore and manage data in PHP
String
$name = "John"; Integer
$age = 25; Float
$price = 19.99; Boolean
$isActive = true; Array
$fruits = ["apple", "banana", "cherry"]; Associative Array
$person = ["name" => "John", "age" => 30]; Null
$value = null; Constant
const PI = 3.14159;
define("MAX", 100); Operators
7 snippetsSymbols for calculations and comparisons
Arithmetic
+ - * / // Add, Sub, Mul, Div
% // Modulus
** // Exponent
++ -- // Increment/Decrement Comparison
== != // Equal (loose)
=== !== // Equal (strict)
> < // Greater, Less
>= <= // Greater/Less or equal
<=> // Spaceship (returns -1, 0, 1) Logical
&& and // AND
|| or // OR
! // NOT
xor // XOR String
. // Concatenation
.= // Concatenate and assign
"Hello, $name" // Interpolation Null Operators
?? // Null coalescing
??= // Null coalescing assignment
?-> // Nullsafe operator Assignment
= += -= *= /= %=
.= // String concatenate assign
??= // Null coalescing assign Bitwise
& // AND
| // OR
^ // XOR
~ // NOT
<< // Left shift
>> // Right shift Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Loops
9 snippetsRepeat code efficiently with iterations
For
for ($i = 0; $i < 5; $i++) {
echo $i;
} Foreach (Array)
foreach ($fruits as $fruit) {
echo $fruit;
} Foreach (Key-Value)
foreach ($person as $key => $value) {
echo "$key: $value";
} While
while ($count < 10) {
$count++;
} Do While
do {
$count++;
} while ($count < 10); Break
for ($i = 0; $i < 10; $i++) {
if ($i === 5) break;
} Continue
for ($i = 0; $i < 10; $i++) {
if ($i === 5) continue;
echo $i;
} Break Level
foreach ($outer as $o) {
foreach ($inner as $i) {
if ($i === 5) break 2; // Breaks both
}
} Array Functions
array_map(fn($n) => $n * 2, $nums);
array_filter($nums, fn($n) => $n > 0); Conditionals
5 snippetsDirect program execution with conditions
If/Else
if ($x > 0) {
echo "positive";
} elseif ($x < 0) {
echo "negative";
} else {
echo "zero";
} Ternary
$result = $condition ? "yes" : "no"; Null Coalescing
$name = $user ?? "Guest";
$name = $user['name'] ?? "Unknown"; Match
$result = match($day) {
"Mon" => "Monday",
"Tue", "Wed" => "Midweek",
default => "Other"
}; Switch
switch ($day) {
case "Mon":
echo "Monday";
break;
default:
echo "Other";
} Functions
5 snippetsReusable blocks of code
Function
function greet(string $name): string {
return "Hello, $name!";
} Default Param
function greet(string $name = "World"): string {
return "Hello, $name!";
} Arrow Function
$square = fn($x) => $x * $x; Variadic
function sum(...$nums): int {
return array_sum($nums);
} Named Args
greet(name: "John", age: 30); Array Functions
6 snippetsManipulate and transform arrays
Map
$doubled = array_map(fn($n) => $n * 2, $nums); Filter
$evens = array_filter($nums, fn($n) => $n % 2 === 0); Reduce
$sum = array_reduce($nums, fn($a, $b) => $a + $b, 0); Merge
$all = array_merge($arr1, $arr2); Sort
sort($nums);
usort($users, fn($a, $b) => $a['age'] <=> $b['age']); Search
$key = array_search("apple", $fruits); Classes & OOP
4 snippetsClass
class Dog {
public function __construct(
public string $name
) {}
public function bark(): string {
return "{$this->name} says woof!";
}
} Inheritance
class Puppy extends Dog {
public function play(): string {
return "{$this->name} is playing!";
}
} Interface
interface Animal {
public function speak(): string;
} Trait
trait Loggable {
public function log(string $msg): void {
echo $msg;
}
} Error Handling
4 snippetsTry/Catch
try {
$result = riskyOperation();
} catch (Exception $e) {
echo $e->getMessage();
} Finally
try {
// code
} catch (Exception $e) {
// handle
} finally {
cleanup();
} Throw
throw new InvalidArgumentException("Invalid input"); Custom Exception
class MyException extends Exception {} String Operations
7 snippetsInterpolation
$greeting = "Hello, $name!"; Concatenate
$full = $first . " " . $last; Length
strlen($str); Substring
substr($str, 0, 5); Replace
str_replace("old", "new", $str); Split
explode(",", "a,b,c"); Join
implode(",", ["a", "b", "c"]);