⬅ Previous TopicKotlin Cheat Sheet
Next Topic ⮕Python Cheat Sheet
PHP code is written inside special tags:
<?php
echo "Hello, World!";
?>
$name = "John";
const PI = 3.14;
define("SITE_NAME", "MySite");
$str = "Hello";
$int = 5;
$flt = 3.14;
$bool = true;
$arr = [1, 2, 3];
$obj = new stdClass();
$none = null;
+, -, *, /, %
==, !=, ===, !==, <, >, <=, >=
&&, ||, !
=, +=, -=, *=, /=
$num = (int)"123";
$float = (float)"12.34";
$bool = (bool)1;
if ($x > 0) {
echo "Positive";
} elseif ($x < 0) {
echo "Negative";
} else {
echo "Zero";
}
switch ($color) {
case "red":
echo "Red";
break;
default:
echo "Other";
}
for ($i = 0; $i < 5; $i++) {
echo $i;
}
foreach ($arr as $value) {
echo $value;
}
while ($x < 10) {
$x++;
}
do {
$x--;
} while ($x > 0);
for ($i = 0; $i < 10; $i++) {
if ($i == 5) break;
if ($i % 2 == 0) continue;
echo $i;
}
function greet($name) {
return "Hello, $name";
}
function greet($name = "Guest") {
echo "Hello, $name";
}
function sum(...$nums) {
return array_sum($nums);
}
function add($a, $b) {
return $a + $b;
}
$greet = function($name) {
return "Hello $name";
};
echo $greet("Sam");
$sum = fn($a, $b) => $a + $b;
$colors = ["red", "green"];
$ages = ["Peter" => 35, "Ben" => 37];
count($arr);
array_merge($a, $b);
array_map(fn($x) => $x * 2, $arr);
foreach ($arr as $value) {
echo $value;
}
$_GET['id'];
$_POST['name'];
$_SERVER['REQUEST_METHOD'];
$greeting = "Hello" . " World";
strlen("hello");
strpos("hello", "e");
str_replace("world", "PHP", "hello world");
$text = <<<EOD
Line 1
Line 2
EOD;
$raw = <<<'EOD'
Raw string
EOD;
class Person {
public $name;
function __construct($name) {
$this->name = $name;
}
function greet() {
return "Hello, $this->name";
}
}
$john = new Person("John");
echo $john->greet();
Defined using public
, private
, or protected
.
function __destruct() {
echo "Goodbye!";
}
class Animal {
function speak() {
echo "Sound";
}
}
class Dog extends Animal {
function speak() {
echo "Bark";
}
}
class Math {
public static function add($a, $b) {
return $a + $b;
}
}
echo Math::add(2, 3);
interface Logger {
public function log($msg);
}
trait Shared {
public function sayHi() {
echo "Hi";
}
}
namespace AppControllers;
use AppModelsUser;
$file = fopen("data.txt", "r");
echo fread($file, filesize("data.txt"));
fclose($file);
file_put_contents("log.txt", "Hello");
echo file_get_contents("log.txt");
move_uploaded_file($_FILES['file']['tmp_name'], "uploads/" . $_FILES['file']['name']);
mkdir("newdir");
unlink("file.txt");
rename("old.txt", "new.txt");
Notices, Warnings, Fatal errors, Parse errors
try {
throw new Exception("Error!");
} catch (Exception $e) {
echo $e->getMessage();
} finally {
echo "Done";
}
class MyException extends Exception {}
throw new MyException("Custom error");
error_reporting(E_ALL);
ini_set("display_errors", 1);
$name = $_GET['name'];
$email = $_POST['email'];
if (empty($_POST['email'])) {
echo "Email is required";
}
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
echo htmlspecialchars($email);
$conn = new mysqli("localhost", "root", "", "test");
$result = $conn->query("SELECT * FROM users");
$stmt = $conn->prepare("SELECT * FROM users WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo $row['name'];
}
echo date("Y-m-d H:i:s");
echo strtotime("next Friday");
$json = json_encode(["name" => "John"]);
$data = json_decode($json, true);
preg_match("/php/", "I love php");
preg_replace("/php/", "PHP", "php is cool");
include "header.php";
require "config.php";
function add(int $a, int $b): int {
return $a + $b;
}
$name = $_GET['name'] ?? 'Guest';
echo 5 <=> 10; // -1
function greet($name, $greeting = "Hello") {
echo "$greeting, $name";
}
greet(name: "Jane", greeting: "Hi");
#[Route("/api")]
function handler() {}
$result = match($status) {
200 => 'OK',
404 => 'Not Found',
default => 'Unknown'
};