Basics of Php

Post on 14-Apr-2015

36 views 0 download

description

Brief Introduction to PHP Scripting

Transcript of Basics of Php

Module-1:

Software Installation

XAMPP:

http://www.apachefriends.org/en/xampp.html

PHP Editor:

http://download.cnet.com/PHP-Designer-2007-Personal/3000-10248_4-10575026.html

Syllabus:

http://www.webtrainings.in/php-training-hyderabad.html

Module-2:

Php Introduction

• PHP stands for Personal Homepage Tools

• PHP also known as Hypertext PreProcessor.

• PHP is a server-side scripting language, like ASP

• PHP scripts are executed on the server

• PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic

ODBC, etc.)

• PHP is an open source software

• PHP is free to download and use

• PHP runs on different platforms (Windows, Linux, Unix, etc.)

• PHP is compatible with almost all servers used today (Apache, IIS, etc.)

• PHP is easy to learn and runs efficiently on the server side

• PHP designed for producing dynamic web pages.

Famous sites developed in php:

facebook

amazon

ebay.com

web Application Architecture:

In Web-application we use different scripts in different layers.

1._ Layer 1 (Front End)

a._ HTML--------->to create forms

b._ JavaScript--->to validate form fields

2._ Layer 2 (Middle tier)

a._ PHP---------->to process form data.

3._ Layer 3 (Back End)

a._ MySQL-------->to store user data after processing for later use.

Php Program:

PHP is scripting language. All programing statements written inside PHP tags.It also supports HTML tags inside php code.

ex: <?php

statement1;

statement2;

?>

Example:

<?php

print "Hello world";

?>

print "<h1>Hello world";

Introduction of Variables

Deffination:

Named memory locations to store and retrive data.

In PHP all variabales starts with '$' sign.

The variable name is case-sensitive.

ex:

$city="Hyderabad";

$City="Delhi";

both are different

variable naming rules:

• it can be any length.

• should not start with number,or any special characters

• should not contain spaces.

• it accepts only '_' under score.

Using variables

<?php

$name = 'Bill';

$address="US";

echo $name, $address; //echo allows commas(,)

?>

Number variable

<?php

$n = 10;

print ($n);

?>

joining with dot operator

<?php

$n = 10;

$text = 'n = ';

print ($text . $n); // print uses dot(.) for contatinating string with variable

?>

<?php

$a =10;

$b =20;

$c =$a+$b;

print ("sum=" . $c );

?>

constant:

• constant defind using define()-function.

• Once a constant is defined, it can never be changed.

• constants does not have '$'.

<?php define("MAXSIZE", 100);

echo MAXSIZE;

define("MAXSIZE", 50);

echo MAXSIZE; //---> 100

define("COMPANY", "Acme Enterprises"); define("YELLOW", "#FFFF00"); define("PI", 3.14); define("NL", "<br>"); //--NL(NewLine)

print "Hello".NL."world";

?>

Understanding Data Types

• Booleans

• Integers

• Floating point numbers

• Strings

• Arrays

• Objects ex: Date

<?php

$firstName="Harry"; // a PHP String

$lastName="Pottor"; // a PHP String

$age=18; // a PHP Integer $height=5.4; // PHP Float $hasDriversLicense=True; // a PHP Boolean

?>

Writing Statements and Comments:

each statement ends with semicolon

Debugging – Feature: PHP is not a strongly typed language(i.e., variables must be declared in the begining before

they are going to use in program)

· In PHP Variables can be created anywhere in your code but case sensitive

Ex:

$name and $Name both are different

comments:

// ---->single line comment

multl ine comment

/* ....

....*/

Running program in a server

server runs php programs and sends result back to the html page.

Testing server

test.php

<?php

phpinfo();

?>

ouput:

Displays system info.

save it in htdocs(if your server is XAMPP). or in www folder (if your server is WAMP)

start server

open the above file in Browser using the following url :

http://localhost/test.php

Embedding HTML in PHP

Ex:

test.php

<?php

print ("<h1>Hello World</h1>");

?>

Embedding PHP in HTML

Hello.php

* if we use php code in HTML it must be stored as .php

<html>

<head>

<title>PHP Test</title>

</head>

<body>

<?php echo '<p>Hello World</p>'; ?>

</body>

</html>

Using Operators

1. Arithmetic Operators

+,-,*,/,%

<?php

print (10 + 20 + 30);

?>

Ex2:

<?php

$first_number = 10;

$second_number = 20;

$total = $first_number + $second_number;

$direct_text = 'The two variables added together';

print ($direct_text . $total);

?>

Operator Precedence

order of evaulation of an expression

1) / * %

2) + -

3) =

ex:

a*(b+c/d)*22 the

correct way is

(a*b)+(c/d)*22

according our expression first it goes to division and goes to multiplication finally addition at last the

result is printed.

ex: a=2,b=3,c=8,d=2

what would be the result.

(2*3)+(8/2)*22

6+4*22

6+88

=94

ex2:

$total =($m1+$m2+$m3)/3;

Assignment Operators

=

+=

ex:

$a=10;

$a=$a+2; //also written as follows

$a+=2; // adds 2 to $a values and stores in the same $a.

-=

*=

/=

ex:

$i=10;

print i;

i+=5;

print i;

Incrementing/Decrementing Operators

++

--

i=10;

i++;

i--->11 i value incremented by 1

i-- 10 i value decremented by 1

Comparison Operators

> greater than

< less than

<= lessthan or equalto

>= greaterthan or equalto

!= not equal to

== equalto

Logical Operators

and---->and

or ----> or

! ---->not

String Operators

<?php

$a = "Hello ";

$b = $a . "World!"; // now $b contains "Hello World!"

$a = "Hello ";

$a .= "World!"; // now $a contains "Hello World!"

print $a;

?>

Using Conditional Statements if is

used to check

Yes/No True/False

Greater/Lessthan ..etc.

If(), else and elseif are called conditional Statements

if: if(condition) {

... ...

}

ex:

if ($a>$b){

print (" A is big");

}

if..else if(condition)

{ ... ...

}

else

{ .... ....

}

ex:

if($a>$b)

{

print ("A is big"); }

else

{ print ("B is big");

}

ex1:

<?php

$user= "admin";

$pwd = "admin";

if($user == "admin" and $pwd== "admin")

print "success";

else

print = "invalid";

?>

3. Nested if

also called as if else ladder. generally used to compare range of values using if..else if

if(condition) {

....

.... }

else if(condition) {…

...

}

else if(condition) {

...

... } else

{ ...

}

ex:

if($a>$b and $a>$c)

print "$a is big";

else if($b>$a and $b>$c) print

"$b is big";

else

print "$c is big";

Terinary if:

short form of If.

c$= $a>$b ? " A is big" : " B is big";

print $c;

Practice programes:

Programs on if condition a=5

b=8

find the biggest one

1)find a person is elegible for vote or not.

2) accept age and find which group he belongs such

as : young,middle,elder

3)find the gender[m/f] and print "Mr" or "Miss" before the name

4) take a totalamt based on amount provide discount if

totalamt between 800- 1000 discount is 15%

if totalamt between 500- 799 discount is 10%

if totalamt below 500 discount is 5%

5) find out employee net salary

if salary above 20,000 DA=10%,TA=6%,HRA=4%,IT=3%

if salary above 15,000 DA=8%,TA=5%,HRA=3%,IT=2%

calaculate GROSS,NET salary

Switch() Statement : is multi conditional braching statement syn:

switch(variable) { case option1:

.... break;

case option2: ....

break; case option3:

.... break;

case option4: .... break;

default:

print"Not in list"; }

Example:

$n=2; switch ($n) {

case 1: print "one"; break;

case 2: print "Two"; break;

case 3: print "Three"; break;

case 4: print "Four"; break;

default: print "no is not in the list";

}

Using the while() Loop

repeating set of statements until giving condition is True. when it becomes false it comes out of the loop.

Usage: ex: To read records from database.

while(condtion)

{

....

...

}

..........

...........

*first checks the condition and then enters into the loop.

$i = 1; //initial value

while ($i < 11) { // condition

print (" i = " . $i . "<BR>");

$i++; //incrementation

}

Usingdo..while()Loop do..while()

***first executes the block and then checks the condition.

it is executed atleast once before it checks the condition.

do{

Something;

}

while(condition);

ex:

$i=6;

do{

print $i; $i++;

}while($i<=5); for() Loop : Used To read array values

for(initialization;condition;incrementation)

{

.....

..... }

for($i=0; $i <= 10; $i++) {

print $i;

}

programs

using for loop

print odd,even no.s

print following start using nested for loop

* * * * * * * * *

print 1 2 3 1 2 3

1 2 3

print Factorial of given no. using while:

N=5 1*2*3*4*5=120 n=5;

f=1; n>=0 //----------->condition f=f*(n);

n--;

print f.

basic programs on variables and arithmatic expr.

1. write a program to find simple Interest

formula si=(PNR)/100;

P(principal amout) N(no of years) R(rate of interest)

2 write a program to find area of circle a =pi* r * 2

3 area of Triangle

base * height / 2

4 area of Rectangle length *

bredth

5 area of square side * side

6 convert

a) celcious to Fahrenheit f=c*(9/5)+32

b) Fahrenheit to celcious c=(f-32)*(5/9)

7 write a program to exchange the vaules of a,b a=5

b=10 print

a=10 b=5.

8 write a progrm to print cube of n value

n=5 cube=n*n*n

Practical:

1.start server

2.write code and save it in c:\xamPP\htdocs\Mysite\hello.php and click debug to check output.

3. running the above program in browser.

Links: http://download.cnet.com/XAMPP-Lite/3000-10248_4-75157363.html (liter version of XAMPP)

www.teamviewer.com (to screen share)