PHP Presentation

14,719
-1

Published on

This is Full Book in PPT of PHP

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
14,719
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
71
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

PHP Presentation

  1. 1. Faculty : Diploma EngineeringBranch : Computer EngineeringSubject Name: PHP & MySQLSubject Code : 360701 Author : Amit K. Lakhani Lecturer in Computer Engineering Department Tolani Foundation Gandhidham Polytechnic Adipur
  2. 2. Chapter - 1 Introduction to PHP
  3. 3. 1.Introduction to PHPThis unit covers… 1.1 History of PHP, Apache web server, MySQL & Open source 1.2 Relationship between Apache, MySQL and PHP (AMP module) 1.3 PHP configurations in PHP 1.4 Apache web server Developed By: Amit Lakhani, TFGP Adipur
  4. 4. 1.1History of PHP, Apacheweb server, MySQL & Open source Developed By: Amit Lakhani, TFGP Adipur
  5. 5. History of PHP• PHP is a general-purpose server-side scripting language originally designed for Web development to produce dynamic Web pages.• It is one of the first developed server-side scripting languages to be embedded into an HTML source document, rather than calling an external file to process data.• Ultimately, the code is interpreted by a Web server with a PHP processor module which generates the resulting Web page. It also has evolved to include a command-line interface capability and can be used in standalone graphical applications. Developed By: Amit Lakhani, TFGP Adipur
  6. 6. History of PHP conti..• PHP can be deployed on most Web servers and also as a standalone shell on almost every operating system and platform free of charge.• PHP was originally created by Rasmus Lerdorf in 1995. The main implementation of PHP is now produced by The PHP Group and serves as the formal reference to the PHP language.• PHP is free software released under the PHP License, which is incompatible with the GNU General Public License (GPL) due to restrictions on the usage of the term PHP.• While PHP originally stood for "Personal Home Page", it is now said to stand for "PHP: Hypertext Preprocessor", a recursive acronym. Developed By: Amit Lakhani, TFGP Adipur
  7. 7. History of MySQL• MySQL is the worlds most used relational database management system (RDBMS) that runs as a server providing multi-user access to a number of databases.• It is named after developer Michael WideniusThe SQL phrase stands for Structured Query Language.• The open source database product that puts the "M" in LAMP, was created by MySQL AB, a company founded in 1995 in Sweden. In 2008, MySQL AB announced that it had agreed to be acquired by Sun Microsystems for approximately $1 billion. Developed By: Amit Lakhani, TFGP Adipur
  8. 8. History of Apache web server• The Apache Web Server Foundation was created in late February, 1994 by a team of eight core contributors, and three additional contributors. The first release was made in April, 1995.• The Apache Web (HTTP) Server is designed to provide static and dynamic web pages over the Internet. It is used by large enterprises, to students to home users for everything from mission critical applications to simple one page websites. Apache is the web server component of the popular LAMP (Linux, Apache, MySQL,Perl/Python / PHP) application stack. Developed By: Amit Lakhani, TFGP Adipur
  9. 9. History of Apache web server conti..• The web server is software that allows a computer to host web pages. The most popular Apache is a web server, an open source which runs on Windows, Linux and other operating systems.• The web server lets you write and test programs running from your local computer exactly the same way they will be seen on the internet.• Apache doesn’t serve up just HTML files it handles a wide range of files, from images and flash files to MP3 audio files, RSS feeds and so on. Developed By: Amit Lakhani, TFGP Adipur
  10. 10. Open source• Refers to a program or software in which the source code (the form of the program when a programmer writes a program in a particular programming language) is available to the general public for use and/or modification from its original design free of charge.• Open source code is typically created as a collaborative effort in which programmers improve upon the code and share the changes within the community. Developed By: Amit Lakhani, TFGP Adipur
  11. 11. Open source conti..• The basics behind the Open Source Initiative is that when programmers can read, redistribute and modify the source code for a piece of software, the software evolves. Open source sprouted in the technological community as a response to proprietary software owned by corporations. Developed By: Amit Lakhani, TFGP Adipur
  12. 12. 1.2Relationship between Apache, MySQL and PHP Developed By: Amit Lakhani, TFGP Adipur
  13. 13. Relationship Apache, MySQL and PHP• AMP stands for Apache web server, MySQL database and PHP or Perl or Python language.• PHP is used to create web pages, MySQL is used to create database for the storage and Apache is used to run PHP pages.• The combination of these three allows us to create dynamic and interactive websites.• It can be used on cross platforms means we can install it on WINDOWS (WAMP) or on LINUX (LAMP). Developed By: Amit Lakhani, TFGP Adipur
  14. 14. 1.3PHP configurations in IIS Developed By: Amit Lakhani, TFGP Adipur
  15. 15. PHP configuration in IISConfiguration of PHP on IIS includes following steps. Thisguide provides configuration about IIS 5.1 shipped withWindow XP sp2.1- Download latest version of PHP fromhttp://www.php.net/downloads.php. Note you shoulddownload installer version not the zip version. Installerversion automatically performs all necessaryconfigurations. If we use zip version, Environmentalvariables must be modified manually2- Run the setup and choose installation directory forexample C:PHP3- On next screen choose your version of web server, herechoose IIS ISAPI and click next. Developed By: Amit Lakhani, TFGP Adipur
  16. 16. PHP configuration in IIS conti..4- On next screen in the extension portion chooseextensions to be installed on local hard drive and clickInstall. You may add and remove extension later. Developed By: Amit Lakhani, TFGP Adipur
  17. 17. Installing WAMP on WINDOWSRun the installer file. Developed By: Amit Lakhani, TFGP Adipur
  18. 18. Installing WAMP on WINDOWS conti..In the license agreement click [I accept the agreement],then click next. Developed By: Amit Lakhani, TFGP Adipur
  19. 19. Installing WAMP on WINDOWS conti..Select the directory where you would like to installWAMP. Developed By: Amit Lakhani, TFGP Adipur
  20. 20. Installing WAMP on WINDOWS conti..Select the start menu folder where you would like toplace WAMP’s shortcuts. Developed By: Amit Lakhani, TFGP Adipur
  21. 21. Installing WAMP on WINDOWS conti..In the additional tasks you may select if WAMP would beloaded on startup or no. Developed By: Amit Lakhani, TFGP Adipur
  22. 22. Installing WAMP on WINDOWS conti..This is the confirmation screen of the options you haveselected. Click back to modify your install settings or clicknext to start the installation. Developed By: Amit Lakhani, TFGP Adipur
  23. 23. Installing WAMP on WINDOWS conti..Some time midway into the installation a window willpop-up asking for the directory where to save thedocument root folder. It is recommended that you leave itas it is and click ok to continue the installation. Developed By: Amit Lakhani, TFGP Adipur
  24. 24. Installing WAMP on WINDOWS conti..After the said pop-up another window will appear askingfor you default browser. If you have no preference forbrowsers just hit Open. If not search for the .exe file ofyour preferred browser then click Open to continue theinstallation. Developed By: Amit Lakhani, TFGP Adipur
  25. 25. Installing WAMP on WINDOWS conti..Once the installation is finished, click finish. Developed By: Amit Lakhani, TFGP Adipur
  26. 26. Installing WAMP on WINDOWS conti..In your System Tray you should now see a “WAMP” icon.Click it, and select Put Online (Note: if it says Put Offlineignore this step and skip to the next one). Developed By: Amit Lakhani, TFGP Adipur
  27. 27. 1.4Apache Web server Developed By: Amit Lakhani, TFGP Adipur
  28. 28. Apache webserver• Apache is generally recognized as the worlds most popular Web server (HTTP server). Originally designed for Unix environments, the Apache Web server has been ported to Windows and other network operating systems.• The name "Apache" derives from the word "patchy" that the Apache developers used to describe early versions of their software.•The Apache Web server provides a full range of Web server features, including CGI, SSL, and virtual domains. Developed By: Amit Lakhani, TFGP Adipur
  29. 29. Apache webserver conti..• Apache also supports plug-in modules for extensibility. Apache is free software, distributed by the Apache Software Foundation that promotes various free and open source advanced Web technologies.Configuration Files• Apache uses a system of three text files for managing its configuration data. All three of these files (almost always) appear in Apaches ./conf directory and are designed to be edited by system administrators: httpd.conf for general settings srm.conf for resource settings access.conf for security settings Developed By: Amit Lakhani, TFGP Adipur
  30. 30. Apache webserver conti..• When Apache first starts, these files are processed in the order shown above. Originally, the initial installation of Apache included default entries within each of the three files.• In the most recent versions of Apache, however, the default installation has changed. Now httpd.conf is treated as the "master" configuration file and it contains all of the settings. Both srm.conf and access.conf still exist in the installation, but they contain no settings and are empty except for some comments. Developed By: Amit Lakhani, TFGP Adipur
  31. 31. Apache webserver conti..• Like many UNIX-style programs, Apache will not re-read the .conf files after the Web server starts running. After making changes, an administrator must restart Apache for the new settings to take effect.Inside Httpd.conf• Traditionaly httpd.conf contained general settings such as the ServerName and Port number. These entries appear as follows in the file: ServerName localhost Port 80 Developed By: Amit Lakhani, TFGP Adipur
  32. 32. Apache webserver conti..• The term "httpd" stands for HTTP Daemon. Recall that in a UNIX environment, the term daemon refers to a type of process designed to launch at system boot and continue running for very long periods of time. The name doesnt make much sense on a Windows platform, but the concept of a UNIX daemon can be compared to the concept of Windows services.• The Windows port of Apache allows for installation as a Windows service but does not require it. This file contains a number of other entries (technically called directives), but for most of these, modifications are optional. Developed By: Amit Lakhani, TFGP Adipur
  33. 33. Apache webserver conti..• Probably the most useful of these entries is ServerAdmin – it allows the administrator to set the email address that will be displayed on error pages (such a "404 pages") in the clients browser, giving users a way to contact someone if a site problem occurs: ServerAdmin compnetworking.guide@about.comAccess and Security Settings• It is recommended practice now for Apache administrators to manage their resource and security settings from httpd.conf. Administrators of older versions of Apache can simply cut their entries from srm.conf and access.conf and paste them into the master file. Developed By: Amit Lakhani, TFGP Adipur
  34. 34. Apache webserver conti..• If an administrator wants to go one step further and delete the two empty files, they should also place the following entries in httpd.conf to prevent Apache from attempting to access them: AccessConfig /dev/null ResourceConfig /dev/null Conclusion• The family of .conf files supports most but not all of Apaches configuration options. Other important mechanisms for managing an Apache Web server include the .htaccess file, the mime.types file, and the family of log files. Look for discussion of these in a future article. Developed By: Amit Lakhani, TFGP Adipur
  35. 35. Summary • History of PHP, Apache web server, MySQL & Open source • Relationship between Apache, MySQL and PHP (AMP module) • PHP configurations in PHP • Apache web server Developed By: Amit Lakhani, TFGP Adipur
  36. 36. Chapter - 2 Basics of PHP
  37. 37. 2.Basics of PHPThis unit covers… 2.1 PHP Structure and syntax 2.2 Creating the PHP pages 2.3 Rules for PHP syntax 2.4 Integrating HTML with PHP 2.5 Constants, Variable: static and global variable 2.6 Conditional structure and looping 2.7 PHP operator 2.8 Arrays foreach construct 2.9 User defined function, argument function, variable function, return function, default argument, variable length argument Developed By: Amit Lakhani, TFGP Adipur
  38. 38. 2.1PHP structure and syntax Developed By: Amit Lakhani, TFGP Adipur
  39. 39. PHP structure and Syntax PHP is a scripting language commonly used on web servers. • Stands for “PHP: Hypertext Preprocessor” • Open source • Embedded code • Comparable with ASP • Multiple operating systems/web servers Developed By: Amit Lakhani, TFGP Adipur
  40. 40. What can it do? • Dynamic generation of web-page content • Database interaction • Processing of user supplied data • Email • File handling • Text processing • Network interaction • And more… Developed By: Amit Lakhani, TFGP Adipur
  41. 41. Fundamentals • PHP is embedded within xhtml pages within the tags: <?php … ?> • The short version of these tags can also be used: <? … ?> • Each line of PHP is terminated, like MySQL, with a semi- colon. Developed By: Amit Lakhani, TFGP Adipur
  42. 42. 2.2Creating the PHP pages Developed By: Amit Lakhani, TFGP Adipur
  43. 43. 2.2 Creating the PHP pages <html> <head> <title>PHP Test</title> PHP statement </head> <body> <?php echo ‘<p>Hello World!</p>’; ?> </body> </html> Developed By: Amit Lakhani, TFGP Adipur
  44. 44. 2.3Rules for PHP syntax Developed By: Amit Lakhani, TFGP Adipur
  45. 45. Rules for PHP syntax•Just as with other programming languages, in PHP, you can create variables and process them in different in different ways.• With PHP, variable names are preceded with a dollar sign, such as $amount. The variable name itself (after the $) can start with a letter or underscore, and then followed by any sequence of letters, digits, or underscores. (That’s like most languages – variables can have letters, digits, and underscores, but can’t start with a digit.) And technically, the dollar sign isn’t part of the variable name. Developed By: Amit Lakhani, TFGP Adipur
  46. 46. Rules for PHP syntax conti..• The semicolon signifies the end of a PHP statement and should never be forgotten. we would need to place a semicolon at the end of each statement.• As with HTML, whitespace is ignored between PHP s tatements. This means it is OK to have one line of PHP code, then 20 lines of blank space before the next line of PHP code. You can also press tab to indent your code and the PHP interpreter will ignore those spaces as well. Developed By: Amit Lakhani, TFGP Adipur
  47. 47. Literals.. • All strings must be enclosed in single of double quotes: ‘Hello’ or “Hello”. • Numbers are not in enclosed in quotes: 1 or 45 or 34.564 • Booleans (true/flase) can be written directly as true or false. Developed By: Amit Lakhani, TFGP Adipur
  48. 48. Comments // This is a comment # This is also a comment /* This is a comment that is spread over multiple lines */ Do not nest multi-line comments // recommended over # Developed By: Amit Lakhani, TFGP Adipur
  49. 49. Displaying Data• There are two language constructs available to display data: print() and echo().• They can be used with or without brackets.• Note that the data ‘displayed’ by PHP is actually parsed by your browser as HTML. View source to see actual output. <?php echo ‘Hello World!<br />’; echo(‘Hello World!<br />’); print ‘Hello World!<br />’; print(‘Hello World!<br />’); ?> Developed By: Amit Lakhani, TFGP Adipur
  50. 50. Escaping Characters• Some characters are considered ‘special’• Escape these with a backslash • Special characters will be flagged when they arise, for example a double or single quote belong in this group… <?php echo ‘Claire O’Reilly ’; echo “said ”Hello”.”; ?> Developed By: Amit Lakhani, TFGP Adipur
  51. 51. Variables: What are they?When we work in PHP, we often need a labelled place tostore a value (be it a string, number, whatever) so we canuse it in multiple places in our script.These labelled ‘places’ are called VARIABLESVariable naming Rules:• $ followed by variable name• Case sensitive • $variable differs from $Variable • Stick to lower-case to be sure!• Name must started with a letter or an underscore • Followed by any number of letters, numbers and underscores Developed By: Amit Lakhani, TFGP Adipur
  52. 52. Variables: example <?php $name = ‘Phil’; $age = 23; echo $name; echo ’ is ‘; echo $age; // Phil is 23 ?> Developed By: Amit Lakhani, TFGP Adipur
  53. 53. 2.4Integrating HTML with PHP Developed By: Amit Lakhani, TFGP Adipur
  54. 54. integrating HTML with PHP <html> <head> <title>PHP Test</title> PHP statement </head> <body> <?php echo ‘<p>Hello World!</p>’; ?> </body> </html> Developed By: Amit Lakhani, TFGP Adipur
  55. 55. 2.5 Constant variable:constant and global variable Developed By: Amit Lakhani, TFGP Adipur
  56. 56. Constant variable • Constants (unchangeable variables) can also be defined. • Each constant is given a name (note no preceding dollar is applied here). • By convention, constant names are usually in UPPERCASE. <?php define(‘NAME’,‘Phil’); define(‘AGE’,23); echo NAME; echo ’ is ‘; echo AGE; // Phil is 23 ?> Developed By: Amit Lakhani, TFGP Adipur
  57. 57. 2.6 ConditionalStructure and Looping Developed By: Amit Lakhani, TFGP Adipur
  58. 58. Control Structures • if, elseif, else • while, do … while • for, foreach • switch • break, continue, return • require, include, require_once, include_once Developed By: Amit Lakhani, TFGP Adipur
  59. 59. If statement • To do something depending on a comparison, use an if statement. if (comparison) { expressions; // do if TRUE } • NB: Notice the curly brackets – these are important! Developed By: Amit Lakhani, TFGP Adipur
  60. 60. If example <?php $a = 10; $b = 13; if ($a<$b) { echo ‘a is smaller than b’; } ?> Developed By: Amit Lakhani, TFGP Adipur
  61. 61. Extending IF statements• It is possible to add extra optional clauses to if statements.. if (comparison) { expressions; // do if TRUE } else { expressions; // do otherwise } Developed By: Amit Lakhani, TFGP Adipur
  62. 62. Extending If statements example if (comparison1) { expressions; } elseif (comparison2) { expressions; } else { expressions; } Developed By: Amit Lakhani, TFGP Adipur
  63. 63. An example.. $a = 10; $b = 13; if ($a<$b) { echo ‘a is smaller than b’; } elseif ($a==$b) { echo ‘a is equal to b’; } else { echo ‘a is bigger than b’; } Developed By: Amit Lakhani, TFGP Adipur
  64. 64. While loops • Might want to do something repeatedly while a comparison is true.. while (comparison) { expressions; } Developed By: Amit Lakhani, TFGP Adipur
  65. 65. While loop example • Lets count to 10! Displays 1,2,3,4,5,..,10: $i = 1; while ($i <= 10) { echo $i++; } Developed By: Amit Lakhani, TFGP Adipur
  66. 66. Do .. While An alternative... $i = 1; do { echo $i++; } while ($i <= 10); Developed By: Amit Lakhani, TFGP Adipur
  67. 67. For loop Sometimes we want to loop around the same bit of code a number of times.. Use a for loop. for (expr1; expr2; expr3) { statements; } • expr1 evaluated/executed initially • expr2 evaluated at beginning of each iteration (Continues if TRUE) • expr3 evaluated/executed at end of each iteration Developed By: Amit Lakhani, TFGP Adipur
  68. 68. For loop example To count from 1 to 10: Continue if true initialise Execute at end of loop for ($i=1; $i<=10; $i++) { echo $i; } Developed By: Amit Lakhani, TFGP Adipur
  69. 69. Foreach loop A foreach loop is designed for arrays. Often you want to loop through each item in an array in turn.. $letters = array(‘a’,’b’,’c’); foreach ($letters as $value) { echo $value; } // outputs a,b,c in turn Developed By: Amit Lakhani, TFGP Adipur
  70. 70. Foreach.. With keys Sometimes we want to use the array ‘key’ value too: $letters = array(‘a’,’b’,’c’); foreach ($letters as $key => $value) { echo “array $key to $value”; } Developed By: Amit Lakhani, TFGP Adipur
  71. 71. Switch statement • expr is evaluated switch (expr) – Case corresponding { to result is executed case (result1): – Otherwise default statements; case is executed break; • break case (result2): – Ensures next case statements; isn’t executed break; default: statements; } Developed By: Amit Lakhani, TFGP Adipur
  72. 72. Switch Example switch ($name) { case ‘Rob’: echo ‘Your name is Rob’; break; case ‘Fred’: echo ‘You are called Fred’; break; default: echo ‘Not sure what your name is’; } Developed By: Amit Lakhani, TFGP Adipur
  73. 73. break, continue, return • break • Ends execution of current for, foreach, do … while, while or switch structure • Option: Number of nested structures to break out of • continue • Skip rest of current loop • Option: Number of nested loops to skip • return • Ends execution of current function/ statement / script Developed By: Amit Lakhani, TFGP Adipur
  74. 74. 2.7PHP operators Developed By: Amit Lakhani, TFGP Adipur
  75. 75. Expressions • Using variables within expressions to do something is what PHP is all about. <?php $name = ‘Rob’; echo $name; ?> Developed By: Amit Lakhani, TFGP Adipur
  76. 76. Operators in PHP • Arithmetic • Assignment • Bitwise • Comparison • Ternary • Incrementing /decrementing • Logical • String Developed By: Amit Lakhani, TFGP Adipur
  77. 77. String Operators Use a dot to concatenate two strings: e.g. $firstname = ‘Rob’; $surname = ‘Tuley’; // displays ‘Rob Tuley’ echo $firstname.’ ‘.$surname; Developed By: Amit Lakhani, TFGP Adipur
  78. 78. Arithmatic Operators Example Name Result $a + $b Addition Sum of $a and $b. $a - $b Subtraction Difference of $a and $b. $a * $b Multiplication Product of $a and $b. $a / $b Division Quotient of $a and $b. $a % $b Modulus Remainder of $a divided by $b. Developed By: Amit Lakhani, TFGP Adipur
  79. 79. Assignment Operators Example Result $a = $b Sets $b to the same value as $a. $a += $b Equivalent to $a = $a + $b. $a .= $b Equivalent to $a = $a.$b. Developed By: Amit Lakhani, TFGP Adipur
  80. 80. Combining Operators Note that you can combine operators, for example use =, + and / in one expression: $a = 4; $b = 2; $c = $a + $b + ($a/$b); // $c has value 4+2+(4/2) = 8 Brackets help group operators. Developed By: Amit Lakhani, TFGP Adipur
  81. 81. Comparison Operators Example Name Result $a == $b Equal TRUE if $a is equal to $b. $a != $b Not equal TRUE if $a is not equal to $b. $a <> $b Not equal TRUE if $a is not equal to $b. $a < $b Less than TRUE if $a is strictly less than $b. $a > $b Greater than TRUE if $a is strictly greater than $b. $a <= $b Less than or equal TRUE if $a is less than or equal to to $b. $a >= $b Gtr than or equal TRUE if $a is greater than or to equal to $b. Developed By: Amit Lakhani, TFGP Adipur
  82. 82. Comparisons operator example • Comparison expressions return a value of TRUE (or ‘1’) or FALSE (or ‘0’). e.g. $a = 10; $b = 13; // result is true (‘1’) echo $a < $b; Developed By: Amit Lakhani, TFGP Adipur
  83. 83. Incrementing/Decrementing Example Name Effect ++$a Pre- Increments $a by one, then increment returns $a. $a++ Post- Returns $a, then increments $a increment by one. --$a Pre- Decrements $a by one, then decrement returns $a. $a-- Post- Returns $a, then decrements decrement $a by one. Developed By: Amit Lakhani, TFGP Adipur
  84. 84. Logical Operators Example Name Result $a and $b And TRUE if both $a and $b are TRUE. $a or $b Or TRUE if either $a or $b is TRUE. $a xor $b Xor TRUE if either $a or $b is TRUE, but not both. !$a Not TRUE if $a is not TRUE. $a && $b And TRUE if both $a and $b are TRUE. $a || $b Or TRUE if either $a or $b is TRUE. Developed By: Amit Lakhani, TFGP Adipur
  85. 85. Ternary Operators A single ? is the ternary operator. (expr) ? if_expr_true : if_expr_false; A test expression evaluates to TRUE or FALSE. TRUE gives first result (before colon) FALSE gives second result (after colon) Developed By: Amit Lakhani, TFGP Adipur
  86. 86. Ternary Operators example <?php $a = 10; $b = 13; echo $a<$b ? ‘a smaller’:‘b smaller’; // string ‘a smaller’ is echoed // to the browser.. ?> Developed By: Amit Lakhani, TFGP Adipur
  87. 87. 2.8Arrays Developed By: Amit Lakhani, TFGP Adipur
  88. 88. Array • So far, we have stored ONE piece of data in each variable. • It is also possible to store multiple pieces of data in ONE variable by using an array. • Each piece of data in an array has a key.. Developed By: Amit Lakhani, TFGP Adipur
  89. 89. Working with Array • Note that trying to echo an entire array will not display the data. To print an entire array to screen (for debug, for example) use the function print_r instead. print_r($letters); Developed By: Amit Lakhani, TFGP Adipur
  90. 90. Array example Normal Variable, no key: $name = ‘Rob’; Array Variable, multiple pieces with ‘keys’: $name[0] = ‘Rob’; $name[1] = ‘Si’; $name[2] = ‘Sarah’; … The ‘key’ Developed By: Amit Lakhani, TFGP Adipur
  91. 91. Array key • Array keys can be strings as well as numbers.. Array key $surname[‘rob’] = ‘Tuley’; $surname[‘si’] = ‘Lewis’; • Notice the way that the key is specified, in square brackets following the variable name. Developed By: Amit Lakhani, TFGP Adipur
  92. 92. Types of Array • Indexed / Numeric array • Indexed array have keys automatically assigned, starting from 0. • Values are stored and accessed in linear fashion • Associative array • Use keys that are manually specified and they use string as index. • A key is some string value • Stores element values in association with key values rather than in linear indexed order. Developed By: Amit Lakhani, TFGP Adipur
  93. 93. Indexed / Numeric Array • This array can store numbers, sting and objects but their index represented by numbers and starts from 0. //first method to create array by array() function <?php $varArray= array(1,2,3,4); ?> //second method to create array <?php $varArray[0]= 1 $varArray[1]= 2 $varArray[2]= 3 ?> Developed By: Amit Lakhani, TFGP Adipur
  94. 94. Example of Indexed / Numeric Array<?php//first method to create array Output:$varArray= array(1,2,3,4,5);foreach($varArray as $val){ print_r($val);}//second method to create array$varArray[0]= 1$varArray[1]= 2$varArray[2]= 3foreach($varArray as $val) print_r($val);?> Developed By: Amit Lakhani, TFGP Adipur
  95. 95. Associative Array • associative will have their index as string so that you can establish strong association between key and value. Array key Array value <?php $dept[comp] = "32"; $dept[it] = "30"; $dept[ec] = "34"; print_r($dept[it]); ?> Developed By: Amit Lakhani, TFGP Adipur
  96. 96. Example of Associative Array// first method to create array<?php Output:$dept[comp] = "32";$dept[it] = "30";$dept[ec] = "34";print_r($dept[it]);?>// second method to create array<?php$dept=array(“comp” => 32, “it” => 30, “ec” => 34,print_r($dept[it]);?> Developed By: Amit Lakhani, TFGP Adipur
  97. 97. Adding an element to Arrays Developed By: Amit Lakhani, TFGP Adipur
  98. 98. Adding an element to an Array• Adding an element to indexed array using [ ] operator and array_push() function• Adding an element to an associative array by specifying index• Adding an element to to the beginning of an array using array_unshift() function. Developed By: Amit Lakhani, TFGP Adipur
  99. 99. Adding an element to an Array example• You can add automatically indexed element using [ ] operator <?php $varArray=array(); Add an element to array $varArray[]=1; $varArray[]=2; for($i=0;$i<=1;$i++) Output: { echo $varArray[$i]; } $varArray=array(); array_push($varArray, 10,20); for($i=0;$i<=1;$i++) { echo $varArray[$i]; }?> Developed By: Amit Lakhani, TFGP Adipur
  100. 100. Adding an element to an Associative array • You can specify the index when adding an element. <?php Add an element to array $varArray=array(); $varArray[one]=1; $varArray[two]=2; echo $varArray[one]; ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  101. 101. Adding an element using array_unshift() • You can add an element to the beginning of an array using array_unshift() function. <?php Output: varArray=array(three); array_unshift($varArray, one); array_unshift($varArray, two); array_unshift($varArray, three); print_r("$varArray[0]"); print_r("$varArray[1]"); print_r("$varArray[2]"); Add an element to array ?> Developed By: Amit Lakhani, TFGP Adipur
  102. 102. Removing anelement from Arrays Developed By: Amit Lakhani, TFGP Adipur
  103. 103. Removing an element from array • You can remove an element using unset() function • You can remove the last element of an array using array_pop() function • You can remove the first element of an array using array_shift() function Developed By: Amit Lakhani, TFGP Adipur
  104. 104. Removing an element from array conti..• You can remove an element using unset() function <?php $varArray=array(); Output: $varArray[]=1; $varArray[]=2; for($i=0;$i<=1;$i++) { echo $varArray[$i]; } unset($varArray[0]); for($i=0;$i<=1;$i++) { Unset the first element echo $varArray[$i]; Of an array }?> Developed By: Amit Lakhani, TFGP Adipur
  105. 105. Removing an element from array conti..• You can remove last element using array_pop() function <?php $varArray=array(); Output: $varArray[]=1; $varArray[]=2; for($i=0;$i<=1;$i++) { echo $varArray[$i]; } $last=array_pop($varArray); for($i=0;$i<=1;$i++) Remove last element { Of an array echo $varArray[$i]; }?> Developed By: Amit Lakhani, TFGP Adipur
  106. 106. Removing an element from array conti..• You can remove first element using array_shift() function <?php $varArray=array(); Output: $varArray[]=1; $varArray[]=2; for($i=0;$i<=1;$i++) { echo $varArray[$i]; } $first=array_shift($varArray); for($i=0;$i<=1;$i++) Remove first element { Of an array echo $varArray[$i]; }?> Developed By: Amit Lakhani, TFGP Adipur
  107. 107. Counting andcombining elements of Arrays Developed By: Amit Lakhani, TFGP Adipur
  108. 108. Counting element of array• You can determine the sizeof an array using count() function <?php $varArray=array(1,2,3,4,5); $varArray_1=array(); echo "The number of elements in first the no of Counts array=".count($varArray); elements in array echo "The number of elements in second array=".count($varArray_1); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  109. 109. Combining multiple array• You can combine multiple array using array_merge() function <?php $varArray=array(1,2,3,4,5); $varArray_1=array(6,7,8,9); $arr_merge = array_merge($varArray, $varArray_1); print_r($arr_merge); Combines ?> Elements of array Output: Developed By: Amit Lakhani, TFGP Adipur
  110. 110. Searching Array Developed By: Amit Lakhani, TFGP Adipur
  111. 111. Searching arrays • You can see an element with a certain key exists using array_key_exists() function • You can see an element with a certain key exists using isset() function • You can check if a specific element exists in an array using in_array() function. Developed By: Amit Lakhani, TFGP Adipur
  112. 112. Searching arrays conti..• You can see an element with a certain key exists using array_key_exists() function <?php key Array name $varArray[0]=1; $varArray[1]=2; if(array_key_exists(1,$varArray)) { print_r($varArray[1]); } ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  113. 113. Searching arrays conti..• You can see an element with a certain key exists using isset() function <?php Array name $varArray[one]=1; $varArray[two]=2; if(isset($varArray[one])) { print_r($varArray[one]); } ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  114. 114. Searching arrays conti..• You can check if a specific element exists in an arrayusing in_array() function. <?php key Array name echo"<font size=50>"; $varArray[0]=1; $varArray[1]=2; Output: if(in_array(1,$varArray)) { print_r($varArray[1]); } ?> Developed By: Amit Lakhani, TFGP Adipur
  115. 115. Sorting Array Developed By: Amit Lakhani, TFGP Adipur
  116. 116. Sorting arrays • PHP has a rich collection of sorting functions that allow you to sort by values and keys. • Variants on most of these functions facilitate sorting in forward or reverse order and provide you with the option of preserving or resetting the associations between array keys and values. • All of PHP’s array sorting functions work on arrays in place and return TRUE to indicates success or FALSE in the event of failure. Developed By: Amit Lakhani, TFGP Adipur
  117. 117. Sorting arrays conti.. • Sorting an Array by Its Values using sort() • Sorting an Array by Its Values using asort() • Sorting an Array by Its Keys using ksort() • Reversing an Array Using arsort() • Reversing an Array Using krsort() • Reversing an Array Using array_reverse() Developed By: Amit Lakhani, TFGP Adipur
  118. 118. Sorting arrays conti..• Sorting an Array by Its Values using sort(): To order an array’s elements using their values, usethe sort() function. This function takes as its argumentsthe array to be sorted and an optional sort flag, and, likePHP’s other sorting functions, this one sorts the array inplace. <?php $varArray=array(100,25,1,45,15); print_r($varArray); sort($varArray); print_r($varArray); Sort an array ?> Developed By: Amit Lakhani, TFGP Adipur
  119. 119. Sorting arrays conti..• Sorting an Array by Its Values using asort(): Because sort() resets all the indexes, you have lostthe original keys. To get around this issue, you can use theasort() function instead. This works in the same way assort() except in one respect: it preserves the array’soriginal key/value associations. <?php $varArray[one]= "second"; $varArray[two]= "first"; $varArray[three]= "third"; asort($varArray); Sort an array print_r($varArray); ?> Developed By: Amit Lakhani, TFGP Adipur
  120. 120. Sorting arrays conti..• Sorting an Array by Its Keys using ksort(): Particularly with regard to associative arrays, it isjust as important to be able to sort arrays by their keys asit is by their values. The ksort() function accomplishes thiswhile maintaining the relationship between keys andvalues. <?php $varArray[one]= "second"; $varArray[two]= "first"; $varArray[three]= "third"; ksort($varArray); Sort an array By its keys print_r($varArray); ?> Developed By: Amit Lakhani, TFGP Adipur
  121. 121. Sorting arrays conti..• Reversing an Array Using arsort(): To sort an associative array by value in reverseorder, use arsort(). Like asort(), this function preserves thearray’s keys. <?php $varArray_1[one]= "second"; $varArray_1[two]= "first"; $varArray_1[three]= "third"; arsort($varArray_1); Sort an array In reverse order print_r($varArray_1); ?> Developed By: Amit Lakhani, TFGP Adipur
  122. 122. Sorting arrays conti..• Reversing an Array Using krsort():The krsort() function sorts an array by key in reverse order. <?php $varArray_1[one]= "second"; $varArray_1[two]= "first"; $varArray_1[three]= "third"; krsort($varArray_1); Sort an array In reverse order print_r($varArray_1); ?> Developed By: Amit Lakhani, TFGP Adipur
  123. 123. Sorting arrays conti..• Reversing an Array Using array_reverse() The array_reverse() function does just what youwould expect; it reverses the order in which the elementsof an array are listed. <?php $varArray_1[one]= "second"; $varArray_1[two]= "first"; $varArray_1[three]= "third"; array_reverse($varArray_1); print_r($varArray_1); ?> Sort an array In reverse order Developed By: Amit Lakhani, TFGP Adipur
  124. 124. Sorting arrays example Developed By: Amit Lakhani, TFGP Adipur
  125. 125. Looping over arrays• The easiest way to loop over is foreach()• Each element in foreach() is a copy. i.e modifying the elements has no effect on the original array. <?php $varArray= array(1,2,3,4,5); print_r($varArray); foreach($varArray as $val) { print_r("$val"); echo"<br>"; } ?> Developed By: Amit Lakhani, TFGP Adipur
  126. 126. Array operators Example Name Result $a + $b Union Union of $a and $b. $a==$b Equality TRUE if $a and $b have the same key/value pairs $a===$b Identity TRUE if $a and $b have the same key/value pairs in the same order of the same type. $a!=$b Inequality TRUE if $a is not equal to $b $a<>$b Inequality TRUE if $a is not equal to $b $a!==$b Non-identity TRUE if $a is not identical to $b Developed By: Amit Lakhani, TFGP Adipur
  127. 127. 2.9User defined function, argument function, variable function, return function, default argument, variable length argument Developed By: Amit Lakhani, TFGP Adipur
  128. 128. Functions • User defined functions • Argument functions • Variable functions • Return functions • Default argument • Variable length argument Developed By: Amit Lakhani, TFGP Adipur
  129. 129. User defined functions • Functions are defined using the keyword function by the function name • Functions may have 0 or more arguments • Code body appears between opening and closing paranthesis. • Functions name begins with a letter or underscore, followed by any letter, numbers or underscores. • Function is called using the function name. • Function do not need to be declared • Recursion is supported Developed By: Amit Lakhani, TFGP Adipur
  130. 130. Example of User defined functions <?php function first() { echo "This is first function"; } function sec() { print "this is second function"; } first(); Output: sec(); ?> Developed By: Amit Lakhani, TFGP Adipur
  131. 131. Functions argument • variables can be passed either by value of by reference Function arguments <?php function addvalues ($value1 = 0, $value2 = 0, $value3 = 0) { $total = $value1 + $value2 + $value3; return $total; } Output: echo addvalues (1) . "<br />"; echo addvalues () . "<br />"; echo addvalues (1,2,3); ?> Developed By: Amit Lakhani, TFGP Adipur
  132. 132. Variable length argument • In addition to support default values for function argument PHP supports variable length argument with function such as func_get_args() • Regardless of what the function definition dictates you can retrieve every single value passed to function using func_get_args() • This functions returns an array where each element is corresponds to a single argument. • This is useful when you want function to accept any number of arguments Developed By: Amit Lakhani, TFGP Adipur
  133. 133. Example of Variable length argument <?php function display($name) { foreach(func_get_args() as $arg) { echo $arg."<br>"; } Output: } display("computer","IT","EC"); ?> Developed By: Amit Lakhani, TFGP Adipur
  134. 134. Returning values • You can return a value from an array using the return keyword. • If there is no return executed then NULL is the default value • You can return only a single value from a function by returning a more complex value such as an array or an object which encapsulate multiple values and using by reference arguments and update those value in your function. Developed By: Amit Lakhani, TFGP Adipur
  135. 135. Example of Returning values <?php function addandsubtract ($firstvalue, $secondvalue) { $firstreturnvalue = ($firstvalue + $secondvalue); $secondreturnvalue = ($firstvalue - $secondvalue); $myarray = array (); $myarray[0] = $firstreturnvalue; $myarray[1] = $secondreturnvalue; return $myarray; } $myarray = array (); $myarray = addandsubtract (10, 3); echo $myarray[0] . "<br />"; echo $myarray[1]; ?> Developed By: Amit Lakhani, TFGP Adipur
  136. 136. Object orientedprogramming in PHP Developed By: Amit Lakhani, TFGP Adipur
  137. 137. Object oriented concepts • Class: User defined data type which includes local function as well as local data. • Object: An individual instance of the data structure defined by a class. They are also known as instance. • Member variable: variables defined inside the class. This data will be invisible to the outside of the class and can be accessed by the member functions. • Member Function: functions defined in the class and are used to access object data. • Inheritance: When a class is defined by inheriting existing functions of a parent class then it is called inheritance. Developed By: Amit Lakhani, TFGP Adipur
  138. 138. Object oriented concepts conti… • Parent class: A class that is inherited by other class. It is also known as base class or super class. • Child class: A class that inherits the property of other class(parent). It is also known as subclass or derived class. • Polymorphism: same function can be used for different purposes. • Overloading: A type of polymorphism in which some or all of operators have different implementation depending on the type of arguments. • Data abstraction: Data details are hidden. Developed By: Amit Lakhani, TFGP Adipur
  139. 139. Object oriented concepts conti… • Encapsulation: where data and member functions can be encapsulated together to form an object. • Constructor: special type of function which will be called automatically whenever there is an object formation from a class. • Destructor: special type of function which will be called automatically whenever there is an object deleted or goes out of scope. Developed By: Amit Lakhani, TFGP Adipur
  140. 140. What is a class?Conceptually, a class represents an object, withassociated methods and variables<?phpclass sample $obj = new sample();{ $obj->getvalue(); var $val; $obj->showvalue(); function getvalue() ?> { $this->val=10; Output: } function showvalue() { echo "you have entered”.$this->val; }}; Developed By: Amit Lakhani, TFGP Adipur
  141. 141. Constructor function• Constructor functions are special types of function which are called automatically whenever an object is created.• PHP provides a special functions called __construct() to define a constructor, you can pass any number of arguments.• Remember.. if the constructor requires arguments, they must be passed when it is instantiated! Developed By: Amit Lakhani, TFGP Adipur
  142. 142. Constructor function example<?phpclass sample $obj=new sample(100, "computer");{ $obj->showvalue();var $ID; ?>var $name;function __construct($ID, $name){ Output:$this->ID= $ID;$this->name = $name;}function showvalue(){echo "ID:".$this->ID."<br>";echo "Name:".$this->name."<br>“;}}; TFGP Adipur Developed By: Amit Lakhani,
  143. 143. Destructor function• Like constructor function you can define a destructor function using __destruct().• You can release all the resources within destructor<?php $obj=new sample();class sample ?>{function __construct() Output:{echo "The constructor is called ";}function __destruct(){echo "The destructor is called ";}}; Developed By: Amit Lakhani, TFGP Adipur
  144. 144. Inheritance• The real power of using classes is the property of inheritance – creating a hierarchy of interlinked classes. Department parent children Computer Mechanical Developed By: Amit Lakhani, TFGP Adipur
  145. 145. Inheritance conti…..• The child classes ‘inherit’ all the methods and variables of the parent class, and can add extra ones of their own.• The effect of inheritance is that the child class has the following characteristics:• Have all the member variable declarations of the parent class.• Have all the same member functions as the parent, which will work the same way as those functions do in the parent. Developed By: Amit Lakhani, TFGP Adipur
  146. 146. Inheritance exampleclass pclass class cclass extends pclass{ Parent class { child class var $p_value; var $c_val; function p_getval() function c_getval() { { $this->p_value = "parent"; $this->c_val="child"; } } function p_showval() function c_showval() { { echo "This is $this->p_value class"; echo "This is $this->c_val class"; }} }} $cobj= new cclass(); $cobj->p_getval(); Object creation $cobj->p_showval(); $cobj->c_getval(); $cobj->c_showval();?> Developed By: Amit Lakhani, TFGP Adipur
  147. 147. Access specifier• Public member: • By default properties and methods are public. • They may be accessed from outside of the class. • They may be accessed from within the class. • They may be accessed from within the class that implements it in inheritance. class sample { var $count; } Developed By: Amit Lakhani, TFGP Adipur
  148. 148. Access specifier conti…• Private member: • By designing a member private, you limit the accessibility to the class in which it is declared. • The private member can’t be referred to from classes that inherit the class in which it is declared and can’t be accessed from outside of the class. • A class member can be made private by using private keyword in front of the member. class sample { private $count; } Developed By: Amit Lakhani, TFGP Adipur
  149. 149. Access specifier conti…• Protected member: • A protected property or method is accessible in the class in which it is declared, as well as in classes that extends that class. • Protected members are not available outside of those two classes. • A class member can be made protected using protected keyword in front of the member. class sample { protected $count; } Developed By: Amit Lakhani, TFGP Adipur
  150. 150. Interface Developed By: Amit Lakhani, TFGP Adipur
  151. 151. Interface concept • You can think of an interface as a template that tells you what methods a class should expose but leaves the details up to you. • Interfaces are useful in that they can help you plan your classes without immediately getting bogged down in the details. • You can also use them to distill the essential functionality from existing classes when it comes time to update and extend an application. Developed By: Amit Lakhani, TFGP Adipur
  152. 152. Interface concept • To declare an interface, simply use the interface keyword, followed by the name of the interface. • Within the body of the interface, list declarations (delimited, as with classes, by braces, {...}) for any methods to be defined by classes that implement the Interface. Developed By: Amit Lakhani, TFGP Adipur
  153. 153. Interface example <?php function p_showval() interface show { { echo "This is” $this->p_value ; function p_showval(); } } } class pclass implements show $cobj= new pclass(); { $cobj->p_getval(); var $p_value; $cobj->p_showval(); function p_getval() ?> { $this->p_value = "parent"; Output: } Developed By: Amit Lakhani, TFGP Adipur
  154. 154. Constants • A constant is a variable which holds the value and the value does not change. •To declare a constant in a class, all you have to do is precede an identifier with the const keyword. • A class constant is always public and static, and for this reason you cannot use the keywords public, private, protected, or static when declaring one. class sample { const count; -------- --------- } Developed By: Amit Lakhani, TFGP Adipur
  155. 155. Abstract classes • An abstract class is one that can’t be instantiated only inherited. • An abstract class is declared using abstract keyword. • When inheriting from an abstract class, all methods marked abstract in the parent’s class declaration must be defined by the child and these methods must be defined with the same visibility. Developed By: Amit Lakhani, TFGP Adipur
  156. 156. Final keyword • PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. • If the class itself is being defined final then it can’t be extended. class sample { final public getval() { -------- --------- } } Developed By: Amit Lakhani, TFGP Adipur
  157. 157. Summary • PHP Structure and syntax • Creating the PHP pages • Rules for PHP syntax • Integrating HTML with PHP • Constants, Variable: static and global variable • Conditional structure and looping • PHP operator • Arrays foreach construct • User defined function, argument function, variable function, return function, default argument, variable length argument Developed By: Amit Lakhani, TFGP Adipur
  158. 158. Chapter - 3 Working with functions
  159. 159. Working with functions PHP offers several built in functions for day to day uses in coding 3.1 Variable functions 3.4 Date functions 3.2 String functions 3.5 Array functions 3.3 Math functions 3.6 File functions Developed By: Amit Lakhani, TFGP Adipur
  160. 160. 3.1Variable functions Developed By: Amit Lakhani, TFGP Adipur
  161. 161. Variable functions• Gettype(): get the type of variable• Settype(): set the type of variable• Isset(): determine if the variable is set and not NULL• Unset(): unset a given variable• Strval(): get string value of variable• Floatval(): get float value of variable• Intval(): get the integer value of variable• Print_r(): prints human readable information about a variable Developed By: Amit Lakhani, TFGP Adipur
  162. 162. Gettype() function • get the type of variable • Syntax: string gettype($var) • Returns the type of the variable • Example: <?php $var=21; Output: echo gettype($var); ?> Developed By: Amit Lakhani, TFGP Adipur
  163. 163. settype() function • determine if a variable is set and is not NULL •Syntax: bool settype($var) • Returns TRUE on success otherwise false • Example: <?php $var=21; Output: settype($var, "string"); echo gettype($var); ?> Developed By: Amit Lakhani, TFGP Adipur
  164. 164. isset() function • set the type of variable • Syntax: bool isset($var, “type”) • Returns TRUE on success otherwise false <?php $var=NULL; if (isset($var)=="TRUE") { Output: echo "var is set"; } else { echo "var is not set"; }echo gettype($var);?> Lakhani, TFGP Adipur Developed By: Amit
  165. 165. unset() function • unset a given variable • Syntax: void unset($var) • Example: <?php $var="comp"; echo "before unset<br>" .$var; echo gettype($var); unset($var); echo "after unset<br>".$var; echo gettype($var); ?> Developed By: Amit Lakhani, TFGP Adipur
  166. 166. strval() function • Get string value of a variable • Syntax: strval ( mixed $var ) Example: <?php $var=25; echo strval($var); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  167. 167. floatval() function • Get float value of a variable • Syntax: floatval ( mixed $var ) Example: <?php $var=25; echo floatval($var); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  168. 168. intval() function • Get integer value of a variable • Syntax: intval ( mixed $var , [int base =10]) Example: <?php Output: echo intval(42); echo "<br>"; echo intval(4.2); ?> Developed By: Amit Lakhani, TFGP Adipur
  169. 169. print_r() function• Prints human readable information about variable• Syntax: print_r ($var)• Example: <?php $var=array(1,2,3,4,5); echo print_r($var); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  170. 170. Var_dump() function• Displays structured information (type and vale) about variable.• Syntax: var_dump ($var) Example: <?php Output: $var=1; $var2=40.50; echo var_dump($var); echo"<br>"; echo var_dump($var2); ?> Developed By: Amit Lakhani, TFGP Adipur
  171. 171. 3.2String functions Developed By: Amit Lakhani, TFGP Adipur
  172. 172. String functions • chr()- returns a one character string containing the character specified by ASCII value. • ord()- returns the ASCII value of the first character of string. • strtolower()- returns string with characters converted to lowercase • strtoupper()-returns string with characters converted to uppercase • strlen() -returns the length of the string • ltrim()-strip whitespace from the beginning of a string • rtrim()-strip whitespace from the end of a string Developed By: Amit Lakhani, TFGP Adipur
  173. 173. String functions conti…• trim()-strip whitespace from the beginning and end of a string• substr() -returns part of a string• strcmp()-compares two inputs and returns an integer.• strcasecmp()-compares two inputs and returns an integer.• strncasecmp()-case sensitive string comparison for first n characters• strpos() -find position of first occurrence of a string• strrpos() -find position of last occurrence of a string Developed By: Amit Lakhani, TFGP Adipur
  174. 174. String functions conti… • strstr() -find first occurrence of a string and is case sensitive •stristr() - find first occurrence of a string inside another string and is case sensitive • str_replace() -replace all occurrences of the search string with the replacement string. • strrev() -reverse a string • echo() -output one or more string • print() -output a string Developed By: Amit Lakhani, TFGP Adipur
  175. 175. chr() function • returns one character string by ASCII value •Syntax: string chr($var) • Example: <?php echo "The character value of of 52 is: ".chr(52); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  176. 176. ord() functionord() function • returns ASCII value of the first character of string •Syntax: int ord($string) • Example: <?php echo ord("computer"); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  177. 177. strtolower() function • returns string with characters converted to lowercase •Syntax: string strtolower($str) • Example: <?php echo "The output of string lower function:<br>"; echo strtolower("Computer DEPT."); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  178. 178. strtoupper() function • returns string with characters converted to uppercase •Syntax: string strtoupper($str) • Example: <?php echo "The output of string upper function:<br>"; echo strtoupper("Computer DEPT."); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  179. 179. strlen() function • returns the length of the string •Syntax: int strlen($str) • Example: <?php echo "The output of string length function:<br>"; echo strlen("Computer"); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  180. 180. ltrim() function • strip whitespace from the beginning of a string •Syntax: ltrim($str) • Example: <?php echo "The output of ltrim function:<br>"; $str = " Computer department"; echo "Without ltrim: " . $str; echo "The length is:". strlen($str); echo "With ltrim: " . $str; echo "The length is:". strlen(ltrim($str)); ?> Developed By: Amit Lakhani, TFGP Adipur
  181. 181. rtrim() function • strip whitespace from the end of a string • Syntax: rtrim($str) <?php echo "The output of rtrim function:<br>"; $str = "Computer department "; echo "Without rtrim: " . $str; echo "The length of the string is:". strlen($str); echo "With rtrim: " . $str; echo "The length of the string is:". Strlen(rtrim($str)); ?> Developed By: Amit Lakhani, TFGP Adipur
  182. 182. trim() function• strip whitespace from the beginning and end of a string• Syntax: string trim($str) <?php echo "The output of trim function:<br>"; $str = “ Computer department "; echo "Without trim: " . $str; echo "The length of the string is:". strlen($str); echo "With trim: " . $str; echo "The length of the string is:". Strlen(trim($str)); ?> Developed By: Amit Lakhani, TFGP Adipur
  183. 183. substr() function• returns part of a string• Syntax: string substr($str, int $start, [int $length])• Example: <?php echo "The output of substring function:<br>"; echo substr("Computer department!",3); echo substr("Computer department!",3,5); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  184. 184. strcmp() function• compares two inputs and returns an integer.• Syntax: int strcmp(string $str_1, string $str_2)• Return values: returns < 0, if str_1 < str_2 returns > 0, if str_1 > str_2 returns 0, if str_1 = str_2•Example: <?php echo "The output of string compare function:<br>"; echo strcmp("Computer","Computer"); ?> Developed By: Amit Lakhani, TFGP Adipur
  185. 185. strcasecmp() function• compares two inputs and returns an integer.• Syntax: int strcasecmp(string $str_1, string $str_2)• Return values: returns < 0, if str_1 < str_2 returns > 0, if str_1 > str_2 returns 0, if str_1 = str_2•Example: <?php echo "The output of string compare function:<br>"; echo strcasecmp("Computer","COMPUTER"); ?> Developed By: Amit Lakhani, TFGP Adipur
  186. 186. strncasecmp() function• case sensitive string comparison for first n characters• Syntax: int strncasecmp(string $str_1, string $str_2, int len)• Return values: returns < 0, if str_1 < str_2 returns > 0, if str_1 > str_2 returns 0, if str_1 = str_2•Example: <?php echo "The output of string compare function:<br>"; echo strncasecmp("Computer","COMPUTER“, 3); ?> Developed By: Amit Lakhani, TFGP Adipur
  187. 187. strpos() function• find position of first occurrence of a string• Syntax: int strpos(string $str, $needle, [ int $offset])•Example: <?php echo "The output of string position function:<br>"; echo strpos("Computer","m“, 2); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  188. 188. strrpos() function• find position of last occurrence of a string• Syntax: int strrpos(string $str, $needle, [ int $offset])•Example: <?php echo "The output of string position function:<br>"; echo strrpos("Computer","m“, 2); ?> Output: The output of string position function: 2 Developed By: Amit Lakhani, TFGP Adipur
  189. 189. strstr() function• find first occurrence of a string and is case sensitive• Syntax: strstr($str,search)•Example: <?php echo "The output of strstr() function:<br>"; echo strstr("Computer","m“); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  190. 190. stristr() function• find first occurrence of a string inside another string and is case sensitive• Syntax: stristr($str,search)•Example: <?php echo "The output of stristr() function:<br>"; echo stristr("Computer","m“); ?> Developed By: Amit Lakhani, TFGP Adipur
  191. 191. str_replace() function• replace all occurrences of the search string with the replacement string.• Syntax: str_replace( find, replace, string, count )•Example: <?php echo "The output of string replace function with case sensitive: <br>"; echo str_replace("department","engg.","computer department", $i); echo $i; ?> Developed By: Amit Lakhani, TFGP Adipur
  192. 192. strrev() function • reverse a string • Syntax: strrev( $string) •Example: <?php echo "The output of strrev() function: <br>"; echo strrev("computer department”); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  193. 193. echo() function • output one or more string • Syntax: echo( $string) •Example: <?php echo "The output of echo function<br>"; $i=1; echo $i; ?> Developed By: Amit Lakhani, TFGP Adipur
  194. 194. print() function • output a string • Syntax: print( $string) •Example: <?php print (“The output of print function<br>“); $i=1; print $i; ?> Developed By: Amit Lakhani, TFGP Adipur
  195. 195. 3.3Math functions Developed By: Amit Lakhani, TFGP Adipur
  196. 196. Maths function • abs()-returns absolute value of a number • ceil() -returns the value of a number rounded UPWARDS to the nearest integer. • floor()-returns the next lowest integer value by rounding down value. • round() -returns the rounded value to specified precision. • fmod() -returns the floating point remainder of dividing the dividend by the divisor. •min() -returns smallest value Developed By: Amit Lakhani, TFGP Adipur
  197. 197. Maths function conti.. • max() -returns largest value • pow() -returns base raised to the power of exp. • sqrt() -returns the square root of the given number • rand() -returns random value between min and max Developed By: Amit Lakhani, TFGP Adipur
  198. 198. abs() function • returns absolute value of a number •Syntax: abs(num) • Example: <?php echo "The output of abs() function<br>"; echo(abs(6.99) . "<br />"); echo(abs(-3) . "<br />"); echo(abs(3)); ?> Developed By: Amit Lakhani, TFGP Adipur
  199. 199. ceil() function• returns the value of a number rounded UPWARDS to the nearest integer.• Syntax: float ceil(num)• Example: <?php echo "The output of ceil() function<br>"; echo(ceil(0.60) . "<br />"); echo(ceil(0.40) . "<br />"); echo(ceil(5) . "<br />"); echo(ceil(5.1) . "<br />"); ?> Developed By: Amit Lakhani, TFGP Adipur
  200. 200. floor() function• returns the next lowest integer value by rounding down value.• Syntax: float floor(num)• Example: <?php echo "The output of floor() function<br>"; echo(floor(0.60) . "<br />"); echo(floor(0.40) . "<br />"); echo(floor(5.1) . "<br />"); echo(floor(-5.1) . "<br />"); ?> Developed By: Amit Lakhani, TFGP Adipur
  201. 201. round() function• returns the rounded value to specified precision.• Syntax: float round(num)• Example: <?php echo "The output of round() function<br>"; echo(round(0.60) . "<br />"); echo(round(10.28765,2) . "<br />"); echo(round(-4.40) . "<br />"); echo(round(-4.60)); ?> Developed By: Amit Lakhani, TFGP Adipur
  202. 202. fmod() function• returns the floating point remainder of dividing the dividend by the divisor.• Syntax: float fmod($x,$y)• Example: <?php echo "The output of fmod() function to display remainder(modulo)<br>"; $r = fmod(5,2); echo $r; ?> Developed By: Amit Lakhani, TFGP Adipur
  203. 203. min() function• returns smallest value• Syntax: min($val1, $val2,….)• Example: <?php echo "The output of min() function <br>"; $r = min(5,2); echo $r; ?> Developed By: Amit Lakhani, TFGP Adipur
  204. 204. max() function• returns largest value• Syntax: max($val1, $val2,….)• Example: <?php echo "The output of max() function <br>"; echo(max(5,7) . "<br />"); echo(max(-3,5) . "<br />"); ?> Developed By: Amit Lakhani, TFGP Adipur
  205. 205. pow() function• returns base raised to the power of exp.• Syntax: pow($base, $exp)• Example: <?php echo "The output of pow() function <br>"; echo pow(4,2) . "<br />"; echo pow(6,2) . "<br />"; ?> Developed By: Amit Lakhani, TFGP Adipur
  206. 206. sqrt() function• returns the square root of the given number• Syntax: float sqrt($arg)• Example: <?php echo "The output of sqrt() function <br>"; echo(sqrt(1) . "<br />"); echo(sqrt(9) . "<br />"); ?> Developed By: Amit Lakhani, TFGP Adipur
  207. 207. rand() function• returns random value between min and max• Syntax: int rand(void) int rand($min, $max)• Example: <?php echo "The output of rand() function <br>"; echo(rand() . "<br />"); echo(rand(10,100)); ?> Developed By: Amit Lakhani, TFGP Adipur
  208. 208. 3.4Date functions Developed By: Amit Lakhani, TFGP Adipur
  209. 209. date() function• Date() – format a local date/time• Getdate() – get date time information• Setdate() – sets the date• Checkdate() – validates a date• Time() – return current UNIX timestamp• Mktime() – get the UNOX timestamp for a date Developed By: Amit Lakhani, TFGP Adipur
  210. 210. date() function• returns a local time and date•Syntax: string date($format, [int $timestap])• Example: <?php echo "The output of date() function”; echo date("d"); echo date("D"); echo date("j"); ?> Developed By: Amit Lakhani, TFGP Adipur
  211. 211. date() function formats Required. Specifies how to return the result: d - The day of the month (from 01 to 31) D - A textual representation of a day (three letters) j - The day of the month without leading zeros (1 to 31) l (lowercase L) - A full textual representation of a day N - The ISO-8601 numeric representation of a day (1 for Monday through 7 for Sunday) S - The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j) w - A numeric representation of the day (0 for Sunday through 6 for Saturday) z - The day of the year (from 0 through 365) W - The ISO-8601 week number of year (weeks starting on Monday) Developed By: Amit Lakhani, TFGP Adipur
  212. 212. date() function formats Required. Specifies how to return the result: F - A full textual representation of a month (January through December) m - A numeric representation of a month (from 01 to 12) M - A short textual representation of a month (three letters) n - A numeric representation of a month, without leading zeros (1 to 12) t - The number of days in the given month L - Whether its a leap year (1 if it is a leap year, 0 otherwise) o - The ISO-8601 year number Y - A four digit representation of a year y - A two digit representation of a year Developed By: Amit Lakhani, TFGP Adipur
  213. 213. date() function formats Required. Specifies how to return the result: a - Lowercase am or pm A - Uppercase AM or PM B - Swatch Internet time (000 to 999) g - 12-hour format of an hour (1 to 12) G - 24-hour format of an hour (0 to 23) h - 12-hour format of an hour (01 to 12) H - 24-hour format of an hour (00 to 23) i - Minutes with leading zeros (00 to 59) s - Seconds, with leading zeros (00 to 59) e - The timezone identifier (Examples: UTC, Atlantic/Azores) I (capital i) - Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise) O - Difference to Greenwich time (GMT) in hours (Example: +0100) Developed By: Amit Lakhani, TFGP Adipur
  214. 214. date() function formats Required. Specifies how to return the result: T - Timezone setting of the PHP machine (Examples: EST, MDT) Z - Timezone offset in seconds. The offset west of UTC is negative, and the offset east of UTC is positive (-43200 to 43200) c - The ISO-8601 date (e.g. 2004-02-12T15:19:21+00:00) r - The RFC 2822 formatted date (e.g. Thu, 21 Dec 2000 16:01:07 +0200) U - The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) Developed By: Amit Lakhani, TFGP Adipur
  215. 215. getdate() function• returns an array containing the date information of the timestap.• Syntax: array getdate([int $timestap])• Example: <?php echo "The output of getdate() function<br>"; print_r(getdate()); ?> Developed By: Amit Lakhani, TFGP Adipur
  216. 216. setdate() function• Resets the current date of the datetime object to a different date.• Syntax: DateTime::setDate(int $y, int $m, int $y)• Example: <?php echo "The output of setdate() function<br>"; $d=new DateTime(); $d->setDate(2011,4,2); echo $d->format(d-m-y); ?> Developed By: Amit Lakhani, TFGP Adipur
  217. 217. checkdate() function• Checks the validity of the date formed by the argument• Syntax: bool checkdate(int $m, int $d, int $y)• Returns TRUE if the given date is valid• Example: <?php echo "The output of checkdate() function<br>"; print_r(checkdate(11,30,2000)); ?> Developed By: Amit Lakhani, TFGP Adipur
  218. 218. time() function• Returns the current UNIX timestamp• Syntax: int time (void)• Returns the current time measured in number of seconds ( Jan 1 1970 00:00:00 GMT)• Example: <?php echo "The output of time() function<br>"; echo time(); ?> Developed By: Amit Lakhani, TFGP Adipur
  219. 219. mktime() function• Returns the UNIX timestamp corresponding to the arguments given.• Syntax: int mktime($hrs, $mins, $sec, $mnt, $day, $yr)•Example: <?php echo "The output of mktime() function<br>"; echo mktime(); echo(date("M-d-Y",mktime(0,0,0,12,36,2001))); echo(date("M-d-Y",mktime(0,0,0,14,1,2001))); ?> Developed By: Amit Lakhani, TFGP Adipur
  220. 220. 3.5Array functions Developed By: Amit Lakhani, TFGP Adipur
  221. 221. Array functionscount() -Counts all elements in an array, or properties in an objectarray_count_values()-Returns an array of values from inputas keys and their count as value.list() -Assigns variables to valuesIn_array()-checks if a value exists in an arraycurrent() -Return the current element in an arraynext()-Advance the internal array pointer of an array Developed By: Amit Lakhani, TFGP Adipur
  222. 222. Array functions conti..prev()-Rewind the internal pointer of an arrayend()-Set the internal pointer of an array to its last elementeach()-Returns the current key and value pair from an array and advances the array cursorsort()-Sorts an arrayArray_merge()-Merges the elements of one or more arraytogether so that the values of one or more are appended tothe end of the previous array. Developed By: Amit Lakhani, TFGP Adipur
  223. 223. Array functions conti..Array_reverse()-Returns the array with the order of theelements reversed.Array_sum()- Returns the sum of values in an array Developed By: Amit Lakhani, TFGP Adipur
  224. 224. count() function• Counts all elements in an array, or properties in an object• Syntax: count($var)• Example: <?php $varArray=array(1,2,3,4,5); echo "The number of elements in first array=".count($varArray); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  225. 225. Array_count_values() function• Returns an array of values from input as keys and their count as value.• Syntax: array array_count_values($var)• Example: <?php echo "The output of array_count_values() function<br>"; $arr=array(1,2,3,4,5); print_r(array_count_values($arr)); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  226. 226. list() function• Assigns variables to values• Syntax: list($var1, $var2,…..)Example: <?php echo "The output of list() function<br>"; $arr=array(1,2,3); list($a,$b,$c)=$arr; echo “The list of values are:<br> $a <br> $b <br> $c "; ?> Developed By: Amit Lakhani, TFGP Adipur
  227. 227. In_array() function• checks if a value exists in an array• Syntax: in_array($key, $array)• Example: <?php $varArray=array(1,2,3); if(in_array(1,$varArray)) { print_r($varArray[1]); } ?> Developed By: Amit Lakhani, TFGP Adipur
  228. 228. current() function• Return the current element in an array• Syntax: mixed current($array)• Example: <?php $arr=array(1,2,3); echo "The current element is ". current($arr); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  229. 229. next() function• Advance the internal array pointer of an array• Syntax: mixed next($array)• Example: <?php $arr=array(1,2,3); echo "The current element is ". current($arr); echo "<br>The next element is ". next($arr); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  230. 230. prev() function• Rewind the internal pointer of an array• Syntax: mixed prev($array)• Example: <?php $arr=array(1,2,3); echo "The current element is ". current($arr); echo "<br>The next element is ". next($arr); echo "<br>The next element is ". prev($arr); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  231. 231. end() function• Set the internal pointer of an array to its last element• Syntax: mixed end($array)• Example: <?php $arr=array(1,2,3); echo "The last element is ". end($arr); ?> Output: Developed By: Amit Lakhani, TFGP Adipur
  232. 232. each() function• Returns the current key and value pair from an array and advances the array cursor• Syntax: array each($array)• Example: <?php $arr=array(1,2,3); $ele=each($arr); echo "The element is "; print_r($ele); ?> Developed By: Amit Lakhani, TFGP Adipur
  233. 233. sort() function• Sorts an array• Syntax: bool sort($array)• Example: <?php $arr=array(1,2,3); sort($arr); print_r($arr); ?> Output: Developed By: Amit Lakhani, TFGP Adipur

×