codegurukool.com · Free coding tutorials & online compiler

Home › PHP Tutorial › All commands

All commands

Every command of this course: name, detailed description, syntax and example code.

Every command used in this course, with its name, a detailed description, the syntax and an example.

echo

Definition: Outputs text or HTML to the page. It can take several values separated by commas.

Syntax:

echo "text";

Example:

echo "Hello, PHP!";

$variable

Definition: Variables start with $ and need no type. PHP decides the type from the value you store.

Syntax:

$name = value;

Example:

$age = 20;

function

Definition: Defines a reusable block of code with optional parameters. return sends a result back.

Syntax:

function name($a) {
  return $a;
}

Example:

function add($a, $b) {
  return $a + $b;
}

if / else

Definition: Chooses which block runs depending on a condition; elseif adds more choices.

Syntax:

if (condition) { ... } else { ... }

Example:

if ($t < 12) { echo "AM"; }

foreach

Definition: Loops over every item of an array, giving you each value (and optionally its key).

Syntax:

foreach ($array as $item) { ... }

Example:

foreach ([1, 2] as $x) { echo $x; }

array()

Definition: Creates an array. Use [] for short syntax and => to give items names (associative array).

Syntax:

$a = [1, 2];
$b = ["key" => "value"];

Example:

$car = ["brand" => "Tata"];

count

Definition: Returns how many items an array holds.

Syntax:

count($array);

Example:

echo count([1, 2, 3]); // 3

define

Definition: Creates a constant whose value can never change. Constants are written without a $.

Syntax:

define("NAME", value);

Example:

define("SITE", "Learn Web");

$_POST

Definition: A superglobal array that holds the values sent by a form with method="post".

Syntax:

$_POST["field"]

Example:

$name = $_POST["name"] ?? "guest";

#

Definition: Comments use // , # or /* */ .

Syntax:

#

Example:

# another comment

↗ PHP install and syntax

print

Definition: print takes one value and returns 1.

Syntax:

print

Example:

print "print returns 1" . PHP_EOL;

↗ PHP echo and print

PHP_EOL

Definition: PHP_EOL is a line break.

Syntax:

PHP_EOL

Example:

echo "Hello", " ", "World", PHP_EOL;
print "print returns 1" . PHP_EOL;
echo "x is " . $x . PHP_EOL;

↗ PHP echo and print

var_dump

Definition: var_dump shows both the type and the value of anything, which makes it the best debugging tool in PHP.

Syntax:

var_dump

Example:

var_dump("text");
var_dump(42);
var_dump(3.14);

↗ PHP data types

strlen

Definition: Common ones are strlen , strtoupper , str_replace and strpos .

Syntax:

strlen

Example:

echo strlen($s) . PHP_EOL;

↗ PHP strings

strtoupper

Definition: Common ones are strlen , strtoupper , str_replace and strpos .

Syntax:

strtoupper

Example:

echo strtoupper($s) . PHP_EOL;

↗ PHP strings

str_replace

Definition: Common ones are strlen , strtoupper , str_replace and strpos .

Syntax:

str_replace

Example:

echo str_replace("World", "PHP", $s) . PHP_EOL;

↗ PHP strings

strpos

Definition: Common ones are strlen , strtoupper , str_replace and strpos .

Syntax:

strpos

Example:

echo strpos($s, "World") . PHP_EOL;

↗ PHP strings

max

Definition: Use max , round , sqrt , pow and intdiv for everyday maths.

Syntax:

max

Example:

echo max(3, 9, 5) . PHP_EOL;

↗ PHP numbers and math

round

Definition: Use max , round , sqrt , pow and intdiv for everyday maths.

Syntax:

round

Example:

echo round(3.14159, 2) . PHP_EOL;

↗ PHP numbers and math

sqrt

Definition: Use max , round , sqrt , pow and intdiv for everyday maths.

Syntax:

sqrt

Example:

echo sqrt(81) . PHP_EOL;

↗ PHP numbers and math

pow

Definition: Use max , round , sqrt , pow and intdiv for everyday maths.

Syntax:

pow

Example:

echo pow(2, 10) . PHP_EOL;

↗ PHP numbers and math

intdiv

Definition: Use max , round , sqrt , pow and intdiv for everyday maths.

Syntax:

intdiv

Example:

echo intdiv(17, 5) . PHP_EOL;

↗ PHP numbers and math

match

Definition: match (PHP 8) is a stricter, shorter switch .

Syntax:

match

Example:

echo match($day) {

↗ PHP switch and match

for

Definition: for counts, while repeats while a condition holds, and foreach walks through an array.

Syntax:

for

Example:

for ($i = 1; $i <= 5; $i++) {

↗ PHP loops

while

Definition: for counts, while repeats while a condition holds, and foreach walks through an array.

Syntax:

while

Example:

while ($n > 0) {

↗ PHP loops

fn

Definition: Arrow functions ( fn ) are a short form for one-line functions.

Syntax:

fn

Example:

$sq = fn($n) => $n * $n;

↗ PHP functions

sort

Definition: [] appends, count counts, sort orders and implode joins items into a string.

Syntax:

sort

Example:

sort($f);

↗ PHP arrays

implode

Definition: [] appends, count counts, sort orders and implode joins items into a string.

Syntax:

implode

Example:

echo implode(", ", $f) . PHP_EOL;

↗ PHP arrays

htmlspecialchars

Definition: Always clean user input with htmlspecialchars before printing it, to prevent XSS attacks.

Syntax:

htmlspecialchars

Example:

$name = htmlspecialchars($_POST["name"] ?? "guest");

↗ PHP superglobals and form handling

date(format, timestamp)

Definition: date(format, timestamp) turns a timestamp into text: Y year, m month, d day, l weekday name.

Syntax:

date(format, timestamp)

Example:

echo date("Y-m-d", $t) . PHP_EOL;
echo date("l", $t);

↗ PHP date and time

l

Definition: date(format, timestamp) turns a timestamp into text: Y year, m month, d day, l weekday name.

Syntax:

l

Example:

echo date("l", $t);

↗ PHP date and time

mktime

Definition: mktime builds a timestamp from hour, minute, second, month, day and year.

Syntax:

mktime

Example:

$t = mktime(0, 0, 0, 12, 25, 2026);

↗ PHP date and time

session_start()

Definition: Call session_start() before any output, then use $_SESSION .

Syntax:

session_start()

Example:

session_start();

↗ PHP cookies and sessions

json_encode

Definition: json_encode turns an array or object into JSON text.

Syntax:

json_encode

Example:

$json = json_encode($data);

↗ PHP JSON

json_decode($text, true)

Definition: json_decode($text, true) turns it back into an associative array.

Syntax:

json_decode($text, true)

Example:

$back = json_decode($json, true);

↗ PHP JSON

throw

Definition: throw raises an exception, catch handles it, and finally always runs, even after an error.

Syntax:

throw

Example:

if ($b === 0) throw new Exception("Cannot divide by zero");

↗ PHP exceptions

catch

Definition: throw raises an exception, catch handles it, and finally always runs, even after an error.

Syntax:

catch

Example:

} catch (Exception $e) {

↗ PHP exceptions

finally

Definition: throw raises an exception, catch handles it, and finally always runs, even after an error.

Syntax:

finally

Example:

} finally {

↗ PHP exceptions

new

Definition: A class is a blueprint and an object is one instance made with new .

Syntax:

new

Example:

$c = new Car("Volvo");

↗ PHP classes, objects and constructors

__construct

Definition: The __construct method runs when the object is created.

Syntax:

__construct

Example:

public function __construct(string $brand) {

↗ PHP classes, objects and constructors

extends

Definition: A child class extends a parent and inherits its properties and methods.

Syntax:

extends

Example:

class Dog extends Animal {

↗ PHP inheritance

protected

Definition: Mark members protected to share them with children but hide them from outside code.

Syntax:

protected

Example:

public function __construct(protected string $name) {}

↗ PHP inheritance

mysqli

Definition: Connect with mysqli (or PDO).

Syntax:

mysqli

Example:

$conn = new mysqli("localhost", "user", "password", "myDB");

↗ PHP MySQL prepared statements

Example code

<?php
echo "Hello, PHP!";
?>

Run this code in the online editor

← Welcome to the PHP coursePHP introduction →