The echo
function is used to display or print output:
<?php echo "Hello World!"; ?>
Comments are used to make the code more understandable for the programmer; they are not executed by the compiler or interpreter.
This is a single-line comment:
// Twinkle Twinkle Little Star
This is a single-line comment:
# Chocolate dedo mujhe yaar
This is a multiline comment:
/* Itedvanatage */
This function dumps information about one or more variables:
<?php var_dump(var1, var2, ...); ?>
Variables are “containers” for storing information.
<?php
$Title = "PHP Cheat Sheet By Itedvantage";
?>
Datatype is a type of data.
A string is a sequence of characters, like “Hello world!”:
<?php
$x = "rocky";
echo $x;
?>
An integer is a number without any decimal part:
<?php
$x = 1234;
var_dump($x);
?>
A float is a number with a decimal point or a number in exponential form:
<?php
$x = 1.2345;
var_dump($x);
?>
An array stores multiple values in one single variable:
<?php
$names = array("rancho","farahan","raju");
var_dump($names);
?>
A class is a template for objects:
<?php
$names = array("rancho","farahan","raju");
var_dump($names);
?>
An object is an instance of the class:
<?php
class Bike {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My bike is a " . $this->color . " " . $this->model . "!";
}
}
$myBike = new Bike("red", "Honda");
echo $myBike->message();
?>
Escape sequences are used for escaping a character during string parsing. They are also used for giving special meaning to represent line breaks, tabs, alerts, and more.
It adds a newline:
<?php
echo "\n";
?>
It inserts a carriage return in the text at this point:
<?php
echo "\r";
?>
It gives a horizontal tab space:
<?php
echo "\t";
?>
It gives a vertical tab space:
<?php
echo "\v";
?>
It is used for escape characters:
<?php
echo "\e";
?>
It is commonly used as page separators but now is also used as section separators:
<?php
echo "\f";
?>
It adds a backslash:
<?php
echo "\\";
?>
Print the next character as a dollar, not as part of a variable:
<?php
echo "\$";
?>
Print the next character as a single quote, not a string closer:
<?php
echo "\";
?>
Print the next character as a double quote, not a string closer:
<?php
echo "\"";
?>
Operators are symbols that tell the compiler or interpreter to perform specific mathematical or logical manipulations. These are of several types.
Sum of $x
and $y
:
$x + $y
Difference of $x
and $y
:
$x - $y
Product of $x
and $y
:
$x * $y
Quotient of $x
and $y
:
$x / $y
The remainder of $x
divided by $y
:
$x % $y
Result of raising $x
to the $y
‘th power:
$x ** $y
The PHP assignment operators are used with numeric values to write a value to a variable.
The left operand gets set to the value of the expression on the right:
x = y
Addition:
x = x + y
Subtraction:
x = x - y
Multiplication:
x = x * y
Division:
x = x / y
Modulus:
x = x % y
Returns true if $x
is equal to $y
:
$x == $y
Returns true if $x
is equal to $y
, and they are of the same type:
$x === $y
Returns true if $x
is not equal to $y
:
$x != $y
Returns true if $x
is not equal to $y
:
$x <> $y
Returns true if $x
is not equal to $y
, or they are not of the same type:
$x !== $y
Returns true if $x
is greater than $y
:
$x > $y
Returns true if $x
is less than $y
:
$x < $y
Returns true if $x
is greater than or equal to $y
:
$x >= $y
Returns true if $x
is less than or equal to $y
:
$x <= $y
Increments $x
by one, then returns $x
:
++$x
Returns $x
, then increments $x
by one:
$x++
Decrements $x
by one, then returns $x
:
--$x
Returns $x
, then decrements $x
by one:
$x--
True if both $x
and $y
are true:
$x and $y
True if either $x
or $y
is true
$x or $y
True if either $x
or $y
is true, but not both:
$x xor $y
True if both $x
and $y
are true:
$x && $y
True if $x
is not true:
!$x
Concatenation of $txt1
and $txt2
:
$txt1 . $txt2
Appends $txt2
to $txt1
:
$txt1 .= $txt2
Union of $x
and $y
:
$x + $y
Returns true if $x
and $y
have the same key/value pairs:
$x == $y
Returns true if $x
and $y
have the same key/value pairs in the same order and of the same types:
$x === $y
Returns true if $x
is not equal to $y
:
$x != $y
Returns true if $x
is not equal to $y
:
$x <> $y
Returns true if $x
is not identical to $y
:
$x !== $y
Returns the value of $x
. The value of $x
is expr2
if expr1 = TRUE
. The value of $x
is expr3
if expr1 = FALSE
:
$x = expr1 ? expr2 : expr3
Conditional statements are used to perform operations based on some condition.
The if
statement checks the condition and if it is True, then the block of if
statement executes; otherwise, control skips that block of code:
if (condition) {
// code to execute if condition is met
}
If the condition of if
block evaluates to True, then if
block executes otherwise else
block executes:
if (condition) {
// code to execute if condition is met
} else {
// code to execute if condition is not met
}
It executes different codes for more than two conditions:
if (condition) {
// code to execute if condition is met
} elseif (condition) {
// code to execute if this condition is met
} else {
// code to execute if none of the conditions are met
}
It allows a variable to be tested for equality against a list of values (cases):
switch (n) {
case x:
// code to execute if n=x;
break;
case y:
// code to execute if n=y;
break;
case z:
// code to execute if n=z;
break;
// add more cases as needed
default:
// code to execute if n is neither of the above;
}
Iterative statements or Loops facilitate programmers to execute any block of code lines repeatedly.
It is used to iterate the statements several times. It is frequently used to traverse the data structures like the array and linked list:
for (starting counter value; ending counter value; increment by which
to increase) {
// code to execute goes here
}
The foreach
loop loops through a block of code for each element in an array:
foreach ($InsertYourArrayName as $value) {
// code to execute goes here
}
It iterates the block of code as long as a specified condition is True or vice versa:
while (condition that must apply) {
// code to execute goes here
}
This loop is very similar to the while
loop with one difference, i.e., the body of the do-while
loop is executed at least once even if the condition is False. It is an exit-controlled loop:
do {
// code to execute goes here;
} while (condition that must apply);
PHP provides a large number of predefined variables to all scripts. The variables represent everything from external variables to built-in environment variables, last error messages, etc. All this information is defined in some predefined variables.
$GLOBALS
is a PHP super global variable which is used to access global variables from anywhere in the PHP script:
<?php
$a = 10;
$b = 15;
function addition() {
$GLOBALS['c'] = $GLOBALS['a'] + $GLOBALS['b'];
}
addition();
echo $c;
?>
$_SERVER['PHP_SELF']
$_SERVER['GATEWAY_INTERFACE']
$_SERVER['SERVER_ADDR']
$_SERVER['SERVER_NAME']
$_SERVER['SERVER_SOFTWARE']
$_SERVER['SERVER_PROTOCOL']
$_SERVER['REQUEST_METHOD']
$_SERVER['REQUEST_TIME']
$_SERVER['QUERY_STRING']
$_SERVER['HTTP_ACCEPT']
$_SERVER['HTTP_ACCEPT_CHARSET']
$_SERVER['HTTP_HOST']
$_SERVER['HTTP_REFERER']
$_SERVER['HTTPS']
$_SERVER['REMOTE_ADDR']
$_SERVER['REMOTE_HOST']
$_SERVER['REMOTE_PORT']
$_SERVER['SCRIPT_FILENAME']
$_SERVER['SERVER_ADMIN']
$_SERVER['SERVER_PORT']
$_SERVER['SERVER_SIGNATURE']
$_SERVER['PATH_TRANSLATED']
$_SERVER['SCRIPT_NAME']
$_SERVER['SCRIPT_URI']
PHP $_GET
is a PHP super global variable which is used to collect form data after submitting an HTML form with method=”get”:
<?php
echo "Hello" . $_GET['Example'] . " at " . $_GET['web'];
?>
PHP $_POST
is a PHP super global variable which is used to collect form data after submitting an HTML form with method=”post”. $_POST
is also widely used to pass variables:
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['fname'];
if (empty($name)) {
echo "Please Enter your name";
} else {
echo $name;
}
}
?>
</body>
</html>
PHP $_REQUEST
is a PHP super global variable which is used to collect data after submitting an HTML form:
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
The PHP variable handling functions are part of the PHP core. No installation is required to use these functions.
Boolval
is used to get the boolean value of a variable:
<?php
echo '0: ' . (boolval(0) ? 'true' : 'false') . "\n";
echo '42: ' . (boolval(42) ? 'true' : 'false') . "\n";
echo '0.0: ' . (boolval(0.0) ? 'true' : 'false') . "\n";
echo '4.2: ' . (boolval(4.2) ? 'true' : 'false') . "\n";
echo '"": ' . (boolval("") ? 'true' : 'false') . "\n";
echo '"string": ' . (boolval("string") ? 'true' : 'false') . "\n";
echo '"0": ' . (boolval("0") ? 'true' : 'false') . "\n";
echo '"1": ' . (boolval("1") ? 'true' : 'false') . "\n";
?>