Вы находитесь на странице: 1из 23

Introduction to PHP

Index

What is PHP ?

Installations & Configurations

Basic Syntax

Types of Error

Variables, Arrays & Strings

Constants

Operator Types

Types of Loop, Conditions & Switch Case

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/

Request Response in PHP

Installation & Configurations

Download from http://www.wampserver.com/en/

WAMP server automatically installs whatever you need to run PHP


application

In case you need to change default configuration use following


files : php.ini ( For PHP related configurations )
my.ini ( For MySQL related configurations )
httpd.conf ( For Apache server related configurations )

PHP.ini is very useful and it is a configuration file that is used to


customize behaviour of PHP at runtime.
The configuration file (php.ini) is read when PHP starts up.

We can modify settings like upload directory, register global


variables, display errors, log errors, max uploading size, maximum
time to execute a script and other configurations using this file.

Basic Syntax

PHP scripts starts with <?php and ends with ?>


Server where shorten tags are enabled, scripts can be starts with
<? and ends with ?>
Kindly refer example below : <?php echo Hello PHP ?>
<? echo Hello PHP with short tag ?>

Commenting

PHP single line & multi line comments

Single line comments can be done by //

Multiline comments starts with /* and ends with */

<?php
// This is single line comment
/*
echo This is multiline comment;
*/
?>

Type of Errors

Notices : These are non-critical errors which PHP encounters while


running a script.
e.g. A variable accessibility before it is declared.

Warnings : The are more serious errors.


e.g. using include()without the existence of the file.

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.

IfElse if Else Statement


if (condition)
code to be executed if condition is true;
else if(condition1)
code to be executed if condition1 is true;
else
code to be executed if both conditions false;

While & Do While


<?php
$a=0;
while($a<10) {
print $a; $a++;
}

?>

$i = 0;
do {
print $i;
} while ($i > 0);

For Loop / Foreach Loop


For Loop
for ($i = 1; $i <= 10; $i++) {

print $i;
}

Foreach Loop
$arr = array(1, 2, 3, 4);
foreach ($arr as $value) {

echo $value;
}

Switch Statement
switch (n)
{
case label1:

code to be executed if n=label1;

break;
case label2:

code to be executed if n=label2;

break;
default:

code to be executed if n is different from both label;


}

Functions

A functions is a block of statements that can be used repeatedly


in a program
A function will not execute immediately when a page loads
A function will be executed by a call to the function
PHP has many built-in functions which will be very useful
We can also create user defined functions
Syntax :function functionName(){
Code to be executed
}

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

Вам также может понравиться