Show Menu
Cheatography

Php for ever Cheat Sheet (DRAFT) by

A to Z

This is a draft cheat sheet. It is a work in progress and is not finished yet.

About Php

Before you continue:
you should have a basic unders­tanding of the following: HTML CSS JavaScript
PHP is an acronym for
"PHP: Hypertext Prepro­cessor"
PHP is
a widely­-used, open source scripting language
PHP is
free to download and use: www.ph­p.net
Php
PHP is a server scripting language
PHP files have extension
".ph­p"
PHP files can contain
text, HTML, CSS, JavaSc­ript, and PHP code

Basic PHP Syntax

A PHP script starts with <?php and ends with ?>:
<?php // PHP code goes here ?>
A PHP file normally contains HTML tags, and some PHP scripting code.
  <!D­OCTYPE html> <ht­ml> <bo­dy>  <h1­>My first PHP page</­h1>  <?php echo "­Hello World!­"; ?>  </b­ody> </h­tml>
Comments in PHP
// single­-line comment

Basic PHP Syntax

A PHP script starts with <?php and ends with ?>:
<?php // PHP code goes here ?>
A PHP file normally contains HTML tags, and some PHP scripting code.
  <!D­OCTYPE html> <ht­ml> <bo­dy>  <h1­>My first PHP page</­h1>  <?php echo "­Hello World!­"; ?>  </b­ody> </h­tml>
Comments in PHP
// or # are the single­-line comment And /* multip­le-­lines comment block */
PHP Case Sensit­ivity:
In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-d­efined functions are NOT case-s­ens­itive.However; all variable names are case-s­ens­itive.
Creating (Decla­ring) PHP Variables
variable starts with the $ sign, $x = 5;
Output Variables
echo statement: echo $x
PHP is a Loosely Typed Language
we did not have to tell PHP which data type the variable is. PHP automa­tically converts the variable to the correct data type, depending on its value.
 

The Point 1

PHP The static Keyword

Normally, when a function is comple­ted­/ex­ecuted, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.

To do this, use the static keyword when you first declare the variable: static $x = 0;

PHP 5 Data Types

PHP Data Types
String Integer Float (floating point numbers - also called double) Boolean Array Object NULL Resource
PHP Array
An array stores multiple values in one single variab­le.$­cars = array(­"­Vol­vo",­"­BMW­"­,"To­yot­a");
PHP Object
is a data type which stores data and inform­ation on how to process that data.
PHP NULL Value
Null is a special data type which can have only one value: NULL.$x = null;
PHP Resource
A common example of using the resource data type is a database call.

PHP 5 Constants

A constant is an identifier (name) for a simple value. The value cannot be changed during the script.
To create a constant, use the define() function.
Syntax
define­(name, value, case-i­nse­nsi­tive)
<?php
define­("GR­EET­ING­", "­Welcome to Amozeh.co­m!");
echo GREETING;
?>

PHP 5 Operators

=== Identical
$x === $y Returns true if $x is equal to $y, and they are of the same type
!== Not identical
Returns true if $x is not equal to $y, or they are not of the same type
PHP String Operators
PHP has two operators that are specially designed for strings.
. Concat­enation
$txt1 . $txt2 Concat­enation of $txt1 and $txt2
.= Concat­enation assignment
$txt1 .= $txt2 Appends $txt2 to $txt1
PHP divides the operators in the following groups:

Arithmetic operators
Assignment operators
Comparison operators
Increm­ent­/De­crement operators
Logical operators
String operators
Array operators
 

PHP Condit­ional Statements

The if Statement
if ($t < "­20") {     echo "Have a good day!"; 
The if...else Statement
if ($t < "­20") {     echo "Have a good day!"; } else {     echo "Have a good night!­"; } ?>
The PHP switch Statement
switch ($favc­olor) {     case "­red­":         echo "Your favorite color is red!";         break;    ...    default:         echo "Your favorite color isn't exists­!!"; }

PHP Loops

while - loops through a block of code as long as the specified condition is true
do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true
for - loops through a block of code a specified number of times
foreach - loops through a block of code for each element in an array
`<?php
$colors = array(­"­red­", "­gre­en", "­blu­e", "­yel­low­");
foreach ($colors as $value) {
echo "­$value <br­>";
}
?> `

PHP 5 Functions

The real power of PHP comes from its functions; it has more than 1000 built-in functions.
Besides the built-in PHP functions, we can create our own functions.
<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}

echo "5 + 10 = " . sum(5, 10) . "­<br­>";
echo "7 + 13 = " . sum(7, 13) . "­<br­>";
echo "2 + 4 = " . sum(2, 4);
?>

PHP 5 Arrays

An array stores multiple values in one single variable:
$cars = array(­"­Vol­vo", "­BMW­", "­Toy­ota­");
In PHP, there are three types of arrays:
Indexed arrays - Arrays with a numeric index Associ­ative arrays - Arrays with named keys Multid­ime­nsional arrays - Arrays containing one or more arrays
Indexed Arrays
$cars = array(­"­Vol­vo", "­BMW­", "­Toy­ota­");
OR
$cars[0] = "­Vol­vo"; $cars[1] = "­BMW­"; $cars[2] = "­Toy­ota­";
The count() function is used to return the length (the number of elements) of an array:
 <?php $cars = array(­"­Vol­vo", "­BMW­", "­Toy­ota­"); echo count(­$cars); ?> 
Loop Through an Indexed Array
 <?php $cars = array(­"­Vol­vo", "­BMW­", "­Toy­ota­"); $arrlength = count(­$cars);  for($x = 0; $x < $arrle­ngth; $x++) {     echo $cars[$x];     echo "­<br­>"; } ?> 
Associ­ative Arrays
Associ­ative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associ­ative array:
$age = array(­"­Pet­er"=­>"35­", "­Ben­"­=>"3­7", "­Joe­"­=>"4­3");
OR
 $age['­Peter'] = "­35"; $age['­Ben'] = "­37"; $age['­Joe'] = "­43"; 
Loop Through an Associ­ative Array
 <?php $age = array(­"­Pet­er"=­>"35­", "­Ben­"­=>"3­7", "­Joe­"­=>"4­3");  foreac­h($age as $x => $x_value) {     echo "­Key­=" . $x . ", Value=­" . $x_value;     echo "­<br­>"; } ?> 
Multid­ime­nsional arrays will be explained in the PHP advanced section.