• Like
Osp ii presentation
Upcoming SlideShare
Loading in...5
×

Osp ii presentation

  • 978 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
978
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
38
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • Outputs
  • Statements
  • Up to 14-01-11 D batch
  • 17-01-11 DBatch stop

Transcript

  • 1. 02/21/12 Open Source Programming
  • 2. 02/21/12 Open Source Programming Introduction Unit – I Open source Programming PHP, Apache, MySQL, Postgress, SQL and Perl- Overview of PHP – Variables, operators, Constants, control structures arrays, Functions, classes – Handling files.
  • 3. 02/21/12 By allowing the open exchange of information, programmers from all over the world contribute to make a truly powerful and efficient piece of software without royalties or fees. Open Source Programming What is Open source
  • 4. 02/21/12
    • They are cross-platform and “technology-neutral.”
    • They embrace diversity.
    • They are free.
    Open Source Programming Why OSP ROCKS?
  • 5. 02/21/12
    • Linux — operating system kernel based on Unix
    • GNU OS — a complete Unix-like operating system
    • OpenBSD — produces a FREE , UNIX-like operating system.(Berkeley Systems Distribution)
    • FreeBSD — operating system derived from Unix an advanced operating system for modern server, desktop, and embedded computer platforms
    • OpenSolaris — Unix Operating System from Sun Microsystems
    • Symbian — real-time mobile operating system designed for smartphones.
    Open Source Programming OSP Software - OS
  • 6. 02/21/12 Apache — HTTP web server Tomcat web server — web container Mediawiki — wiki server software Alfresco, TYPO3 — content management system RenovatioCMS — content management system Joomla — content management system Drupal — content management system provides a collaborative environment for social networking sites, corporate Web sites, intranets, e-commerce applications and discussion sites. Open Source Programming OSP Software - Server
  • 7. 02/21/12 WordPress — blog software MongoDB — document-oriented, non-relational database Moodle — course management system or virtual learning environment openSIS — open source Student Information System Open Source Programming OSP Software
  • 8. 02/21/12 osCommerce — allows store owners to setup, run, and maintain online stores with minimum effort and with no costs P eaZip — File archiver Mozilla Firefox — web browser OpenOffice.org — office suite Stockfish — chess engine series, one of the strongest chess programs 7-Zip – File Archiver Open Source Programming OSP Software
  • 9. 02/21/12 PHP — Hypertext Preprocessor Ruby,Tcl/Tk PERL & Python — Interpreted Dynamic Language MySQL — Data Base Open Source Programming OSP Software (Others)
  • 10. open source companies
    • IBM
        • uses and develops Apache and Linux;
    • Apple
        • released core layers of Mac OS X Server as an open source BSD operating system called Darwin; open sourcing the QuickTime Streaming Server and the OpenPlay network gaming toolkit
    • HP
        • uses and releases products running Linux
    02/21/12
  • 11. 02/21/12
    • Sun
        • supports some open source development
    • Red Hat Software
      • Linux vendor
  • 12. 02/21/12
  • 13. 02/21/12
    • MySQL : It enables PHP and Apache to work together to access and display data in a readable format to a browser.
    Open Source Programming
    • AMP is an acronym formed from the initials of A pache, M ySQL and P HP or P ython or P erl.
    • Apache : It acts as Web server . Its main job is to parse any file requested by a browser and display the correct results according to the code within that file.
    • PHP : PHP is a server-side scripting language that allows your Web site to be truly dynamic.
    Intro to AMP Package
  • 14. 02/21/12 Open Source Programming AMP as Restaurant
    • Apache – This is the Chef . Whatever people ask for, prepares it without complaint. She is quick, flexible, and able to prepare a multitude of different types of foods.
  • 15. 02/21/12 Open Source Programming AMP as Restaurant
    • PHP – This is the Waiter , gets requests from the patron and carries them back to the kitchen with specific instructions about how the meal should be prepared.
  • 16. 02/21/12 Open Source Programming AMP as Restaurant MySQL – This is Stockroom of ingredients.
  • 17. LICENSES
    • License that are popular and widely used or with strong communities ::.
    • Apache License, 2.0 (Apache-2.0)
    • BSD 3-Clause "New" or "Revised" license (BSD-3-Clause)
    • BSD 3-Clause "Simplified" or"FreeBSD" license (BSD-2-Clause)
    02/21/12
  • 18. 02/21/12 GNU General Public License (GPL) GNU Library or "Lesser" General Public License (LGPL) MIT license (MIT) Mozilla Public License 2.0 (MPL-2.0) Common Development and Distribution License (CDDL-1.0) Eclipse Public License (EPL-1.0)
  • 19. 02/21/12
    • Appache is a open source Web server Responds to client requests by providing resources
    • Web server and client communicate with platform-independent Hypertext Transfer Protocol (HTTP).
    • Apache server runs mostly on unix,linux and solaris platform.
    • It is secure (password protected pages)
    • It has been tested thoroughly by developers and users
    • Correctness(It MUST conform to the HTTP spec)
    • Speed
    Open Source Programming Apache Introduction
  • 20. 02/21/12
    • Request methods
      • get
      • post
      • Retrieve and send client form data to Web server
      • Post data to a server-side form handler
    Open Source Programming HTTP request types
  • 21. 02/21/12
    • Multi-tier application ( n -tier application)
      • Information tier (data or bottom tier)
        • Maintains data for the application
        • Stores data in a relational database management system (RDBMS)
      • Middle tier
        • Implements business logic and presentation logic
        • Control interactions between application clients and application data
      • Client tier (top tier)
        • Application’s user interface
        • Users interact directly with the application through the client tier
    Open Source Programming System Architecture
  • 22. 02/21/12 Open Source Programming
    • P HP H ypertext P reprocessor.
      • Other Names : P ersonal H ome P age, P rofessional H ome P age
    • PHP 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.
    • create your .php files, put them in your web directory and the server will automatically parse them for you. There is no need to compile anything nor do you need to install any extra tools.
    Overview of Php
  • 23. 02/21/12
    • Is an open source.
    • 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
    Open Source Programming Features of PHP
  • 24. 02/21/12 Open Source Programming CLIENT WEB SERVER HTTP Request (url) <HTML> <?php PHP code ?> </HTML> Gets Page <HTML> <B>Hello</B> </HTML> Interprets the PHP code Server response How it works? Browser creates the web page
  • 25. 02/21/12 Open Source Programming
    • PHP programs are written using a text editor, such as Notepad or WordPad, just like HTML pages.
    • PHP pages, for the most part, end in a .php extension. This extension signifies to the server that it needs to parse the PHP code before sending the resulting HTML code to the viewer’s Web browser.
    PHP with HTML
  • 26. 02/21/12 Open Source Programming
    • What makes PHP so different is that it not only allows HTML pages to be created.
    • It is invisible to your Web site visitors. The only thing they see the resulting HTML output. This gives you more security for your PHP code and more flexibility in writing it.
    • HTML can also be written inside the PHP section of your page.
    • PHP can also be written as a standalone program, with no HTML
    PHP with HTML
  • 27. 02/21/12 Open Source Programming PHP is denoted in the page with opening and closing tags as follows: <?php //php code ; ?> PHP Syntax
  • 28. 02/21/12 <HTML> <HEAD> <TITLE> My First PHP Program </TITLE> <h1> </HEAD> <BODY> <?php echo “I’m a PHP Program.”; ?> </BODY> </HTML> Open Source Programming Code PHP Programs Result
  • 29. 02/21/12 <?php $name=“VIT UNIVERSITY”; echo ‘My name is ‘,$name; ?> Open Source Programming Code PHP Programs Result
  • 30. 02/21/12
    • PHP is an untyped language
      • variables type can change on the fly.
    • Four basic data types:
    • More data types
      • Integer
      • Double
      • String
      • Boolean
      • Array
      • Object
    Open Source Programming Data Type
  • 31. 02/21/12
    • ..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.
    Open Source Programming Constants
  • 32. 02/21/12
    • 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.
    Open Source Programming Variables
  • 33. 02/21/12 Open Source Programming
    • Rules for Naming Variables:
    • In PHP, unlike some other programming languages, there is no restriction on the size of a variable name.
    • Variable names should identified by dollar ($) symbol.
      • Variable names can begin with an underscore.
      • Variable names cannot begin with a numeric character.
      • Variable names must be relevant and self-explanatory.
    Variables
  • 34. 02/21/12 Open Source Programming Variables
    • Valid Examples Non Valid Examples
      • $prod_desc $9OctSales
      • $Intvar Sales123
      • $_Salesamt $*asgs
  • 35. 02/21/12 $MyStrVal = &quot; This is an example of a string value &quot;; $MyIntVal = 145665; // An integer value $MyBoolval = True; // A Boolean value can either be True or False $MyFloatVal=2346.45 // A float value $MyArrVal[0] = &quot;My&quot;; //A array containing three elements $MyArrVal[1] = &quot;First&quot;; $MyArrVal[2] = &quot;Array&quot;; Open Source Programming Variables (cont)
  • 36. 02/21/12 Open Source Programming In PHP, Variable type can be changed by Settype Syntax: Settype(Variablename, “newDataType”); E.g. $pi = 3.14 //float Settype($pi,”string”); //now string – “3.14” Settype($pi,”integer”);// now integer - 3 Settype Settype
  • 37. 02/21/12 Open Source Programming Syntax: Gettype(Variablename); E.g. $pi = 3.14; //float print gettype($pi); Print “---$pi <br>”; Settype($pi,”string”); print gettype($pi); Print “---$pi <br>”; Settype($pi,”integer”); print gettype($pi); Print “---$pi <br>”; In PHP, Variable type and can know by Gettype . Settype & Gettype
  • 38. 02/21/12
    • 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;
    Open Source Programming Operators
  • 39. 02/21/12 Open Source Programming
    • In a double-quoted string any variable names are expanded to their values.
    • In a single-quoted string, no variable expansion takes place.
    $name = &quot;Phil&quot;; $age = 23; echo '$name is $age'; $name = &quot;Phil&quot;; $age = 23; echo “$name is $age”; Output is $name is $age Output is Phil is 23 Quotes ( “ Vs. ‘ )
  • 40. 02/21/12 Logical Operator Open Source Programming Operators (cont) Comparison Operator And Or ! Xor && ( high precedence) || ( high precedence) == != < > <= >= === (identical)
  • 41. 02/21/12 Open Source Programming
    • Single Line Comment - Single line comment in PHP is identified with //.
    • <?Php
    • // my first prg
    • ?>
    • Multi Lines Comment - Multi lines comment in PHP is identified by /* … */
    • <?Php
    • /* line1
    • line2
    • line 3 */
    • ?>
    Comment Line in PHP
  • 42. 02/21/12 Open Source Programming 1. Echo & Print() is the common method in outputting data. Since it is a language construct, echo doesn’t require parenthesis like print(). 2. Output Text Usage: <?php echo “Hello World”; print(“Hello World”); // prints out Hello World ?> Output in PHP
  • 43. 02/21/12 Open Source Programming 3. Output the value of a PHP variable: <?php echo $hits; // prints out the number of hits print ($hits); // returns a value if print process success. ?> 4. Can use “<br>” , to print in next line: <?php echo $a , “<br>”, $b ; // prints a & b in separate lines ?> Output in PHP
  • 44. 02/21/12 1. If…else Statement – single decision stmt
      • if (<condition>)
      • {
        • // True part Php code
        • }
      • else
      • {
        • // False part php code
        • }
      • if ($num1 > $num2 )
      • {
        • $max = $num1;
        • }
      • else
      • {
      • $max = $num2;
        • }
    Syntax E.g. Open Source Programming Conditional Statements (Branching)
  • 45. 02/21/12 2. If…else if Statement – multiple decision stmt
      • if (<condition1>)
      • {
        • // True part Php code
        • }
      • else if (<condition2>)
      • {
        • //1 st False 2 nd true part php code
        • }
        • else
        • {
        • // false part PhP code
        • }
      • if ($num1 > $num2 )
      • {
        • $max = $num1;
        • }
      • else if ($num2 > $num3 )
      • {
      • $max = $num3;
        • }
        • else
        • $max = $num2 ;
    Syntax E.g. Open Source Programming Conditional Statements (Branching)
  • 46. 02/21/12 3. Switch Statement – replacement of “if …… else if stmt”.
      • switch (expression)
      • {
      • case value1: {stmts1; break;}
      • case value2: {stmts2; break;}
      • …………
      • …………
      • [default: stmts;]
      • }
      • switch ($day)
      • {
      • case 1: {echo “its Sunday”; break;}
      • case 2: {echo “its Monday”; break;}
      • ………
      • ………
      • case 7: {echo “its Saturday”; break;}
      • default: {echo “not specified value”;}
      • }
    Syntax E.g. Open Source Programming Conditional Statements (Branching)
  • 47. 02/21/12 Open Source Programming 4. For Statement – replacement of “if …… else if stmt”.
      • for( initial_expression;
      • termination_check;
      • index_updation)
      • {
      • statement (S);
      • }
      • for($i=1; $i < 10; $i++)
      • echo $i;
      • (or)
      • $i=1;
      • for(;$i<10;)
      • {
      • echo $i;
      • $i=$i+1;
      • }
    Syntax E.g. Conditional Statements (Looping)
  • 48. 02/21/12 5. Do … While Statement
      • do
      • {
      • Statement (S);
      • }
      • while (<condition>);
      • do
      • {
      • $i=$i+1;
      • echo $i;
      • } while ($i < 10);
    Syntax E.g. Open Source Programming Conditional Statements (Looping)
  • 49. 02/21/12 5. While Statement
      • while(<condition>)
      • {
      • Statement (S);
      • }
    • while ($i < 10)
    • {
      • $i=$i+1;
      • echo $i;
      • }
    Syntax E.g. Open Source Programming Conditional Statements (Looping)
  • 50. 02/21/12
    • In Php, arrays are lists of bits of information mapped with keys and stored under one variable name. For example, you can store a person’s name and address or a list of states in one variable.
    Open Source Programming
    • Generally, array is a collection of homogeneous elements.
    Arrays Let’s store a person’s name and age under one variable name : $name = array(‘firstname’=>’Albert’,‘lastname’=>’Einstein’,‘age’=>124);
  • 51. 02/21/12 Open Source Programming <?php $name[ ’firstname’ ] = “Albert”; $name[ ’lastname’ ] = “Einstein”; $name[ ’age’ ] = 124; ?> <?php $flavor[] = ‘blue rasberry’; $flavor[] = ‘root beer’; $flavor[] = ‘pineapple’; ?> Array with Implicit key Array with Explicit key Arrays
  • 52. 02/21/12 Open Source Programming Array of arrays is called as multidimensional arrays. $Student = array ( &quot;0&quot;=> array (&quot;name&quot;=>&quot;James&quot;, &quot;sex&quot;=>&quot;Male&quot;, &quot;age&quot;=>&quot;28&quot;), &quot;1&quot;=> array (&quot;name&quot;=>&quot;John&quot;, &quot;sex&quot;=>&quot;Male&quot;, &quot;age&quot;=>&quot;25&quot;), &quot;2&quot;=> array (&quot;name&quot;=>&quot;Susan&quot;, &quot;sex&quot;=>&quot;Female&quot;, &quot;age&quot;=>&quot;24&quot;)); $student[“1&quot;][“name&quot;] – returns John Multidimensional Arrays
  • 53. 02/21/12 $u= array ( &quot;0&quot;=> array (&quot;name&quot;=>&quot;James&quot;, &quot;sex&quot;=>&quot;Male&quot;, &quot;age&quot;=>&quot;28&quot;), &quot;1&quot;=> array (&quot;name&quot;=>&quot;John&quot;, &quot;sex&quot;=>&quot;Male&quot;, &quot;age&quot;=>&quot;25&quot;), &quot;2&quot;=> array (&quot;name&quot;=>&quot;Susan&quot;, &quot;sex&quot;=>&quot;Female&quot;, &quot;age&quot;=>&quot;24&quot;)); foreach ($u as $key=>$value) { echo &quot;$key is $value&quot;; echo &quot;The actual user is $key.<br/>&quot;; foreach ($value as $iKey => $iValue) { echo &quot; ---> $iKey -> $iValue <br/>&quot;; } } ?>
  • 54. 02/21/12 Output : 0 is Array The actual user is 0. ---> name -> James ---> sex -> Male ---> age -> 28 1 is Array The actual user is 1. ---> name -> John ---> sex -> Male ---> age -> 25 2 is Array The actual user is 2. ---> name -> Susan ---> sex -> Female ---> age -> 24
  • 55. 02/21/12 <html> <body> <FORM ACTION =“arr.php&quot; METHOD = &quot;post&quot;> ur name <INPUT TYPE=&quot;text&quot; NAME=&quot;ur name&quot;><br> cost <INPUT TYPE=&quot;text&quot; NAME=&quot;cost&quot;><br> days <INPUT TYPE=&quot;text&quot; NAME=&quot;days&quot;><br> <INPUT TYPE=&quot;submit&quot;name=&quot;submit&quot;><br> <?php if(isset($_POST['submit'])) { $d[0]=$_POST['cost']; $d[1]=$_POST['days']; $d[2]=$d[0]+$d[1]; echo &quot;output <INPUT TYPE=text value=$d[2]>&quot;; foreach($d as $v) echo &quot;output <INPUT TYPE=text value=$v>&quot; } ?> </body> </form> </html>
  • 56. 02/21/12 Functions: A function is a block of code that is not immediately executed but can be called by scripts whenever needs. Functions can be built-in or user-defined . They can require information to be passed to them and usually return a value. Open Source Programming Functions
  • 57. 02/21/12 Open Source Programming <?php $a=array(&quot;a&quot;=>&quot;Dog&quot;,&quot;b&quot;=>&quot;Cat&quot;,&quot;c&quot;=>&quot;Horse&quot;); print_r($a); ?> Array ( [a] => Dog [b] => Cat [c] => Horse ) Array( key=>value ) – Creates an array with keys and values Output: E.g.: Array Functions
  • 58. 02/21/12 Open Source Programming array_chunk( array,size,preserve_key ) - splits an array into chunks of new arrays Array Functions
  • 59. 02/21/12 Array ( [0] => Array ( [0] => Cat [1] => Dog ) [1] => Array ( [0] => Horse [1] => Cow ) ) Output: <?php $a=array(&quot;a&quot;=>&quot;Cat&quot;,&quot;b&quot;=>&quot;Dog&quot;,&quot;c&quot;=>&quot;Horse&quot;,&quot;d&quot;=>&quot;Cow&quot;); print_r(array_chunk($a,2)); ?> E.g.: 1 Open Source Programming Array Functions
  • 60. 02/21/12 Array ( [0] => Array ( [a] => Cat [b] => Dog ) [1] => Array ( [c] => Horse [d] => Cow ) ) Output: <?php $a=array(&quot;a&quot;=>&quot;Cat&quot;,&quot;b&quot;=>&quot;Dog&quot;,&quot;c&quot;=>&quot;Horse&quot;,&quot;d&quot;=>&quot;Cow&quot;); print_r(array_chunk($a,2,true)); ?> E.g.: 2 Open Source Programming Array Functions
  • 61. 02/21/12 array_combine( array1,array2 ) creates an array by combining two other arrays, where the first array is the keys, and the other array is the values. Open Source Programming Array Functions Array ( [a] => Cat [b] => Dog [c] => Horse [d] => Cow ) Output: <?php $a1=array(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;); $a2=array(&quot;Cat&quot;,&quot;Dog&quot;,&quot;Horse&quot;,&quot;Cow&quot;); print_r(array_combine($a1,$a2)); ?> E.g.:
  • 62. 02/21/12 array_count_values( array ) returns an array, where the keys are the original array's values, and the values is the number of occurrences. Open Source Programming <?php $a=array(&quot;Cat&quot;,&quot;Dog&quot;,&quot;Horse&quot;,&quot;Dog&quot;); print_r(array_count_values($a)); ?> E.g: Array ( [Cat] => 1 [Dog] => 2 [Horse] => 1 ) Output: Array Functions
  • 63. 02/21/12 array_diff( arr1,arr2,… ) function compares two or more arrays, and returns an array with the keys and values from the first array, only if the value is not present in any of the other arrays. Open Source Programming <?php $a1=array(0=>&quot;Cat&quot;,1=>&quot;Dog&quot;,2=>&quot;Horse&quot;); $a2=array(3=>&quot;Horse&quot;,4=>&quot;Dog&quot;,5=>&quot;Fish&quot;); print_r(array_diff($a1,$a2)); ?> E.g: Array ( [0] => Cat ) Output: Array Functions
  • 64. 02/21/12 The array_diff_assoc( arr1, arr2,… ) function compares two or more arrays, and returns an array with the keys and values from the first array, only if they are not present in any of the other arrays. Computes the difference of arrays with additional index check. Open Source Programming <?php $a1=array(0=>&quot;Cat&quot;,1=>&quot;Dog&quot;;,2=>&quot;Horse&quot;); $a2=array(0=>&quot;Rat&quot;,1=>&quot;Horse&quot;;,2=>&quot;Dog&quot;); $a3=array(0=>&quot;Horse&quot;,1=>&quot;Dog&quot;,2=>&quot;Cat&quot;); print_r(array_diff_assoc($a1,$a2,$a3)); ?> E.g: Array ( [0] => Cat [2] => Horse ) Output: Array Functions
  • 65. 02/21/12 array_fill ( start,number,value ) function returns an array filled with the values you describe where start is starting index,number defines number of entries. Open Source Programming <?php $a=array_fill(2,3,&quot;Dog&quot;); print_r($a); ?> E.g: Array ( [2] => Dog [3] => Dog [4] => Dog ) Output: Array Functions
  • 66. 02/21/12 The array_flip ( array ) function returns an array with all the original keys as values, and all original values as keys. Open Source Programming <?php $a=array(0=>&quot;Dog&quot;,1=>&quot;Cat&quot;,2=>&quot;Horse&quot;); print_r(array_flip($a)); ?> E.g: Array ( [Dog] => 0 [Cat] => 1 [Horse] => 2 ) Output: Array Functions
  • 67. 02/21/12 The array_key_exists( key,arr ) function checks an array for a specified key, and returns true if the key exists and false is the key does not exist. Open Source Programming <?php $a=array(&quot;a&quot;=>&quot;Dog&quot;,&quot;b&quot;=>&quot;Cat&quot;); if (array_key_exists(&quot;a&quot;,$a))     echo &quot;Key exists!&quot;;   else     echo &quot;Key does not exist!&quot;;   ?> E.g: Key exists! Output: Array Functions
  • 68. 02/21/12 The array_merge( array1,array2,array3... ) function merges one or more arrays into one array. If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. Open Source Programming <?php $a1=array(&quot;a&quot;=>&quot;Horse&quot;,&quot;b&quot;=>&quot;Dog&quot;); $a2=array(&quot;c&quot;=>&quot;Cow&quot;,&quot;b&quot;=>&quot;Cat&quot;); print_r(array_merge($a1,$a2)); ?> E.g: Array ( [a] => Horse [b] => Cat [c] => Cow ) Output: Array Functions
  • 69. 02/21/12 Open Source Programming <?php $a=array(0=>&quot;v&quot;,1=>&quot;h&quot;); $a1=array(1=>&quot;h&quot;,2=>&quot;g&quot;); print_r(array_merge($a,$a1))f; ?> E.g: Array ( [0] => Horse [1] => Dog ) Output: Array Functions
  • 70. 02/21/12 array_multisort( array1 , sortingorder , sorting type , array2, array3 ) returns a sorted array. You can assign one or more arrays. Open Source Programming <?php $a1=array(“Dog&quot;,&quot;Cat&quot;); $a2=array(&quot;Fido&quot;,&quot;Missy&quot;); array_multisort($a1,$a2); print_r($a1); print_r($a2); ?> E.g: Array ( [0] => Cat [1] => Dog ) Array ( [0] => Missy [1] => Fido ) Output: Array Functions
  • 71. 02/21/12 Open Source Programming <?php $a1=array(&quot;Dog&quot;,&quot;Dog&quot;,&quot;Cat&quot;); $a2=array(&quot;Pluto&quot;,&quot;Fido&quot;,&quot;Missy&quot;); array_multisort($a1,SORT_ASC,$a2,SORT_DESC); print_r($a1); print_r($a2); ?> E.g: Array ( [0] => Cat [1] => Dog [2] => Dog ) Array ( [0] => Missy [1] => Pluto [2] => Fido ) Output: Array Functions
  • 72. 02/21/12 The array_pop( array ) function deletes the last element of an array. Array_shift( array ) – removes an element at the beginning of the array Open Source Programming <?php $a=array(&quot;Dog&quot;,&quot;Cat&quot;,&quot;Horse&quot;); array_pop($a); print_r($a); array_shift($a); print_r($a); ?> E.g: Array ( [0] => Dog [1] => Cat ) Array ( [0] => Cat ) Output: Array Functions
  • 73. 02/21/12 The array_push( array,value1,value2... ) function inserts one or more elements to the end of an array. Array_unshift( array ) – inserts an element at the beginning of the array Open Source Programming <?php $a=array(&quot;Dog&quot;,&quot;Cat&quot;); array_push($a,&quot;Horse&quot;,&quot;Bird&quot;); Print_r($a); array_unshift($a, “fish”); print_r($a); ?> E.g: Array ( [0] => Dog [1] => Cat [2] => Horse [3] => Bird ) Array ( [0] => fish [1]=>Dog [2] => Cat [3] => Horse [4] => Bird ) Output: Array Functions
  • 74. 02/21/12 array_push — Push one or more elements onto the end of array Open Source Programming <?php $a=array(&quot;a&quot;=>&quot;Dog&quot;,&quot;b&quot;=>&quot;Cat&quot;); array_push($a,&quot;Horse&quot;,&quot;Bird&quot;); print_r($a); ?> E.g: Array ( [a] => Dog [b] => Cat [0] => Horse [1] => Bird ) Output: Array Functions
  • 75. 02/21/12 array_rand( array,num ) function returns a random key from an array, or it returns an array of random keys if you specify that the function should return more than one key. Open Source Programming <?php $a=array(&quot;a&quot;=>&quot;Dog&quot;,&quot;b&quot;=>&quot;Cat&quot;,&quot;c&quot;=>&quot;Horse&quot;); print_r(array_rand($a,1)); ?> E.g: b Output: Array Functions
  • 76. 02/21/12 The array_reverse( array,preserve ) function returns an array in the reverse order. Open Source Programming <?php $a=array(&quot;a&quot;=>&quot;Dog&quot;,&quot;b&quot;=>&quot;Cat&quot;,&quot;c&quot;=>&quot;Horse&quot;); print_r(array_reverse($a)); ?> E.g: Array ( [c] => Horse [b] => Cat [a] => Dog ) Output: Array Functions
  • 77. 02/21/12 The array_search( value,array,strict ) function search an array for a value and returns the key. Open Source Programming <?php $a=array(&quot;a&quot;=>&quot;Dog&quot;,&quot;b&quot;=>&quot;Cat&quot;,&quot;c&quot;=>&quot;Horse&quot;); echo array_search(&quot;Dog&quot;,$a); ?> E.g: a Output: Array Functions
  • 78. 02/21/12 The array_slice( array, start, length, preserve ) function returns selected parts of an array. Open Source Programming <?php $a=array(0=>&quot;Dog&quot;,1=>&quot;Cat&quot;,2=>&quot;Horse&quot;,3=>&quot;Bird&quot;); print_r(array_slice($a,1,2)); ?> E.g: Array ( [0] => Cat [1] => Horse ) Output: Array Functions
  • 79. 02/21/12 The array_splice( array,start,length,array ) function removes selected elements from an array and replaces it with new elements. The function also returns an array with the removed elements. Open Source Programming <?php $a1=array(0=>&quot;Dog&quot;,1=>&quot;Cat&quot;,2=>&quot;Horse&quot;,3=>&quot;Bird&quot;); $a2=array(0=>&quot;Tiger&quot;,1=>&quot;Lion&quot;); array_splice($a1,0,2,$a2); print_r($a1); ?> E.g: Array ( [0] => Tiger [1] => Lion [2] => Horse [3] => Bird ) Output: Array Functions
  • 80. 02/21/12 The array_sum( array ) function returns the sum of all the values in the array. Open Source Programming <?php $a=array(0=>&quot;5&quot;,1=>&quot;15&quot;,2=>&quot;25&quot;); echo array_sum($a); ?> E.g: 45 Output: Array Functions
  • 81. 02/21/12 The arsort( array,sorttype ) function sorts an array by the values in reverse order. The values keep their original keys. Open Source Programming <?php $my_array = array(&quot;a&quot; => &quot;Dog&quot;, &quot;b&quot; => &quot;Cat&quot;, &quot;c&quot; => &quot;Horse&quot;); arsort($my_array); print_r($my_array); ?> E.g: Array ( [c] => Horse [a] => Dog [b] => Cat ) Output: Array Functions
  • 82. 02/21/12 The asort( array,sorttype ) function sorts an array by the values. The values keep their original keys Open Source Programming <?php $my_array = array(&quot;a&quot; => &quot;Dog&quot;, &quot;b&quot; => &quot;Cat&quot;, &quot;c&quot; => &quot;Horse&quot;); asort($my_array); print_r($my_array); ?> E.g: Array ( [b] => Cat [a] => Dog [c] => Horse ) Output: Array Functions
  • 83. 02/21/12 The krsort( array,sorttype ) function sorts an array by the keys in reverse order. The values keep their original keys. Open Source Programming <?php $my_array = array(&quot;a&quot; => &quot;Dog&quot;, &quot;b&quot; => &quot;Cat&quot;, &quot;c&quot; => &quot;Horse&quot;); krsort($my_array); print_r($my_array); ?> E.g: Array ( [c] => Horse [b] => Cat [a] => Dog ) Output: Array Functions
  • 84. 02/21/12 The ksort( array,sorttype ) function sorts an array by the keys. The values keep their original keys. Open Source Programming <?php $my_array = array(&quot;a&quot; => &quot;Dog&quot;, &quot;b&quot; => &quot;Cat&quot;, &quot;c&quot; => &quot;Horse&quot;); ksort($my_array); print_r($my_array); ?> E.g: Array ( [a] => Dog [b] => Cat [c] => Horse ) Output: Array Functions
  • 85. 02/21/12 The rsort( array ) function sorts an array by the values in reverse order. This function assigns new keys for the elements in the array. Existing keys will be removed. Open Source Programming <?php $my_array = array(&quot;a&quot; => &quot;Dog&quot;, &quot;b&quot; => &quot;Cat&quot;, &quot;c&quot; => &quot;Horse&quot;); rsort($my_array); print_r($my_array); ?> E.g: Array ( [0] => Horse [1] => Dog [2] => Cat ) Output: Array Functions
  • 86. 02/21/12 The sort( array ) function sorts an array by the values. This function assigns new keys for the elements in the array. Existing keys will be removed. Open Source Programming <?php $my_array = array(&quot;a&quot; => &quot;Dog&quot;, &quot;b&quot; => &quot;Cat&quot;, &quot;c&quot; => &quot;Horse&quot;); sort($my_array); print_r($my_array); ?> E.g: Array ( [0] => Cat [1] => Dog [2] => Horse ) Output: Array Functions
  • 87. 02/21/12 Open Source Programming CASE_LOWER CASE_UPPER SORT_ASC SORT_DESC SORT_REGULAR SORT_NUMERIC SORT_STRING SORT_LOCALE_STRING COUNT_NORMAL COUNT_RECURSIVE Array Constants EXTR_OVERWRITE EXTR_SKIP EXTR_PREFIX_SAME EXTR_PREFIX_ALL EXTR_PREFIX_INVALID EXTR_PREFIX_IF_EXISTS EXTR_IF_EXISTS EXTR_REFS
  • 88. 02/21/12 The count_chars( string,mode ) function returns how many times an ASCII character occurs within a string and returns the information. Open Source Programming String Functions Array ( [32] => 1 [33] => 1 [72] => 1 [87] => 1 [100] => 1 [101] => 1 [108] => 3 [111] => 2 [114] => 1 ) Output: <?php $str = &quot;Hello World!&quot;; print_r(count_chars($str,1)); ?> E.g:
  • 89. 02/21/12 The different return modes are: 0 - an array with the ASCII value as key and number of occurrences as value 1 - an array with the ASCII value as key and number of occurrences as value, only lists occurrences greater than zero 2 - an array with the ASCII value as key and number of occurrences as value, only lists occurrences equal to zero are listed 3 - a string with all the different characters used 4 - a string with all the unused characters Open Source Programming String Functions
  • 90. 02/21/12 The explode( separator, string, limit ) function breaks a string into an array. Open Source Programming Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day. ) Output: String Functions <?php $str = &quot;Hello world. It's a beautiful day.&quot;; print_r (explode(&quot; &quot;,$str)); ?> E.g:
  • 91. 02/21/12 The implode( separator,array ) & join ( separator,array ) function returns a string from the elements of an array. Open Source Programming Hello World! Beautiful Day! Output: String Functions <?php $arr = array('Hello','World!','Beautiful','Day!'); echo implode(&quot; &quot;,$arr); ?> E.g:
  • 92. 02/21/12 The ltrim( string,charlist ) & rtrim( string,charlist ) function will remove whitespaces or other predefined character from the left and right side of a string respectively. The soundex( string ) function calculates the soundex key of a string. The str_shuffle( string ) function randomly shuffles all the characters of a string. Open Source Programming String Functions
  • 93. 02/21/12 The strlen ( string ) function returns the length of a string. Open Source Programming Length = 12 Output: String Functions <?php $a= “hello World!”; echo “Length = “, strlen($a); ?> E.g:
  • 94. 02/21/12 The strrev ( string ) reverses the given string. Open Source Programming Reverse of “hello World!” is !dlroW olleh Output: String Functions <?php $a= “hello World!”; echo “Reverse of ”$a” is “, strrev($a); ?> E.g:
  • 95. 02/21/12 The strtoupper ( string ) converts to upper case character The strtolower ( string ) converts to lower case character Open Source Programming Upper of “hello World!” is HELLO WORLD! Lower of “hello World!” is hello world Output: String Functions <?php $a= “hello World!”; echo “Upper of ”$a” is “, strtoupper($a); echo “Lower of ”$a” is “, strtolower($a); ?> E.g:
  • 96. 02/21/12 The strpos ( string,exp ) returns the numerical position of first appearance of exp. The strrpos ( string,exp ) returns the numerical position of last appearance of exp. Open Source Programming 2 9 Output: String Functions <?php $a= “hello World!”; echo strpos($a,”l”),”<br>”; echo strrpos($a,”l”); ?> E.g:
  • 97. 02/21/12 The substr ( string,start,length ) function returns a sub string of the size “length” from the position of “start”. Open Source Programming lo Output: String Functions <?php $a= “hello World!”; echo substr($a,3,2); ?> E.g:
  • 98. 02/21/12 The substr_count ( string,substr ) counts number of times a sub string occurred in given string. Open Source Programming 2 Output: String Functions <?php $a= “hello Worlod!”; echo substr_count($a,”lo”); ?> E.g:
  • 99. 02/21/12 The substr_replace ( string,replacement,start,len ) replaces a portion of a string with a replacement string, beginning the substitution at a specified starting position and ending at a predefined replacement length. Open Source Programming heaaorld! Output: String Functions <?php $a= “hello World!”; echo substr_replace($a,”aa”,2,5); ?> E.g:
  • 100. 02/21/12 The ucfirst ( string ) converts the first character to upper case. The ucwords ( string ) converts the first character of each word to upper case. Open Source Programming Hello world! Hello World! Output: String Functions <?php $a= “hello world!”; echo ucfirst($a),”<br>”; echo ucwords($a); ?> E.g:
  • 101. 02/21/12 The parse_str ( string,arr ) function parses a query string into variables. Open Source Programming 23 Kai Jim Output: String Functions <?php parse_str(&quot;id=23&name=Kai%20Jim&quot;); echo $id.&quot;<br />&quot;; echo $name; ?> E.g:
  • 102. 02/21/12 The str_replace( find,replace,string,count ) function replaces some characters with some other characters in a string. Note: str_ireplace() – for case sensitive Open Source Programming Array ( [0] => blue [1] => pink [2] => green [3] => yellow ) Replacements: 1 Output: String Functions <?php $arr = array(&quot;blue&quot;,&quot;red&quot;,&quot;green&quot;,&quot;yellow&quot;); print_r(str_ireplace(&quot;RED&quot;,&quot;pink&quot;,$arr,$i)); echo &quot;Replacements: $i&quot;; ?> E.g:
  • 103. 02/21/12 The str_pad( string,length,padchar,padtype ) function pads a string to a new length. Open Source Programming .........Hello World Output: String Functions <?php $str = &quot;Hello World&quot;; echo str_pad($str,20,&quot;.&quot;,STR_PAD_LEFT); ?> E.g:
  • 104. 02/21/12 The str_split( string,length ) function splits a string into an array. Default length is 1. Open Source Programming Array ( [0] => Hel [1] => lo ) Output: String Functions <?php print_r(str_split(&quot;Hello&quot;,3)); ?> E.g:
  • 105. 02/21/12 The str_word_count( string ) function counts the number of words in a string. Open Source Programming 2 Output: String Functions <?php echo str_word_count(&quot;Hello world!&quot;); ?> E.g:
  • 106. 02/21/12 The strcasecmp( string1,string2 ) function compares two strings as case insensitive. Strcmp( string1,string2 ) compares as case sensitive. This function returns: 0 - if the two strings are equal < 0 - if string1 is less than string2 > 0 - if string1 is greater than string2 Open Source Programming 0 1 Output: String Functions <?php echo strcasecmp(&quot;Hello world!&quot;,&quot;HELLO WORLD!&quot;); echo “<br>”; echo strcmp(&quot;Hello world!&quot;,&quot;HELLO WORLD!&quot;); ?> E.g:
  • 107. 02/21/12 The stripos( string,find,start ) function returns the position of the first occurrence of a string inside another string. But doesn’t check for case sensitivity. In strpos it’s d same but check for case sensitivity. Strpos( string,find,start ) – case sensitive. If the string is not found, this function returns FALSE. Open Source Programming 6 Output: String Functions <?php echo stripos(&quot;Hello world!&quot;,&quot;WO&quot;); ?> E.g:
  • 108. 02/21/12 The stristr( string,exp ) function searches for the first occurrence of a string inside another string. This function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found Open Source Programming World! Have a nice day Output: String Functions <?php echo stristr(&quot;Hello world! Have a nice day&quot;,&quot;WORLD&quot;); ?> E.g:
  • 109. 02/21/12 Boolean Checkdate( day,month,year ) – used to check the given parameters are a valid date. Date( formatstring ) - The date() function returns a string representation of the current date and/or time formatted according to the instructions specified by a predefined format. Array Getdate ( int timestamp ) – returns array of datetime components Time() – returns time stamp Mktime( int h,int min, int sec, int day, int mnth, int yr ) – create a date and time Open Source Programming Date Functions
  • 110. 02/21/12
    • Some mathematical functions are listed:
    • sin(X) , cos(x), tan(x), asin(x), acos(x), atan(x), exp(x), log(x) – are trigonometric functions returns the respected values.
    • Ceil(x), floor(x) – used to round the numbers.
    • Abs(x) – returns absolute value for the given number.
    • Fmod(x,y) – returns the reminder of the division x /y.
    • Min(x,y) & Max(x,y) – returns the min and max values respectively.
    • Bindec(x), binoct(x),binhex(x) – converts the given binary value into decimal, octal and hexadecimal.
    • decbin(x), decoct(x), dechex(x) – converts the given decimal value into binary, octal and hexadecimal.
    • Base_convert(val,bastype, convtype) – converts the given value of basetype has to be converted to the convtype.
    Open Source Programming Math Functions
  • 111. 02/21/12
    • PHP offers a number of useful predefined variables that are accessible from anywhere within the executing script and provide you with a substantial amount of environment-specific information. You can sift through these variables to retrieve details about the current user session, the user’s operating environment, the local operating environment, and more.
    • They are:
        • $_SERVER
        • $_GET
        • $_POST
        • $_COOKIE
        • $_ENV
        • $_SESSION
        • $_FILES
    Open Source Programming Super Globals
  • 112. 02/21/12 $_SERVER: The $_SERVER super global contains information created by the Web server and offers a bevy of information regarding the server and client configuration and the current request environment. $_GET & $_POST: The $_GET and $_POST super globals are used to retrieve information from forms, like user input. $_COOKIE: The $_COOKIE super global stores information passed into the script through HTTP cookies Open Source Programming Super Globals
  • 113. 02/21/12 $_ENV: The $_ENV super global offers information regarding the PHP parser’s underlying server environment. $_SESSION: The $_SESSION super global contains information regarding all session variables $_FILES: The $_FILES super global contains information regarding data uploaded to the server via the POST method. This super global is a tad different from the others. Open Source Programming Super Globals
  • 114. 02/21/12
    • Creating Function:
    • Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
    • A piece of information passed to a function is called an argument.
    • Some functions require that more than one argument be passed to them. Arguments in these cases must be separated by commas:
    Open Source Programming User Defined Functions
  • 115. 02/21/12 Function E.g . <?php function generateFooter() { echo &quot;Copyright 2007 W. Jason Gilmore&quot;; } // Once defined, call the function likes so: generateFooter(); // function calling ?> Output: Copyright 2007 W. Jason Gilmore Open Source Programming User Defined Functions
  • 116. 02/21/12
    • Function Call:
    • A function call consists of the functionName(parameters) followed by parentheses .
    • A function can return a value using the return statement in conjunction with a value or object.
    • The function can be called as pass-by-value and pass-by-reference. By default, parameters are passed to functions by value.
    Open Source Programming User Defined Functions
  • 117. 02/21/12 Function with argument: <?php function writeName ($fname ) { echo $fname . &quot; Refsnes.<br />&quot;; } echo &quot;My name is &quot;; writeName(&quot; Kai Jim &quot;); echo &quot;My sister's name is &quot;; writeName(&quot; Hege &quot;); echo &quot;My brother's name is &quot;; writeName(&quot; Stale &quot;); ?> Open Source Programming Output: My name is Kai Jim Refsnes. My sister's name is Hege Refsnes. My brother's name is Stale Refsnes. User Defined Functions Formal Argument Fn. Call with Actual Argument
  • 118. 02/21/12 Function with arguments & return value: <?php function calculateAmt ($cost, $num ) { return ($cost * $num); } $price=10; $tot=5; Echo “ Total Amount “, calculateAmt($price, $tot); ?> Open Source Programming Output: Total Amount 50 User Defined Functions
  • 119. 02/21/12 Fn. Call by Ref.: <?php $cost = 20.99; $tax = 0.0575; function calculateCost( & $cost, $tax) { // Modify the $cost variable $cost = $cost + ($cost * $tax); // Perform some random change to the $tax variable. $tax += 4; } Echo “(bfr fn call) Cost is $cost <br>”; calculateCost($cost, $tax); Echo &quot;Tax is $tax*100 <br>&quot;; Echo “(aft fn call) Cost is: $cost”; ?> Open Source Programming Output: (bfr fn call) Cost is 20.99 Tax is 5.75 (aft fn call) Cost is: 22.20 User Defined Functions
  • 120. 02/21/12
    • Variable Scope:
    • Variables used inside a function are different from those used outside a function. The variables used inside the function are limited to the scope of the function.
    • The global statement declares a variable within a function as being the same as the variable that is used outside of the function.
    Open Source Programming User Defined Functions
  • 121. 02/21/12
    • Fn. With global variable:
    • <?php
        • function doublevalue( )
        • {
        • global $temp;
        • $temp = $temp * 2;
        • }
        • $temp = 5;
        • doublevalue( );
        • echo “Temp is: $temp&quot;;
    • ?>
    Open Source Programming Output: Temp is: 10 User Defined Functions
  • 122. 02/21/12 Open Source Programming PHP Form Handling < form method = POST action = form.php> Name: < input type =text name = fname > <br > Age: < input type = text name = age > < br > < input type = submit value = submit> </ form > HTML Code (Form.html) <?php $a = $_POST[' fname ']; $b = $_POST[' age ']; echo &quot;Welcome Mr./Ms. $a. You are $b years old &quot;; ?> PHP Code (form.php)
  • 123. 02/21/12 Open Source Programming HTML Browser Result PHP Form Handling
  • 124. 02/21/12 Open Source Programming Open Source Programming 1. The application attempts something. 2. If the attempt fails, the exception-handling feature throws an exception. 3. The assigned handler catches the exception and performs any necessary tasks. 4 . The exception-handling feature cleans up any resources consumed during the attempt. In PHP, Exception handling can be done by try …throw… catch . Exception Handling PHP
  • 125. 02/21/12 Open Source Programming Open Source Programming Exception handling syntax: Try { statement block; if goes wrong throw new exception(error message) } catch (exception $E) { echo $E.getMessage(); } Exception Handling PHP
  • 126. 02/21/12 <?php $a=10; $b=0; try { if ($b == 0) throw new exception(&quot;Divide by zero error&quot;); $c = $a/$b; echo &quot; $a&quot;,&quot; / &quot;,&quot;$b = &quot;, $a/$b; } catch(Exception $e) { echo $e->getMessage(); } ?> Open Source Programming Output: Divide by zero error Exception Handling in PHP
  • 127. 02/21/12 Open Source Programming
    • Basic class definitions begin with the keyword  class , followed by a class name , followed by a pair of curly braces which enclose the definitions of the properties and methods belonging to the class.
    • The class name can be any valid label which is a not a PHP  reserved word . A valid class name starts with a letter or underscore , followed by any number of letters, numbers, or underscores.
    • A class may contain its own  constants, variables  (called &quot;properties&quot;), and functions (called &quot;methods&quot;).
    • $this is a variable that indicates the current object. For instance, $this->a gets the data from the $a variable in the object.
    Object Oriented PHP - Class
  • 128. 02/21/12 Open Source Programming E.g.: <?php class  SimpleClass {     // property declaration      public  $var = ‘Hello';     // method declaration     public function displayVar()   {         echo $this->var;     } $obj1 = new SimpleClass(); $Obj1->displayVar(); } ?>
    • To create an instance of a class, the new keyword must be used.
    • Now any abc object that is created contains a property called $var with the value of “Hello”.
    • This property can be accessed and even be changed with the help of objects.
    • In this, the  ->  operator is used to access the properties or methods of the object.
    Object Oriented PHP - Object
  • 129. 02/21/12 Open Source Programming
    • A class can inherit the methods and properties of another class by using the keyword extends in the class declaration. It is not possible to extend multiple classes; a class can only inherit from one base class .
    • The inherited methods and properties can be overridden by redeclaring them with the same name defined in the parent class.
    • However, if the parent class has defined a method as final, that method may not be overridden.
    • It is possible to access the overridden methods or static properties by referencing them with parent.
    • Abstract methods are special in that they are declared only within a parent class but are implemented in child classes. Only classes declared as abstract can contain abstract methods.
    Inheritance
  • 130. 02/21/12 <?php class ExtendClass extends SimpleClass {     // Redefine the parent method     function displayVar()     {         echo &quot;Extending classn&quot;;         parent::displayVar();     } } $extended = new ExtendClass(); $extended->displayVar(); ?> Open Source Programming Extending class Hello E.G . Output: Inheritance
  • 131. 02/21/12 Open Source Programming
    • A constructor is defined as a block of code that automatically executes at the time of object instantiation.
    • PHP recognizes constructors by the name __construct() .
    • PHP does not automatically call the parent constructor; you must call it explicitly using the parent keyword.
    • Destructors are created like any other method but must be titled __destruct().
    Constructors & Destructors
  • 132. 02/21/12 E.g . <?php class BaseClass {    function __construct() {        print &quot;In BaseClass constructorn&quot;;    } } class SubClass extends BaseClass {    function __construct() {        parent::__construct();        print &quot;In SubClass constructorn&quot;;    } } $obj = new BaseClass(); $obj = new SubClass(); ?> Output: In BaseClass constructor In BaseClass constructor In SubClass constructor Open Source Programming Constructors & Destructors
  • 133. 02/21/12 Open Source Programming
    • Overloading in PHP provides means to dynamically &quot;create&quot; properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types.
    • The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope.
    • All overloading methods must be defined as public .
    Overloading
  • 134. 02/21/12 E.g. <?php class foo { public function printItem($string) { echo 'Foo: ' . $string . PHP_EOL; } public function printPHP() { echo 'PHP is great.' . PHP_EOL; } } class c extends foo { public function printItem($string) { echo &quot;Extending from FOOn&quot;; parent::printItem(&quot;this is gudn&quot;); } } $foo = new foo(); $c = new c(); $foo->printItem('baz'); ' $foo->printPHP(); $c->printItem('from C'); $c->printPHP(); ?> Foo: baz PHP is great. Extending from FOO Foo: this is gud PHP is great. Output: Open Source Programming Overloading
  • 135. 02/21/12 Open Source Programming Can insert the content of one PHP file into another PHP file before the server executes it, with the include() or require() function. The include() function takes all the content in a specified file and includes it in the current file. Files are included based on the file path given . The two functions are identical in every way, except how they handle errors: 1. include() generates a warning, but the script will continue execution 2. require() generates a fatal error, and the script will stop E.g. <?php include(&quot;wrongFile.php&quot;); echo &quot;Hello World!&quot;; ?> Output: Error Message Hello World Include & Require
  • 136. 02/21/12 Open Source Programming E.g. <?php require(&quot;wrongFile.php&quot;); echo &quot;Hello World!&quot;; ?> Output: Error Message Include & Require
  • 137. 02/21/12 Open Source Programming
    • Organizing related data into entities commonly referred to as files .
    • To create a file we use fopen Function. The  fopen  function needs two important pieces of information to operate correctly.
    • First parameter requires file name to open.
    • Second parameter indicates the mode in which the file has to be opened.
    E.g. <?php $ourFileName = &quot;testFile.txt&quot;; $ourFileHandle = fopen($ourFileName, ‘w‘) ?> Files
  • 138. 02/21/12 Open Source Programming
    • Some modes are
    Files
  • 139. 02/21/12 File closing can be done by fclose(fp); E.g. <?php $ourFileName = &quot;testFile.txt&quot;; $ourFileHandle = fopen($ourFileName, ‘w') or die(&quot;can't open file&quot;); fclose($ourFileHandle); ?> Files
  • 140. 02/21/12 Open Source Programming 1) fopen(filename,mode) - used for opening a file with specific mode. 2) fclose(fp) - used to close the file. (fp -> filepointer) 3) feof(fp) - used to find the End of File 4) fgetc(fp) - Gets character from file pointer 5) fgets(fp) - Gets a line from file pointer 6) fread(fp,size) - Binary-safe file read 7) fscanf(fp,format) - Parses input from a file according to a format 8) fwrite(fp,string) - Binary-safe file write 9) file_put_contents(fp,string) - Write a string to a file 10) file_get_contents(fp) - Reads entire file into a string 11) file(fp) - Reads entire file into an array 12) file_exists(fp) - Checks whether a file or directory exists Some File Functions
  • 141. 02/21/12 Open Source Programming 13) is_readable(fp) - Tells whether the filename is readable 14) is_writable(fp) - Tells whether the filename is writable 15) is_file(path) - Tells whether the filename is a regular file 16) is_dir(path) - Tells whether the filename is a directory 17) is_link(path) - Tells whether the filename is a symbolic link 18) readlink(path) - Returns the target of a symbolic link 19) readdir - Read entry from directory handle 20) glob - Find pathnames matching a pattern 21) filesize(fp) - Gets file size 22) filetype(fp) - Gets file type 23) fprintf(fp,format) - Write a formatted string to a stream 24) fstat(fp) - Gets information about a file using an open file pointer Some File Functions
  • 142. 02/21/12 Open Source Programming <?php $myFile = &quot;testFile.txt&quot;; $fh = fopen($myFile, 'w') or die(&quot;can't open file&quot;); $stringData = &quot;Bobby Boppern&quot;; fwrite($fh, $stringData); $stringData = &quot;Tracy Tannern&quot;; fwrite($fh,$stringData); fclose($fh); ?> E.g. <?php $ourFileName = &quot;testFile.txt&quot;; $ourFileHandle = fopen($ourFileName, ‘w') or die(&quot;can't open file&quot;); ?> File Write
  • 143. 02/21/12 Open Source Programming E.g. <?php // get contents of a file into a string $filename = &quot;testfile.txt&quot;; $handle = fopen($filename, &quot;r&quot;); //$contents = fread($handle, filesize($filename)); while (!feof($handle)) echo fgetc($handle),&quot;<br>&quot;; //print_r(&quot;$contents&quot;); fclose($handle); ?> Output: Bobby Bopper Tracy Tanner File Read
  • 144. 02/21/12 Open Source Programming E.g. <?php $myFile = &quot;testFile.txt&quot;; $fh = fopen($myFile, 'a+') or die(&quot;can't open file&quot;); $stringData = &quot;new1&quot;; fwrite($fh, $stringData); $stringData = &quot;new2n&quot;; fwrite($fh, $stringData); fclose($fh); ?> Output: Bobby Bopper Tracy Tannernew1new2 File Append
  • 145. 02/21/12
  • 146. 02/21/12