1 / 66

PHP (Hypertext Pre Processor)

PHP (Hypertext Pre Processor). What is PHP?. PHP is a server-side scripting language. PHP is used to create dynamic web pages. PHP was initially known as Personal Home Page and later renamed to Hypertext Pre Processor. Why PHP?. PHP is easy to learn and implement. PHP is free.

glain
Download Presentation

PHP (Hypertext Pre Processor)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP(Hypertext Pre Processor)

  2. What is PHP? • PHP is a server-side scripting language. • PHP is used to create dynamic web pages. • PHP was initially known as Personal Home Page and later renamed to Hypertext Pre Processor.

  3. Why PHP? • PHP is easy to learn and implement. • PHP is free. • Huge community support (popular).

  4. History • PHP was developed by RasmusLerdorf in the year 1995. Initial version was PHP/FI (Form Interpreter). • PHP version 2 was released on June 8, 1995 by Lerdorf. • PHP version 3 was released in the year 1997. • PHP version 4 was released on May 22, 2000. • PHP version 5 was released on July 13, 2004. Object Oriented features were introduced. • PHP version 6 is still under development.

  5. Creating a PHP Script <html> <head> <title>PHP Demo</title> </head> <body> <?php echo “Hello World!”; ?> </body> </html>

  6. Executing PHP Script • Install a web server like WAMP or XAMPP. • Run the server. • Open a web browser and type the following: http://localhost/filename.php or http://localhost:80/filename.php

  7. Points to remember while writing PHP scripts • All PHP code must be enclosed in <?php and ?> tags. • Every PHP statement should end with a semi-colon. • Blank lines within the PHP tags are ignored. • Single comments are written using // and multiline comments should be enclosed between /* and */

  8. Variables • Naming rules and conventions: • Every variable must be preceded by the $ sign. • A variable name must start with a letter or an underscore. • A variable name should not contain any special symbols like @, # etc… • A variable name should not contain any white spaces. • PHP has no limit on the length of variable names

  9. Declaring and Using Variables Syntax for declaring a variable: $variable_name; Syntax for using a variable: $variable_name = value;

  10. Declaring and Using Variables (cont…) Assigning value of another variable to a new variable: $var1 = 10; $var2 = $var1; or $var1 = 10; $var2 = &$var1; //Passing a reference

  11. Dynamically assigning variable name $var1 = ‘abc’; ${$var1} = ‘value’; //Variable name is abc.

  12. Destroying Variables The unset() function can be used to destroy variables or array elements in the memory. $var1 = 10; echo $var1; unset($var1); echo $var1; //error message will be displayed

  13. Constants • Constants are identifiers which are used to store values that cannot be changed during the execution of the script. • Constant names does not start with a $ sign. • Constant names are generally written in uppercase letters.

  14. Creating and Using Constants Syntax for creating constant: define(“CONSTANT_NAME”, Value); Using constants: <?php define(“PI”, 3.142); echo “PI”; echo “Radius of circle is: PI*10*10”; ?>

  15. Data Types • PHP provides eight data types. They are: • integer (0, 3, -9 etc…) • float, double (1.5, -4,23, 0.6 etc…) • string (“Hello”, ‘hi’, “name”) • boolean (true, false) • array • object (Collection of attributes and functions) • resource (Which stores references to functions and resources external to PHP like database tables) • NULL (Special data type whose only value is null)

  16. Determining Data Type • We can use gettype(variable_name) to get the data type of the given variable. • Functions to check the data type of variables: • is_bool() • is_numeric() • is_int() • is_float() • is_string() • is_null() • is_array() • is_object()

  17. Type Casting Syntax for casting the data type of a variable is: variable = (target_type)variable; Example: $x = (boolean)$x;

  18. Type Casting (cont…) Function settype() can also be used to type cast a value or a variable. Syntax: settype(variable_name, “target_type”) Example: settype($x, “bool”); settype($x, “array”);

  19. Operators in PHP

  20. Operator Precedence

  21. Control Statements • Types of control statements in PHP: • Conditional statements (if, if-else, switch) • Looping statements (while, do-while, for, foreach) • Jump statements (break, continue, exit)

  22. if statement Syntax: if(expression) { statements(s); }

  23. if-else statement Syntax: if(expression) { statement(s); } else { statement(s); }

  24. elseif ladder Syntax: if(condition1) { statement(s); } elseif(condition2) { statement(s); } … else { statement(s); }

  25. switch statement Syntax: switch(condition) { case label1: statement(s); break; case label2: statement(s); break; … default: statement(s); }

  26. while loop Syntax: while(condition) { statement(s); }

  27. do...while loop Syntax: do { statement(s); }while(condition);

  28. for loop Syntax: for(initialization; condition; increment/decrement) { statement(s); }

  29. foreach loop Syntax: foreach(array as [key=>]value) { statement(s); }

  30. break statement Syntax: loop { … [condition] break; … }

  31. continue statement Syntax: loop { … [condition] continue; … }

  32. exit statement Syntax: loop { … [condition] exit; … }

  33. Arrays Syntax for creating an array: $array_name = array(); or $array_name = array(ele1, ele2,…,eleN);

  34. Arrays (cont…) Syntax for storing value in an array: $array_name[index] = value; Syntax for accessing value in an array: $array_name[index]

  35. Arrays (cont…) For printing the array elements use print_r() function. print_r($array_name); For finding whether a variable is an array or not use is_array() function. is_array($array_name);

  36. Arrays (cont…) • Types of arrays: • Numeric array • Associative array • Multidimensional array

  37. Associative Array • In associative arrays each index is treated as a value. Each element is accessed with the key (index). Example: <html> <head> <title>Associative Array Demo</title> </head> <body> <?php $arr = array("name"=>"teja", "gender"=>"male", "email"=>"teja@gmail.com"); print_r($arr); ?> </body> </html>

  38. Multidimensional Array • A multidimensional array is created as an array of arrays. Example: <html> <head> <title>Multi Dimensional Array Demo</title> </head> <body> <?php $arr = array(array(1,2,3), array(4,5,6)); print_r($arr[0][2]); ?> </body> </html>

  39. Using foreach with Numeric Arrays //PHP script to print all the elements in the array using foreach loop <html> <head> <title>foreach loop with numeric arrays</title> </head> <body> <?php $arr = array(1,2,3,4,5,6); foreach($arr as $x) echo "$x “; ?> </body> </html>

  40. Using foreach with Associative Arrays //PHP script to print all the elements in an associative array using foreach loop <html> <head> <title>foreach loop aith associative arrays</title> </head> <body> <?php $arr = array("name"=>"teja", "gender"=>"male", "email"=>"teja@gmail.com"); foreach($arr as $attr => $val) echo "$attr is: $val<br />"; ?> </body> </html>

  41. Array Functions

  42. Array Functions (cont…)

  43. Array Functions (cont…)

  44. Functions Syntax for creating a function: function function_name( [parameters list] ) { … Statement(s); … [return]; }

  45. Functions (cont…) • Naming conventions for naming a function: • Function names are not case sensitive. • Function names can contain only letters, digits or underscore. • Function names should start with a letter or underscore. • Two functions cannot have the same function name. • Reserved keywords cannot be used as function names.

  46. Functions (cont…) Syntax for calling or invoking a function: function_name([parameters-list]);

  47. Functions (cont…) Example: <html> <head> <title>Online PHP Script Execution</title> </head> <body> <?php function sum($a, $b) //function definition { echo "Sum of $a and $b is: ".($a+$b); } sum(10, 20); //function call ?> </body> </html>

  48. Variable Scope • By default a variable is local with respect to the block in which it is declared. • To declare global variables, use global keyword. • Another way to mimic the functionality of a global variable is to pass the reference of a variable by using the & operator.

  49. String Manipulation Functions

  50. String Manipulation Functions (cont…)

More Related