Basics of Php

August 26, 2017 | Author: Rajesh Bangalore | Category: Php, Control Flow, Web Application, Areas Of Computer Science, Software Development
Share Embed Donate


Short Description

Brief Introduction to PHP Scripting...

Description

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:

Example: print "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



Numbervariable

joining withdotoperator

// print uses dot(.) for contatinating string with variable



constant: • constant defind using define()-function. • Once a constant is defined, it can never be changed. • constants does not have '$'.



UnderstandingDataTypes • • • • • •

Booleans Integers Floating point numbers Strings Arrays Objects ex: Date



WritingStatementsandComments: 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 multlinecomment /* .... ....*/

Runningprograminaserver server runs php programs and sends result back to the html page. Testingserver test.php 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 EmbeddingHTML inPHP Ex: test.php

EmbeddingPHPinHTML Hello.php * if we use php code in HTML it must be stored as .php PHP Test

UsingOperators 1. Arithmetic Operators +,-,*,/,% Ex2: Operator Precedence order of evaulation of an expression 1) / * % 2) + 3) = ex: a*(b+c/d)*22 the correctwayis (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;

AssignmentOperators = += 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/DecrementingOperators ++ -i=10; i++;

i--->11 i value incremented by 1 i-- 10 i value decremented by 1 ComparisonOperators >

greater than

<

less than

=

greaterthan or equalto

!=

not equal to

==

equalto

LogicalOperators and---->and or ----> or ! ---->not StringOperators



// now $a contains "Hello World!"

UsingConditionalStatements 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:

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";

Terinaryif: short form of If.

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

Practice programes: Programs on ifcondition 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"; } Usingthewhile()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 . ""); $i++; } Usingdo..while()Loop do..while()

//incrementation

***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($icondition f=f*(n); n--; print f. basicprogramsonvariablesandarithmaticexpr. 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=(f32)*(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)

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF