PHP Cheatsheet V1

a PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.
PHP 7 is the latest stable release..


can be used :-

  • PHP based web page
  • To preform dynamic action ie email or updata data sources
  • As a endpoint returning JSON or xml
  • As a endpoint returning HTML

When embedded in Html pages
  • Php can be call dynamicly from buttons on forms
  • Directly embeded in html source for function calls small dynamic generate html units
  • called via AXAJ (normally jquery)

Code Examples

using php functions with html (use .php extension) with html tag at start Today is Thursday 19/05 time is 07:39:57pm
Using a button to call php

Learn about exclusive offers and receive regular hints and tips :      




Using a button to call php (getjson)

Select item from database      


via a script using ajax,calls an external php file
( which echos html code) and then uses .replacewith
json endpoint using ajax, calls an external php file
(encode mysql to json) jquery build table
Comments // single line , /* multi line*/


Variables Keywords echo are case-insensitive, variables are case-sensitive. PHP is a Loosely Typed Language
Globals ans static variables can be used full range of strings, interge etc
Names rules
  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive ($age and $AGE are two different variables)
  • Arithmetic operators: +-*/% and **
  • Assignment operators: = += -= /= %=
  • Comparison operators: == === (= and same type) != <> > < >= <=
  • Increment/Decrement operators: ++$x(pre-increment) $x++(post-increment) ans -- options
  • Logical operators: and, or, xor,&&, || and !
  • String operators: . ($x. $y ) and .= $x .= $y
  • Array operators: Examples
  • Conditional assignment operators:Examples

Full list
statements ##
  • Indexed arrays - Arrays with a numeric index

    Examples : $cars = array("Volvo", "BMW", "Toyota"); $cars[0] = "Volvo";$cars[1] = "BMW";

  • Associative arrays - Arrays with named keys

    Examples :$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");$age['Peter'] = "35"; $age['Ben'] = "37"; loop through

  • Multidimensional arrays - Arrays containing one or more arrays

    2 dim $cars = array ( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15));

Objects PHP 5 is 'object-capable language'. with ability to build use classes
tutorials :- w3 objects w3resource
Built in Functions PHP has more than 1000 built-in functions Including:-
  • Dates: date(format,timestamp) : format= Y/m/d and l day of week
  • Files: abilty to open/create read/write files, search trees /upload files etc Read more
  • Filters: used to validate and sanitize external input. Read More

Cookies and Sessions

Http is a stateless protocol; cookies allow us to track the state of the application using small files stored on the user’s computer. Session are stored on server, sessions are dependent on Cookie
  • Sessions are stored on server side. Cookies are on the client side.
  • Sessions are closed when the user closes his browser. For cookies, you can set time that when it will be expired.
  • Sessions are safe that cookies. Because, since stored on client's computer, there are ways to modify or manipulate cookies.


Session management: Cookies are widely used to manage user sessions. For example, when you use an online shopping cart, you keep adding items in the cart and finally when you checkout, all of those items are added to the list of items you have purchased. This can be achieved using cookies.

User identification: Once a user visits a webpage, using cookies, that user can be remembered. And later on, depending upon the search/visit pattern of the user, content which the user likely to be visited are served. A good example of this is 'Retargetting'. A concept used in online marketing, where depending upon the user's choice of content, advertisements of the relevant product, which the user may buy, are served.

Tracking / Analytics: Cookies are used to track the user. Which, in turn, is used to analyze and serve various kind of data of great value, like location, technologies (e.g. browser, OS) form where the user visited, how long (s)he stayed on various pages etc.
Cookie named '' is not set!

JSON Access

used to transfer data/to from web page for example return data from mysql database
  • json_encode() : used to encode a value to JSON format
  • json_decode() : used to decode a JSON object into a PHP object or an associative array

Forms handing

  • $_GET is an array of variables passed to the current script via the URL parameters.( visible to everyone, used for sending non-sensitive data )
  • $_POST is an array of variables passed to the current script via the HTTP POST method.
Forms example


sql and html injection

Cross-site scripting (XSS) Cross site scripting is a type of malicious web attack in which an external script is injected into the website’s code or output.
  • htmlspecialchar : converts hmtl codes into plain text and avoids html injection example ( htmlspecialchars($search, ENT_QUOTES, 'UTF-8');
  • htmlentities: as above but strip forign lngauge codes
  • strip_tags: removes html tags
References :-

File I/O

Php file functions

Php has a wide range of functions from file i/o

Example code line access mode

// access one line at a time <?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "

Example code

$item = $_POST['filename'];
$filename = $item ;
$file = fopen( $filename, "r" );
if( $file == false ) {
echo ( "Error in opening file::".$filename );
$filesize = filesize( $filename );
$filetext = fread( $file, $filesize );
fclose( $file );
$new = htmlspecialchars($filetext, ENT_QUOTES);
$finaltext = nl2br($new);
echo ( $finaltext ) ;
// access one line at a time <?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "