Php_cheatsheet
Php_cheatsheet
</>CodeWithHarry
Php Cheatsheet
"Php Cheatsheet for all php developers"
By CodeWithHarry Updated: 5 April 2025
Basics
Hello World
The echo function is used to display or print output:
Comments
Comments are used to make the code more understandable for the programmer; they are not
executed by the compiler or interpreter.
One Liner
https://www.codewithharry.com/blogpost/php-cheatsheet 1/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
Multiline
/* Code With
Harry */
Vardump
Variables
Defining Variables
<?php
$Title = "PHP Cheat Sheet By CodeWithHarry";
?>
https://www.codewithharry.com/blogpost/php-cheatsheet 2/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
Datatypes
String
<?php
$x = "Harry";
echo $x;
?>
Integer
<?php
$x = 1234;
var_dump($x);
?>
Float
<?php
$x = 1.2345;
var_dump($x);
?>
Array
<?php
$names = array("Harry","Rohan","Shubham");
https://www.codewithharry.com/blogpost/php-cheatsheet 3/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
var_dump($names);
?>
Class
<?php
class Harry{
// code goes here...
}
?>
Object
<?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 . "!";
}
}
Escape Characters
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.
https://www.codewithharry.com/blogpost/php-cheatsheet 4/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
Line feed
It adds a newline:
<?php
echo "\n";
?>
Carriage return
<?php
echo "\r";
?>
Horizontal tab
<?php
echo "\t";
?>
Vertical tab
<?php
echo "\v";
?>
Escape
https://www.codewithharry.com/blogpost/php-cheatsheet 5/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
<?php
echo "\e";
?>
Form feed
It is commonly used as page separators but now is also used as section separators:
<?php
echo "\f";
?>
Backslash
It adds a backslash:
<?php
echo "\\";
?>
Dollar sign
<?php
echo "\$";
?>
Single quote
Print the next character as a single quote, not a string closer:
<?php
echo "\'";
?>
https://www.codewithharry.com/blogpost/php-cheatsheet 6/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
Double quote
<?php
echo "\"";
?>
Operators
Operators are symbols that tell the compiler or interpreter to perform specific mathematical or
logical manipulations. These are of several types.
Arithmetic Operators
Addition
Sum of $x and $y :
$x + $y
Subtraction
Difference of $x and $y :
$x - $y
Multiplication
Product of $x and $y :
$x * $y
Division
https://www.codewithharry.com/blogpost/php-cheatsheet 7/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
Quotient of $x and $y :
$x / $y
Modulus
The remainder of $x divided by $y :
$x % $y
Exponentiation
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.
x=y
The left operand gets set to the value of the expression on the right:
x = y
x += y
Addition:
x = x + y
x -= y
https://www.codewithharry.com/blogpost/php-cheatsheet 8/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
Subtraction:
x = x - y
x *= y
Multiplication:
x = x * y
x /= y
Division:
x = x / y
x %= y
Modulus:
x = x % y
Equal
$x == $y
Identical
Returns true if $x is equal to $y , and they are of the same type:
https://www.codewithharry.com/blogpost/php-cheatsheet 9/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
$x === $y
Not equal
Returns true if $x is not equal to $y :
$x != $y
Not equal
Returns true if $x is not equal to $y :
$x <> $y
Not identical
Returns true if $x is not equal to $y , or they are not of the same type:
$x !== $y
Greater than
$x > $y
Less than
$x < $y
$x >= $y
$x <= $y
Pre-increment
Increments $x by one, then returns $x :
++$x
Post-increment
Returns $x , then increments $x by one:
$x++
Pre-decrement
Decrements $x by one, then returns $x :
--$x
Post-decrement
Returns $x , then decrements $x by one:
https://www.codewithharry.com/blogpost/php-cheatsheet 11/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
$x--
And
True if both $x and $y are true:
$x and $y
Or
True if either $x or $y is true:
$x or $y
Xor
$x xor $y
And
$x && $y
Or
True if either $x or $y is true:
$x || $y
https://www.codewithharry.com/blogpost/php-cheatsheet 12/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
Not
!$x
Concatenation
Concatenation of $txt1 and $txt2 :
$txt1 . $txt2
Concatenation assignment
Appends $txt2 to $txt1 :
$txt1 .= $txt2
Union
Union of $x and $y :
$x + $y
Equality
Returns true if $x and $y have the same key/value pairs:
$x == $y
https://www.codewithharry.com/blogpost/php-cheatsheet 13/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
Identity
Returns true if $x and $y have the same key/value pairs in the same order and of the same
types:
$x === $y
Inequality
$x != $y
Inequality
Returns true if $x is not equal to $y :
$x <> $y
Non-identity
Returns true if $x is not identical to $y :
$x !== $y
Ternary
Returns the value of $x . The value of $x is expr2 if expr1 = TRUE . The value of $x is
expr3 if expr1 = FALSE :
https://www.codewithharry.com/blogpost/php-cheatsheet 14/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
Conditional Statements
If Statement
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..Else
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
}
If..Elseif..Else
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
}
Switch Statement
https://www.codewithharry.com/blogpost/php-cheatsheet 15/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
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;
}
Loops
Iterative statements or Loops facilitate programmers to execute any block of code lines
repeatedly.
For Loop
It is used to iterate the statements several times. It is frequently used to traverse the data
structures like the array and linked list:
Foreach Loop
The foreach loop loops through a block of code for each element in an array:
https://www.codewithharry.com/blogpost/php-cheatsheet 16/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
While Loop
It iterates the block of code as long as a specified condition is True or vice versa:
Do-While Loop
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);
Predefined Variables
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
$GLOBALS is a PHP super global variable which is used to access global variables from
<?php
$a = 10;
$b = 15;
function addition() {
https://www.codewithharry.com/blogpost/php-cheatsheet 17/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
$_SERVER
Returns the filename of the currently executing script:
$_SERVER['PHP_SELF']
Returns the version of the Common Gateway Interface (CGI) the server is using:
$_SERVER['GATEWAY_INTERFACE']
$_SERVER['SERVER_ADDR']
$_SERVER['SERVER_NAME']
$_SERVER['SERVER_SOFTWARE']
Returns the name and revision of the information protocol (such as HTTP/1.1):
$_SERVER['SERVER_PROTOCOL']
https://www.codewithharry.com/blogpost/php-cheatsheet 18/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
Returns the request method used to access the page (such as POST):
$_SERVER['REQUEST_METHOD']
$_SERVER['REQUEST_TIME']
Returns the query string if the page is accessed via a query string:
$_SERVER['QUERY_STRING']
$_SERVER['HTTP_ACCEPT']
Returns the Accept_Charset header from the current request (such as utf-8,ISO-8859-1):
$_SERVER['HTTP_ACCEPT_CHARSET']
$_SERVER['HTTP_HOST']
Returns the complete URL of the current page (not reliable because not all user-agents
support it):
$_SERVER['HTTP_REFERER']
https://www.codewithharry.com/blogpost/php-cheatsheet 19/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
$_SERVER['HTTPS']
Returns the IP address from where the user is viewing the current page:
$_SERVER['REMOTE_ADDR']
Returns the Hostname from where the user is viewing the current page:
$_SERVER['REMOTE_HOST']
Returns the port being used on the user's machine to communicate with the web server:
$_SERVER['REMOTE_PORT']
$_SERVER['SCRIPT_FILENAME']
Returns the value given to the SERVER_ADMIN directive in the web server configuration
file (if your script runs on a virtual host, it will be the value defined for that virtual host)
(such as [email protected]):
$_SERVER['SERVER_ADMIN']
Returns the port on the server machine being used by the webserver for communication
(such as 80):
$_SERVER['SERVER_PORT']
https://www.codewithharry.com/blogpost/php-cheatsheet 20/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
Returns the server version and virtual hostname which are added to server-generated
pages:
$_SERVER['SERVER_SIGNATURE']
$_SERVER['PATH_TRANSLATED']
$_SERVER['SCRIPT_NAME']
$_SERVER['SCRIPT_URI']
$_GET
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'];
?>
$_POST
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>
https://www.codewithharry.com/blogpost/php-cheatsheet 21/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
$_REQUEST
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>
https://www.codewithharry.com/blogpost/php-cheatsheet 22/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
Variable-handling Functions
The PHP variable handling functions are part of the PHP core. No installation is required to
use these functions.
boolval
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
Tags
Share
php cheatsheet
Main Learn
Home Courses
Contact Tutorials
Work With Us Notes
My Gear
Legal Social
Terms GitHub
Privacy Twitter (X)
Refund YouTube
Facebook
https://www.codewithharry.com/blogpost/php-cheatsheet 23/24
4/18/25, 11:37 AM Php Cheatsheet | Blog | CodeWithHarry
https://www.codewithharry.com/blogpost/php-cheatsheet 24/24