Find the Phel form for a PHP pattern you already know. Click a category to filter, or browse them all.
Variables
Variable assignment
PHP
$userName = "world";
echo "Hello $userName";Phel
(def user-name "world")
(println (str "Hello " user-name))Local bindings (let)
PHP
function area(float $r): float {
$pi = 3.14159;
$squared = $r * $r;
return $pi * $squared;
}Phel
(defn area [r]
(let [pi 3.14159
squared (* r r)]
(* pi squared)))Functions
Basic function
PHP
function add(int $a, int $b): int {
return $a + $b;
}
echo add(2, 3); // 5Phel
(defn add [a b]
(+ a b))
(add 2 3) ; => 5Default params / multi-arity
PHP
function greet(string $name = "World"): string {
return "Hello, $name!";
}
greet(); // "Hello, World!"
greet("Phel"); // "Hello, Phel!"Phel
(defn greet
([] (greet "World"))
([name] (str "Hello, " name "!")))
(greet) ; => "Hello, World!"
(greet "Phel") ; => "Hello, Phel!"Anonymous function
PHP
$double = fn($x) => $x * 2;
$add = function($a, $b) {
return $a + $b;
};
echo $double(5); // 10Phel
(def double #(* % 2))
(def add (fn [a b] (+ a b)))
(double 5) ; => 10Type annotations
PHP
function greet(?string $name): string {
return "Hello, " . ($name ?? "World");
}Phel
(defn ^string greet [^"?string" name]
(str "Hello, " (or name "World")))Variadic arguments
PHP
function sum(int ...$numbers): int {
return array_sum($numbers);
}
echo sum(1, 2, 3, 4); // 10Phel
(defn sum [& numbers]
(reduce + 0 numbers))
(sum 1 2 3 4) ; => 10Arrays and collections
Create array / vector
PHP
$numbers = [1, 2, 3, 4, 5];
$first = $numbers[0]; // 1
$count = count($numbers); // 5Phel
(def numbers [1 2 3 4 5])
(first numbers) ; => 1
(count numbers) ; => 5Associative array / map
PHP
$user = [
'name' => 'Alice',
'age' => 30,
'role' => 'admin',
];
$userName = $user['name']; // "Alice"Phel
(def user {:name "Alice"
:age 30
:role "admin"})
(:name user) ; => "Alice"Add element
PHP
$items = [1, 2, 3];
$items[] = 4; // [1, 2, 3, 4]
$map = ['a' => 1];
$map['b'] = 2; // ['a' => 1, 'b' => 2]Phel
(def items [1 2 3])
(conj items 4) ; => [1 2 3 4]
(def m {:a 1})
(assoc m :b 2) ; => {:a 1 :b 2}Remove element
PHP
$user = ['name' => 'Alice', 'age' => 30];
unset($user['age']);
// ['name' => 'Alice']Phel
(def user {:name "Alice" :age 30})
(dissoc user :age) ; => {:name "Alice"}Nested access
PHP
$data = [
'user' => [
'address' => [
'city' => 'Berlin',
],
],
];
$city = $data['user']['address']['city'];Phel
(def data {:user {:address {:city "Berlin"}}})
(get-in data [:user :address :city]) ; => "Berlin"Strings
Concatenation
PHP
$firstName = "Ada";
$lastName = "Lovelace";
$full = $firstName . " " . $lastName;
$greeting = "Hello, " . $firstName . "!";Phel
(def first-name "Ada")
(def last-name "Lovelace")
(str first-name " " last-name) ; => "Ada Lovelace"
(str "Hello, " first-name "!") ; => "Hello, Ada!"Formatting / interpolation
PHP
$msg = sprintf("Hello, %s! You are %d.", $userName, $age);
$price = sprintf("$%.2f", $amount);Phel
(def user-name "Alice")
(def age 30)
(def amount 9.5)
(format "Hello, %s! You are %d." user-name age) ; => "Hello, Alice! You are 30."
(format "$%.2f" amount) ; => "$9.50"Split / join
PHP
$parts = explode(",", "a,b,c"); // ["a", "b", "c"]
$joined = implode("-", $parts); // "a-b-c"Phel
(ns my-app
(:require phel.string :as str))
(def parts (str/split "a,b,c" #",")) ; => ["a" "b" "c"]
(str/join "-" parts) ; => "a-b-c"Control flow
If / else
PHP
if ($age >= 18) {
$status = "adult";
} else {
$status = "minor";
}Phel
(def age 21)
(def status
(if (>= age 18) "adult" "minor"))Switch / case
PHP
switch ($code) {
case 200: $msg = "OK"; break;
case 404: $msg = "Not Found"; break;
case 500: $msg = "Server Error"; break;
default: $msg = "Unknown";
}Phel
(def code 404)
(def msg
(case code
200 "OK"
404 "Not Found"
500 "Server Error"
"Unknown"))
; => "Not Found"Ternary and null coalescing
PHP
$label = $itemCount > 0 ? "has items" : "empty";
$userName = $input ?? "default";
$host = $config['db']['host'] ?? "localhost";Phel
(def item-count 3)
(def input nil)
(def config {:db {:host "db.example.com"}})
(if (> item-count 0) "has items" "empty") ; => "has items"
(or input "default") ; => "default"
(or (get-in config [:db :host]) "localhost") ; => "db.example.com"Loops
Foreach
PHP
foreach ($items as $item) {
echo $item . "\n";
}
foreach ($map as $key => $value) {
echo "$key: $value\n";
}Phel
(def items ["a" "b" "c"])
(def my-map {:x 1 :y 2})
(foreach [item items]
(println item))
(foreach [k v my-map]
(println (str k ": " v)))Accumulator loop / reduce
PHP
$sum = 0;
for ($i = 1; $i <= 10; $i++) {
$sum += $i;
}
// $sum = 55Phel
(reduce + 0 (range 1 11))
; => 55While / loop-recur
PHP
$n = 10;
$acc = 0;
while ($n > 0) {
$acc += $n;
$n--;
}
// $acc = 55Phel
(loop [n 10
acc 0]
(if (> n 0)
(recur (dec n) (+ acc n))
acc))
; => 55OOP and interop
Method call
PHP
$formatted = $date->format("Y-m-d");
$result = $date->modify("+1 month")->format("Y-m-d");Phel
(ns my.module
(:use DateTimeImmutable))
(def date (DateTimeImmutable. "2024-01-15"))
(def formatted (.format date "Y-m-d"))
(def result
(-> date
(.modify "+1 month")
(.format "Y-m-d")))Functional
Pipe / threading
PHP
// Nested calls (inside-out)
$result = implode(", ",
array_map(
fn($x) => strtoupper($x),
array_filter(
$names,
fn($x) => strlen($x) > 3
)
)
);Phel
(ns my-app
(:require phel.string :as str))
(def names ["Alice" "Bob" "Eve" "Charlie"])
(def result
(->> names
(filter #(> (count %) 3))
(map str/upper-case)
(str/join ", ")))
; => "ALICE, CHARLIE"