Você está na página 1de 54

Evolution of PHP

PHP gained popularity with Web developers around 1998


PHP was created by Rasmus Lerdorf 1994

PHP started out as a set of simple tools coded in the C


language

PHP was to replace the Perl scripts Rasmus used on his


personal home page. (hence the original meaning of the
PHP acronym).

Evolution of PHP

He released PHP to the general public in 1995,


and called it PHP version 2.

Two more developers, Zeev Suraski and Andi


Gutmans, joined in 1997.

They rewrote most of PHP and, along with


Rasmus, released PHP version 3.0 in June 1998.

Evolution of PHP Cont

Zeev and Andi rewrote the PHP core yet again, calling
it the Zend Engine .

The new version, PHP 4, was launched in May 2000.

PHP 5 was launched in 2004 under Zend Engine 2.

PHP 5 added support for OOP

What is PHP ?

PHP stands for PHP: Hypertext Pre-processor

PHP is a scripting language for building


dynamic, interactive Web sites.

PHP is a server - side scripting language,


which means that PHP scripts, or programs,
usually run on a Web Server

Dynamic Pages

A dynamic Web page is a page whose


contents can change automatically each
time the page is viewed.

An interactive Website is a site that


responds to input from its visitors

A Web forum is a good example

Scripting Language

Runs in response to events.

Runs from top to bottom.

Usually interpreted by another software.

Uses of PHP

Reading and processing the contents of a Web


form sent by the visitor.

Reading, writing, and creating files on the Web


server .

Working with data in a database stored on the


Web server.

Uses of PHP

Grabbing and processing data from other Web


sites and feeds.

Generating dynamic graphics, such as charts and


manipulated photos.

Once its finished processing, it can send a


customized HTML Web page back to the visitor.

Why Use PHP?

A large number of Internet service providers (ISPs) and


Web hosting companies that support it

Hundreds of thousands of developers are using PHP

PHP is that is cross platform; to develop, to deploy


and to use

Powerful and Robust

Supports OOP

Basic Syntax and Statements


PHP syntax is very familiar to that of
JavaScript, C, C++, C#, Objective-C, Java, Perl,
or any other C-derived language.
echo "Hello world";

Incorporating PHP within HTML

PHP documents end with the extension .php

When a web server encounters this


extension in a requested file, it automatically
passes it to the PHP processor.

A PHP program is responsible for passing


back a clean file suitable for display in a web
browser.

Calling the PHP Parser

To trigger the PHP commands, a new tag has to be


learned. The first part is:

<?php
and the last part is :
?>

A small PHP Hello World program might look like:


<?php
echo "Hello world";
?>

Using Comments

There are two ways in which comments


can be added to PHP code.

Single Line comments

Multiline comments

Using Comments Cont


Single Line comments:

// This is a Comment.

# This is another Comment.

Using Comments Cont


Multiline comments:

/*This is
a MultiLine
Comment. */

Variables

Variable names can include letters,


numbers, and the underscore character
( _ ) , but they cannot include spaces.

Legal variables might look like:


$a;
$a_longish_variable_name;
$_2453;
$sleepyZZZZ;

Variables Contd

Super Global Variables

Examles:

$_GET: contains any variables provided to a script


through the GET method.
$_POST: contains any variables provided to a script
through the POST method.
$_FILES: contains any variables provided to a script
through file uploads.

Constants

A value, once defined, is set for the


remainder of the program and cannot be
altered.

define("ROOT_LOCATION",
"/usr/local/www/");
$directory = ROOT_LOCATION;

Constants Cont

PHP comes with dozens of predefined constants.

Examples:
__LINE__ : The current line number of the
file.

__FILE__ : The full path and filename of the


file. If
used inside an include, the name
of the
included file is returned.

Constants Contd
Example :
echo "This is line ".__LINE__." of file
".__FILE__;

Arrays
There are two types of array :

Basic Array

Associative Array

Arrays Cont
Basic Array :

$myArray = array ('one', 2, '3');

$myArray[1] = 'two';

$myArray[] = 'the fifth element';

Arrays Cont
Associative Array :
$birthdays =
array('Kevin' => '1978-04-12', Amy'
=>'1980-05-16);

$birthdays['Kevin'] = '1978-04-12';
echo 'My birthday is: '. $birthdays['Kevin'];

Data Types

PHP is a loosely typed language.

Type Example
1. Boolean true
2. Integer 5
3. Float or Double
4. String hello
5. Object
6. Array
7. NULL

3.234

Data Types
Examples of test functions :

is_null();
is_int();
is_double();
is_array();
is_bool();

<?php
$testing;
echo "is null? ".is_null($testing);
?>

Arithmetic Operators
Operator

Description

Addition

Subtraction

Multiplication

/
%

Division
Modulus

++

Increment

Decrement

Example

$j + 1
$j - 6
$j * 11
$j / 4
$j % 9
++$j
$j

Assignment Operators
Operator
=
+=

Example

$j =15
$j +=5

-=

$j -=3

*=

$j *=8

/=
.=
%=

$j /=16
$j .=$k
$j %=4

Equivalent to

$j = 15
$j = $j + 5
$j = $j - 3
$j = $j * 8
$j = $j / 16
$j = $j . $k
$j = $j % 4

String Concatenation

String concatenation uses the period (.)


operator to append one string of characters
to another.

echo "You have " . $msgs . "


messages.";

Comparison Operators
Operator Description Example
== Is equal to

$j ==4

=== Is identical to

$j === 4

!= Is not equal to

$j !=21

>

Is greater than

<

Is less than

$j >3

$j <100

>= Is greater than or equal to


<=

Is less than or equal to

$j >=15
$j <=8

Comparison Operators
Operator

Description Example

&& And $j == 3 && $k == 2


and Low-precedence and $j == 3 and $k == 2
|| Or
or

$j < 5 || $j > 10

Low-precedence or $j < 5 or $j > 10

! Not ! ($j == $k)


xor

Exclusive or

$j xor $k

Control Structures (if Statement)


if (expression) {
// code to execute if the expression
evaluates to true
}

if else Statement
if (expression) {
// code to execute if the expression
evaluates to true
} else {
// code to execute in all other cases
}

if elseifelse Statement
if (expression) {
// code to execute if the expression
evaluates to true
} elseif (another expression) {
// code to execute if the previous
expression failed
// and this one evaluates to true
} else {
// code to execute in all other cases
}

if elseifelse Statement ( Contd)


<?php
$mood = "sad";
if ($mood== "happy")
{
echo "Hooray! Im in a good mood!
} elseif ($mood== "sad") {
echo "Awww. Dont be down!";
} else {
echo "Im neither happy nor sad, but
$mood.";
}
?>

switch Statement
switch (expression) {
case result1:
// execute this if expression results in result1
break;
case result2:
// execute this if expression results in result2
break;
default:
// execute this if no break statement
// has been encountered hitherto
}

switch Statement Contd


<?php
$mood = "sad";
switch ($mood)
{
case "happy":
echo "Hooray! Im in a good mood!";
break;
case "sad":
echo "Awww. Dont be down!";
break;
default:
echo "Im neither happy nor sad, but {$mood}.";
break;
}
?>

Control Structures (Loops)

while Loop

for Loop

Support continue and break

while Loop
while (expression) {
// do something
}
<?php
$counter = 1;
while ($counter <= 6) {
echo $counter." times 2 is ".($counter * 2)."<br />";

$counter++;
}
?>

for Loop
for (initialization expression; test expression; modification
expression) {
// code to be executed
}

<?php
for ($counter=1; $counter<=6; $counter++) {
echo $counter." times 2 is ".($counter* 2)."<br/>";
}
?>

Functions

A function is a self-contained block of


code that can be called by scripts.

When called, the functions code is


executed and performs a particular
task.

Functions Contd
Two types of Functions

Built in functions

User defined functions

Functions Contd
Built in functions

some_function($an_argument,$another_argument);

strtoupper("Hello Web!");

$new_string = strtoupper("Hello Web!");

Functions Contd
User defined functions
function
some_function($argument1,$argument2) {
//function code here
}

Functions Contd
User defined functions
function printMe($txt)
{
echo $txt."<br/>";
}

Functions Contd
User defined functions
<?php
function addNums($firstnum, $secondnum)
{
$result= $firstnum + $secondnum;
return $result;
}
echo addNums(3,5); //will print 8
?>

ADVANCED PHP
Class :
Define what an object looks like, what
information it can store, and what
actions and calculations it can perform.

class MyClassname {
}

OOP IN PHP
Properties:

Class variables, called properties, are where you


put information that is specific to the object.

The declaration starts with the scope keyword


(public, protected, or private) followed by the
property

public someProperty = true ;

OOP IN PHP
Methods:

A method is a function in a class.


public function myMethod($input)
{
// php code
}

OOP IN PHP
Methods:
To access a property of the class in a method use:
$this->someProperty

public function myMethod{


$this->someProperty = fasle;
$this->otherMethod() ;
}

OOP IN PHP
Constructors:

The constructor method in a class


automatically launches as soon as the class
is instantiated.

A unique feature of PHP classes is the use of


the __construct() statement as a
constructor function

OOP IN PHP
Constructors:

public function __construct() {


$this->someProperty = fasle;
}

Interacting with Database


Three API s for interacting with databases:

mysql

PDO

mysqli

Você também pode gostar