Embed
Email

Php

Document Sample
Php
Shared by: Ranjani Silva
Stats
views:
3
posted:
2/9/2012
language:
pages:
40
Introduction to PHP





PHP Fundamentals

What is PHP?

 PHP stands for "PHP Hypertext Preprocessor”

 PHP is a server-side scripting language designed

specifically for the Web.

 An open source language

 PHP code can be embedded within an HTML page,

which will be executed each time that page is visited.

 Example code (all equivalent):

 Short Style:

 XML Style:

 Script Style: echo “Hello

World!”;

 ASP Style:

History of PHP



 Created by Rasmus Lerdorf in 1995 for tracking

access to his resume

 Originally a set of Perl scripts known as the

“Personal Home Page” tools

 Rewritten in C with database functionality

 Added a forms interpreter and released as

PHP/FI: includes Perl-like variables, and HTML

embedded syntax

Processing a PHP Page

Performance





 Zdnet Statistics

 PHP pumped out about 47 pages/second

 Microsoft ASP pumped out about 43 pages/second

 Allaire ColdFusion pumped out about 29 pages/second

 Sun Java JSP pumped out about 13 pages/second



* From PHP HOWTO, July 2001

Features of PHP

 Very Efficient – Can serve millions of hits per day.

 Database Integration – Supports many databases, such as

mySQL and Oracle. Also has excellent XML support as of PHP 5.

 Built-in Libraries – Tailored to web development, one can

connect to other network services, send email, work with cookies,

generate PDF documents, and make GIF images on the fly all with a

few lines of code.

 It’s Free – Available on http://www.php.net

 Easy to Learn – Very similar in syntax to C/C++/Java and Perl.

 Portable – Works on Unix based operating systems, on Mac OS X, as

well as on versions of Microsoft Windows. Your PHP code will often

work without modification on a different system running PHP.

What is PHP Good For?



 It is great for complex web page designs

 E-commerce sites with heavy traffic (ex. Amazon)

 Complex bulletin boards and forums (ex. phpBB)

 Secure websites (ex. Novasis)

 Email web hosts (ex. Gmail)

 Working with and integrating XML into your webpage

 Database management and search (ex. theFaceBook)

Differences From Java



 Data types are not required in variable

declarations.

 The $ symbol precedes all variables in PHP

 Constants are declared using the define()

method in PHP: ex. define(“AOL", "something");

 Constructors do not necessarily have to be

the same name as the class name.

 Destructors are used in PHP to remove

objects from memory after they are

constructed.

The Basics

The Results



 Output from a script

goes directly into the

HTML that is parsed

 This is what is meant

by a ‘dynamic’

webpage

Variables





 Variables in PHP are represented by a dollar

sign followed by the name of the variable.

The variable name is case-sensitive.

 Variable names follow the same rules as

other labels in PHP. A valid variable name

starts with a letter or underscore, followed by

any number of letters, numbers, or

underscores.

Example Script





Data Types



 Four scalar types:

 boolean integer double string

 Two compound types:

 array object

 The type of a variable is usually not set by the

programmer; rather, it is decided at runtime by

PHP depending on the context in which that

variable is used.

 To get the data type of some particular variable, we

can use gettype() function.

Example Script

";

echo gettype($vardouble),"";

echo gettype($varstring1),"";

echo gettype($varstring2),"";

echo gettype($varbool),"";

?>

PHP Language Basics



 Constants, Data Types and Variables

 Operators

 Contains all of the operators like in C and Perl (even the

ternary)

 Statements

 if, if/elseif

 Switch/case

 for, while, and do/while loops

 Include and require statements for code reuse

PHP Language Basics

 Arithmetic Operators

 $m=$a + $b ; $n= $a - $b ; $p= $a * $b ; $q=$a / $b;

 $r=$a % $b;

 Assignment Operators

 $a = 4; $b = $a;





 Comparison Operators

 $a == $b $a != $b $a $b $a =

$b

 Increment & Decrement Operators

 PHP also supports the standard increment and decrement

operators:

PHP Language Basics



 Concatenating Strings

 The "." operator concatenates two values:

$sentence_a = "statement 1 ";

$sentence_b = " statement 2”

$sentence_c = $a . $b;

PHP Language Basics



 if (condition here) { }

 if (condition here) { }

else { }





 if (condition here) { }

elseif ( another condition here ) { }

elseif ( yet another condition here ) { }

else { } //none of the other conditions

PHP Language Basics

switch(condition variable to check)

{ case "true":

do some stuff here;

break;

case "another condition is true":

do some more processing here;

break;

default: // no above

conditions are true

perform some default processing

here...;

break;

}

PHP Language Basics



WHILE (condition here) {}



FOR (first condition, test

condition, increment

condition ) { }

PHP Language Basics



 Hello World!: An Example

 There is more than one way to do it





$greeting = “Hello World!”

printf(“%s”, $greeting);



$hello = “Hello”;

$world = “World!”;

print $hello . $world;



php?>

Constants & Data Types



 Constants define a string or numeric

value

 Constants do not begin with a dollar

sign

 Examples:

 define(“COMPANY”, “Acme Enterprises”);

 define(“YELLOW”, “#FFFF00”);

 define(“PI”, 3.14);

 define(“NL”, “\n”);

Constants & Data Types



 Using a constant

 print(“Company name: “ . COMPANY . NL);

 Data types

 Integers, doubles and strings

 isValid = true; // Boolean

 25 // Integer

 3.14 // Double

 ‘Four’ // String

 “Total value” // Another string

Constants & Data Types



 Data types

 Strings and type conversion

 $street = 123;

 $street = $street . “ Main Street”;

 $city = ‘Naperville’;

$state = ‘IL’;

 $address = $street;

 $address = $address . NL . “$city, $state”;

 $number = $address + 1; // $number equals 124

Introduction to PHP





Array Processing

Creating an array

 There are more ways to create an array in PHP.





$colorList = array("red","green","blue","black","white");







$colorList[] = "red"; $colorList[0] = "red";

$colorList[] = "green"; $colorList[1] = "green";

$colorList[] = "blue"; $colorList[2] = "blue";

$colorList[] = "black"; $colorList[3] = "black";

$colorList[] = "white"; $colorList[4] = "white";

Display the array content

 If you want only display one element of the array then you can just

write the following code

echo $colorList[0];



 You can write a loop and display all the elements like this:

for ($i=0;$i"red",

"grass"=>"green",

"sky"=>"blue",

"night"=>"black",

"wall"=>"white");



 Array keys are case-sensitive, but type insensitive.

 It means that 'a' differs from 'A' but '1' is the same as 1.



$colorList["apple"] = "red";

$colorList["grass"] = "green";

$colorList["sky"] = "blue";

$colorList["night"] = "black";

$colorList["wall"] = "white";

Associative arrays



echo "The sky is ".$colorList["sky"]

." and the grass is ".$colorList["grass"];



 You can mix your array and use numbers and strings in

the same list like this:

$colorList["apple"] = "red";

$colorList[5] = "green";

$colorList["sky"] = "blue";

$colorList["night"] = "black";

$colorList[22] = "white";



 As you can see even the numbers can be any so you don't

have to make it continuous. However be aware of using

such mixed arrays as it can result errors.

Multidimensional arrays

$myLists['colors'] = array("apple"=>"red",

"grass"=>"green",

"sky"=>"blue",

"night"=>"black",

"wall"=>"white");



$myLists['cars'] = array("BMW"=>"M6",

"Mercedes"=>"E 270 CDI",

"Lexus"=>"IS 220d",

"Mazda"=>"6",

"Toyota"=>"Avensis");

 To acces and display an element in the multidimensional array you just

extend the key list as follows:

echo $myLists['cars']['Toyota'];

Introduction to PHP





File Handling

fopen( )

 fopen() has two main input fields. The file and the opening mode.

Here are the opening modes that you can specify:

 'r' - Open for reading only; place the file pointer at the beginning of

the file.

 'r+' - Open for reading and writing; place the file pointer at the

beginning of the file.

 'w' - Open for writing only; place the file pointer at the beginning of

the file and truncate the file to zero length. If the file does not exist,

attempt to create it.

 'w+' - Open for reading and writing; place the file pointer at the

beginning of the file and truncate the file to zero length. If the file

does not exist, attempt to create it.

 'a' - Open for writing only; place the file pointer at the end of the

file. If the file does not exist, attempt to create it.

 'a+' - Open for reading and writing; place the file pointer at the end

of the file. If the file does not exist, attempt to create it.

Sample Code



Open and Write to a File



$my_file = 'file.txt';

$handle = fopen($my_file, 'w') or

die('Cannot open file:

'.$my_file);

$data = 'This is the data';

fwrite($handle, $data);

fclose($handle);

Append to a File



$my_file = 'file.txt';

$handle = fopen($my_file, 'a') or

die('Cannot open file: '.$my_file);

$data = "\n".'New data line 1';

fwrite($handle, $data);

$new_data = "\n".'New data line 2';

fwrite($handle, $new_data);

fclose($handle);

Read a File





$my_file = 'file.txt';

$handle = fopen($my_file, 'r');

$data =

fread($handle,filesize($my_file));

print "file contents:";

print $data;

fclose($handle);

Read Line by Line & Deleting a file



$handle = fopen($my_file, 'r') or die('Cannot open

file: '.$my_file);



echo " reading line by line..";



while (!feof($handle)){

$line = trim(fgets($handle));

echo "line content: $line"."";



}

fclose($handle);



unlink($my_file);

Introduction to PHP





Form Processing

Example Form





Your name:

Your age:





Form Processing



Welcome!!!

.

You are

years old.



 To get the values of those input items at PHP code,

we can use $_POST['InputName'] syntax. It will

return the value entered by user to that particular

input field as String.

 Modify $-POST into $_GET and see the difference in

the url.


Related docs
Other docs by Ranjani Silva
Asp .net
Views: 12  |  Downloads: 0
processes
Views: 0  |  Downloads: 0
Artificial Intelligence
Views: 11  |  Downloads: 0
Php
Views: 3  |  Downloads: 0
Linq
Views: 2  |  Downloads: 0
BNF
Views: 1  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!