You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is an overview of the built-in PHP language features and its basic syntax
so you have better understanding of which chapters has been introduced so far.
PHP in a nutshell
Scripting language
Procedural and object oriented language
Dynamically (weakly) typed
Syntax similar to C, C++, C#, Java and Perl
Imperative language
PHP has closures
Hello world
File hello.php:
<?phpecho'Hello world.';
can be run from the command line with:
php hello.php
Operators
Arithmetic operators
Operator
Name
Result
-$a
negation
Opposite of $a.
$a + $b
addition
Sum of $a and $b.
$a - $b
Subtraction
Difference of $a and $b.
$a * $b
Multiplication
Product of $a and $b.
$a / $b
division
Quotient of $a and $b.
$a % $b
modulus
Remainder of $a divided by $b.
$a ** $b
Exponentiation
Result of raising $a to the $b'th power.
Comparison operators
Operator
Name
Result
$a == $b
Equal
TRUE if $a is equal to $b after type juggling.
$a === $b
Identical
TRUE if $a is equal to $b, and they are of the same type.
$a != $b
Not equal
TRUE if $a is not equal to $b after type juggling.
$a <> $b
Not equal
TRUE if $a is not equal to $b after type juggling.
$a !== $b
Not identical
TRUE if $a is not equal to $b, or they are not of the same type.
$a < $b
Less than
TRUE if $a is strictly less than $b.
$a > $b
Greater than
TRUE if $a is strictly greater than $b.
$a <= $b
Less than or equal to
TRUE if $a is less than or equal to $b.
$a >= $b
Greater than or equal to
TRUE if $a is greater than or equal to $b.
Logical operators
Operator
Name
Result
$a and $b
And
TRUE if both $a and $b are TRUE.
$a or $b
Or
TRUE if either $a or $b is TRUE.
$a xor $b
Xor
TRUE if either $a or $b is TRUE, but not both.
! $a
Not
TRUE if $a is not TRUE.
$a && $b
And
TRUE if both $a and $b are TRUE.
`$a
$b`
Assignment operators
Operator
Description
=
Set a value to variable
+=
Addition of numeric value to variable
.=
Add string to variable
Declarations
$i = 1; - assign value to variable
define('FOO', 'something'); - define a constant
Functions
// A simple functionfunctionfunctionName() {}
// A function with parametersfunctionfunctionName($param1, $param2) {}
Anonymous functions (closures)
<?phpechopreg_replace_callback('~-([a-z])~', function ($match) {
returnstrtoupper($match[1]);
}, 'hello-world');
// outputs helloWorld// Anonymous function variable assignment example$greet = function($name) {
printf("Hello %s\r\n", $name);
};
$greet('World');
// Inherit$message = 'hello';
// Without "use" keyword$example = function () {
var_dump($message);
};
echo$example();
// Inherit $message$example = function () use ($message) {
var_dump($message);
};
echo$example();
TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
$a != $b
Inequality
TRUE if $a is not equal to $b.
$a <> $b
Inequality
TRUE if $a is not equal to $b.
$a !== $b
Non-identity
TRUE if $a is not identical to $b.
Built-in types
Type
boolean
integer
float
string
array
object
resource
NULL
Errors
PHP errors
<?php// Turn off all error reportingerror_reporting(0);
// Report simple running errorserror_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized// variables or catch variable name misspellings ...)error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICEerror_reporting(E_ALL & ~E_NOTICE);
// Report all PHP errors (see changelog)error_reporting(E_ALL);
// Report all PHP errorserror_reporting(-1);
// Same as error_reporting(E_ALL);ini_set('error_reporting', E_ALL);