Php

455 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
455
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Php

  1. 1. 1
  2. 2. Introduction to PHP• PHP Hypertext Preprocessor. – Other Names : Personal Home Page, Professional Home Page• Is a server side scripting language. – Capable of generating the HTML pages• HTML generates the web page with the static text and images.• However the need evolved for dynamic web based application, mostly involving database usage. 2
  3. 3. WEB SERVER Gets Page <HTML> <?php PHP code ?> HTTP Request </HTML> (url) Interprets the PHP code Server response <HTML> <B>Hello</B> </HTML>CLIENT Hello Browser creates the web page 3
  4. 4. Why PHP?• ..there are no. of server side scripting available like ASP, SSJS, JSP…..• PHP involves – simplicity in scripting (..generally using the database) – platform independence.• PHP is – primarily designed for web applications – well optimized for the response times needed for web applications• Is an open source. 4
  5. 5. PHP Language features• PHP language features such as control structures, operators, variable types, function declaration, class/object declaration are almost similar to any compiled or interpreted language such as C or C++. 5
  6. 6. PHP Data Type• Three basic data types – Integer – Double – String• More data types – Array – Object• PHP is an untyped language – variables type can change on the fly. 6
  7. 7. PHP Block• PHP code block is embedded within the <?php and ?> tags.• When the server encounters the PHP tags it switches from the HTML to PHP mode.• There are four different ways to embed the PHP code – <?php echo(“Some PHP code”); ?> – <? echo(“Some PHP code”); ?> – <SCRIPT Language=„php‟> echo(“Some PHP code”); </SCRIPT> – <% echo(“Some PHP code”); %> 7
  8. 8. PHP Constants• ..values that never changes• Constants are defined in PHP by using the define() function. – For e.g. define(“NCST”, “National Centre for Software Technology”)• defined() function says whether the constant exists or not. 8
  9. 9. PHP Variables• The variables in PHP are declared by appending the $ sign to the variable name. – For e.g $company = “NCST”; $sum = 10.0;• variable‟s data type is changed by the value that is assigned to the variable.• Type casting allows to change the data type explicitly. 9
  10. 10. PHP Variables (cont.)• Rich set of functions for working with variable. – For e.g • gettype, settype, isset, unset, is_int, intval etc etc 10
  11. 11. PHP Operators• All the operators such as arithmetic, assignment, Comparison, and logical operators are similar to the operators in C and C++.• In PHP the string concatenation operator is denoted by „.‟. – For e.g. • $name = “My name is”.$myname; 11
  12. 12. PHP Statements• IF statement if (<condition>) { //php code goes here } else { //php code goes here }• Alternative Syntax if(<condition>) : //html code goes here else : //html code goes here endif; 12
  13. 13. PHP Statements (cont.)• For loop for($i=0;$i < 10;$++i) { echo(“the value is :”. $i); } – Alternative Syntax for($i=0;$i < 10;$++i) : // html code goes here endfor;• While loop• Do-While loop 13
  14. 14. Functions• Function declaration in PHP function my_func(<parameters>) { //do something in the function } – for e.g. function sayHello() { echo(“<B>hello amrish<B><BR>”); } 14
  15. 15. Functions (cont.)• Assigning functions to the variables – for e.g • $hello = “my_func”; • to invoke the function my_func() through the variable $hello( );• When an argument is to be passed by reference, an ampersand (&) is placed before the parameter name – for e.g. my_func(&$my_refvar); 15
  16. 16. Arrays• ..contains value set• each element has a value, data stored in the element.• And has a key by which the element can be referred to. 16
  17. 17. Initializing Arrays• No of ways to initialize the array. – For e.g. • $ncststaff[] = “amrish”; $ncststaff[] = “murali”; $ncststaff[] = “narayan”; • $ncststaff[123] = “amrish”; $ncststaff[122] = “murali”; $ncststaff[121] = “narayan”; • $ncststaff = array (“amrish”, “murali”, “narayan”); – to change the indices of the array use => operator. 17
  18. 18. Accessing the Array Elements• The elements in the array can be accessed by using the list and each constructs – for e.g while(list($key,$value) = each(countries)) echo(“$value<BR>n”); – current(<arrayname>) gives the current value being accessed. key(<arrayname>) gives the index of the current element that is being accessed. – prev(<arrayname>) gives the previous element. – next(<arrayname>) gives the next element. 18
  19. 19. Accessing the Array Elements (cont.)– Array_walk(<arrayname>,<function_name>) • function_name is the function that is written for every member of an array. • For e.g $ncststaff = array (“amrish”, “murali”, “narayan”); array_walk ($ncststaff, printstaff); // function to print each element of the array function printstaff($names) { echo “<B>$names</B><BR>n”; } 19
  20. 20. Arrays (cont.)• $ncststaff = array (“dake” => array(“amrish”, “lakshana”, “venkat”), “spc” => array(“narayan”, “murali”,“prasad”)); – creates a two dimensional array.• Sorting Functions – sort() : sorts the elements in the numeric and alphabetical order. – rsort() : sorts the elements in the reverse order. – asort() : sorts the elements in the array without changing the indices. – ksort() : sorts the arrays by key. 20
  21. 21. Classes• Class is a template of an object and includes the properties and methods that describe an object and behavior.• Class in PHP is defined using class statement. 21
  22. 22. Classes (cont.)• For e.g<? class company { // define the properties var $companyname; // define the methods function company($cname) { $this->companyname = $cname; } function getnames($idcode) { //return the name of the employee for the required idcode } }?> 22
  23. 23. THANK YOU Ayush Chugh(11csu036) Ayushi Gupta(11csu037) 23

×