You are on page 1of 7

PHP Quick Start Guide

Document Version 6 (April 2016)


Requirements
1. Web Server (Internet Information Services (IIS), Apache, Nginx, etc...)
2. Database Server (MySQL, Postgresql, Microsoft SQL Server, etc...)
3. Text Editor (Sublime, Notepad, Notepad++, Visual Studio Code, etc...)
4. Web Browser (Edge, Chrome, Firefox, Safari, Opera, etc...)
*The PHP syntax looks like C++.
PHP Open and Close Tag
<?php and ?>
HTML Comment
<!-- Comments here ->
PHP Comment
// single line comment
or
/*
Multi-line
comment
*/
Output String
echo Hello PHP;
Difference Between Single Quote and Double Quote
Single quote means the data including the variable inside is considered as string
$var = PHP;
echo Welcome to $var.;
*This would output Welcome to $var.
Using double quote would parse the variable inside
$var = PHP;
echo Welcome to $var.;
*This would output Welcome to PHP.
Include External File
include(filename.php); - the codes below this line would still be executed even If the file
doesn't exist
1
PHP Quick Start Guide Version 6 (April 1, 2016)

or
require(filename.php) ; - PHP would output an error if the file doesnt exist
PHP Variables
Always starts with a dollar symbol ($var)
PHP Lines of Codes
Every line always ends with a semi-colon (;)
Header (Redirection)
header(Location: http://www.hubnob.com);
*Must be used when nothing is written in the page
Assignment Operator
Use = symbol to assign a value
$var = 1;
Arithmetic Operators
( + ) addition
( - ) subtraction
( * ) multiplication
( / ) division
( % ) - modulus (remainder)
Comparison Operators
( == ) - equal to
( != ) - not equal to
( < ) - less than
( > ) - greater than
( <= ) - less than or equal to
( >= ) - greater than or equal to
Logical Operators
( ! ) not
( && ) and
( || ) - or
Combination Arithmetic and Assignment Operators
( += ) - plus equals ($x += 2 means $x = $x + 2)
( -= ) - minus equals ($x -= 2 means $x = $x 2)
( *= ) - multiply equals ($x *= 2 means $x = $x * 2)
( /= ) - divide equals ($x /= 2 means $x = $x / 2 )
( %= ) - modulo equals ($x %= 2 means $x = $x % 2)
( .= ) - concatenate equals ($var.=hello edison means $var = $var .hello edison)
Pre / Post Increment / Decrement
( $var++) - same as $var += 1 or $var = $var + 1
( $var--) - same as $var -= 1 or $var = $var 1
2
PHP Quick Start Guide Version 6 (April 1, 2016)

Concatenation
Use period (.) to join two or more string or variables
Common Variables
$_POST[var] get value from form via post method
$_GET[var] get value from form via get method
$_SERVER[HTTP_REFERER] get the url of the referrer
Cookie (Commonly used for Remember Me features)
*Cookie can stay on the browser even for weeks or months
Set Cookie
setcookie(name, value, expire, path, domain);
ex. setcookie(user, Edison Go Tan, time()+3600);
Get Cookie
$user = $_COOKIE[user];
Delete Cookie
setcookie(user, , time()-3600);
*Set expiry to one hour ago
Session (Commonly used for keeping temporary data like login info)
*Session is deleted when the user closed the web browser
Requirements
session_start();
*Must be placed before the <HTML> tag
Storing A Session Data
$_SESSION[name] = 1;
Retrieve Session Data
$name = $_SESSION[name];
Cleaning and Destroying Session
unset($_SESSION[name]);
or
session_destroy();
*Destroy all session data (reset)

3
PHP Quick Start Guide Version 6 (April 1, 2016)

Conditional Statements
If Statement
if($var){
statement;
}
*If you put a variable ($var for example) in the condition, the return value would be either
TRUE or FALSE. If the variable contains any data then it returns TRUE
If Else Statement
if($var == 1){
statement;
} else {
statement;
}
*You use == symbol to compare
*Using single = symbol means you are moving the data to the variable to the left which in
return returns TRUE
Else If with If Else Statement
if(condition){
statement;
} elseif(condition){
statement;
} else {
statement;
}
For Loop
for($x=0; $x<10; $x++){
statement;
}
*Initialization; Condition; Increment or Decrement
For Each
$var[edison] = 1;
$var[rachel] = 2;
$var[jc] = 3;
$var[stephanie] = 4;
foreach($var as $name => $num){
echo The name is .$name. and the number is .$num;
}
While Loop
while(condition){
statement;
}

4
PHP Quick Start Guide Version 6 (April 1, 2016)

Do While Loop
do{
statement;
} while(condition);
Switch Statement
switch($var){
case edison: statement;
break;
case rachel: statement;
break;
default: statement;
break;
}
PHP Functions
function myFunctionName(){
statement;
}
*Calling myFunctionName() would execute the statement inside

function myFunctionName($var){
echo $var;
}
*You can put parameter inside the parenthesis
myFunctionName($x, $y){
$z = $x + $y;
return $z;
}
$finalvar = myFunctionName(1, 2);
echo $finalvar; - this would output the sum of $x and $y which is 3
*You can also return value from the function
Numerically Indexed / Associative Array
$var[0] = Edison;
$var[1] = Rachel;
or
$var[0][0] = Wensh;
$var[0][1] = Stephanie;
or
$var[edison] = 2000;
5
PHP Quick Start Guide Version 6 (April 1, 2016)

$var[meg] = 2000;
Other Commonly Used Functions
trim($var) - used to remove extra spaces from left and right
strip_tags($var) - used to remove html, javascript and php tags
stripslashes($var) - returns a string with backslashes stripped off while double
backslashes( \\ ) are made into single backslash ( \ )
isset($var) - used to check if the variable has been set
Resources
Web Servers and PHP
Apache - http://httpd.apache.org
Nginx - http://www.nginx.org
LightTPD - http://www.lighttpd.net
PHP - http://www.php.net
Cloud Providers
Microsoft Azure http://azure.microsoft.com
Amazon EC2 http://www.amazon.com/aws
Rackspace Cloud - http://www.rackspace.com
Softlayer - http://www.softlayer.com
Database and Tools
MySQL (Database) - http://www.mysql.com
SQLyog (Database Windows GUI)- https://github.com/webyog/sqlyog-community
PHPMyAdmin (Database Web Based GUI)- http://www.phpmyadmin.net
PostgreSQL (Database) - http://www.postgresql.org
PHPPgAdmin (Database Web Based GUI) - http://phppgadmin.sf.net
MariaDB (Database) - http://www.mariadb.org
MongoDB (Database) - http://www.mongodb.org
Cassandra (Database) - http://cassandra.apache.org
Integrated Development Environment and Text Editors
Sublime (All Platform) - http://www.sublimetext.com
Microsoft Visual Studio and Microsoft Visual Studio Code http://www.visualstudio.com
Notepad++ (Windows) - http://notepad-plus-plus.org
Eclipse PDT (All Platform) - http://www.eclipse.org/pdt
Netbeans (All Platform) - http://www.netbeans.org
Operating Systems
Red Hat Enterprise Linux - http://www.redhat.com
CENTOS (RHEL Free Version) - http://www.centos.org
Ubuntu (Based on Debian Linux) - http://www.ubuntu.com
MacOS X - http://www.apple.com/osx
FreeBSD - http://www.freebsd.org
Windows 10 http://www.microsoft.com
6
PHP Quick Start Guide Version 6 (April 1, 2016)

Automatic Installers (For Development Environment Only)


XAMPP - http://www.apachefriends.org/en/xampp.html
WAMP - http://www.wampserver.com/en
Microsoft Web Platform Installer - http://www.microsoft.com/web
PHP Frameworks
Laravel http://www.laravel.com
Symfony - http://www.symfony.com
Zend - http://framework.zend.com
Yii - http://www.yiiframework.com
PHP Based Content Management System (CMS)
Wordpress - http://www.wordpress.org
Drupal - http://www.drupal.org
Joomla - http://www.joomla.org
File Transfer
Filezilla Client - http://filezilla.sf.net

PHP Quick Start Guide v. 6


April 1, 2016 (Friday)
2016 Edison Go Tan
This guide is for beginners only.
For comments, suggestions or requests, please contact Mr. Edison Go Tan.
Email: edison@hubnob.com
Twitter: @edisongotan
Facebook: http://www.facebook.com/edisongotan

7
PHP Quick Start Guide Version 6 (April 1, 2016)

You might also like