Professional Documents
Culture Documents
Index
What is PHP ?
Basic Syntax
Types of Error
Constants
Operator Types
Functions
Global Variables
What is PHP?
PHP stands for PHP: Hypertext Preprocessor.
PHP is a server-side scripting language, generally used to build web
applications
PHP scripts are executed on the server
PHP supports many databases (MySQL, Informix, Oracle,
PostgreSQL, Generic ODBC, etc.)
PHP is an open source which is easy to use & free to download
More in PHP is on http://www.php.net/manual/
Basic Syntax
Commenting
<?php
// This is single line comment
/*
echo This is multiline comment;
*/
?>
Type of Errors
Fatal Errors : These errors are critical errors. It results termination of script
execution
PHP Variables
Declaration of PHP variable starts with $ symbol.
e.g. $variable_name = value;
Variable Naming Rules : A variable name can only contain alpha-numeric characters and
underscores (a-Z, 0-9, and _ )
A variable name should not contain spaces.
Variables are case-sensitive
If a variable name is more than one word, it should be separated
with underscore
PHP Arrays
Numeric Array : Arrays with a numeric index
$vehicleArray = array(car, bus);
Associative Array : Arrays with named keys
$ages = array("Jon"=>26, "Arya"=>20, "Sansa"=>24);
Multidimensional Array : Arrays containing one or more arrays
$familyArray = array(Eddard=>array(Rob, Jon, Arya),
Lannister=>array(Jammie, Tyrion, Cersei));
PHP Strings
String variables are used for values that contains character strings.
e.g.
$string = Hello World;
For merging strings concatenation operator(.) is used
e.g.
$string1 = Hello;
$string2 = World;
echo $string1. .$string2 // Output Hello World
PHP Constants
Constant can be defined using define.
Constant's value can not be altered.
e.g.
define(CONSTANT, 'php');
echo CONSTANT;
//prints php
Operator Types
PHP language supports following type of operators.
Arithmetic Operators (+,-,*,%)
Comparison Operators (==, ===, !=, !==, <, > <=, >=)
Logical (or Relational) Operators (and, or, &&, ||, !)
Assignment Operators (=, +=, -=, *=, /=, %=)
Conditional (or ternary) Operators ( ? : )
Type of Loops
PHP supports following four loop types
for :- loops through a block of code a specified number of times.
while :- loops through a block of code if and as long as a specified
condition is true.
do...while :- loops through a block of code once, and then repeats the
loop as long as a special condition is true.
foreach :- loops through a block of code for each element in an array.
continue and break keywords can be used to control loop execution.
?>
$i = 0;
do {
print $i;
} while ($i > 0);
print $i;
}
Foreach Loop
$arr = array(1, 2, 3, 4);
foreach ($arr as $value) {
echo $value;
}
Switch Statement
switch (n)
{
case label1:
break;
case label2:
break;
default:
Functions
Superglobals
PHP superglobal variables are : $GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
Reference Links
http://www.php.net/manual
http://www.w3schools.com/php/default.asp
http://www.tutorialspoint.com/php/
Thank You