Total PHP


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Total PHP

  1. 1. Introduction to Open source PHP & Joomla Asst. Prof Roshni M. Raval
  2. 3. What You Should Already Know <ul><li>HTML/XHTML </li></ul><ul><li>JavaScript </li></ul><ul><li>CSS </li></ul>
  3. 4. HTML For describing web pages
  4. 5. What is HTML? <ul><li>HTML stands for H yper T ext M arkup L anguage </li></ul><ul><li>For describing web pages. </li></ul><ul><li>Not a programming language, it is a markup language </li></ul><ul><li>A markup language is a set of markup tags </li></ul><ul><li>HTML follows w3c rules. </li></ul><ul><li>Div based code. </li></ul>
  5. 6. Java Script Is a Library for interactive UI and client side validation
  6. 7. Why JavaScript? <ul><li>JavaScript is a scripting language </li></ul><ul><li>JavaScript was designed to add interactivity to HTML pages </li></ul><ul><li>Lightweight programming language </li></ul><ul><li>Embedded directly into HTML pages </li></ul><ul><li>Interpreted language (means that scripts execute without preliminary compilation) </li></ul><ul><li>Everyone can use JavaScript without purchasing a license </li></ul><ul><li>jQuery </li></ul>
  7. 8. ..continue <ul><li>JavaScript is the most popular scripting language on the internet, and works in all major browsers, such as. </li></ul><ul><li>Internet Explorer </li></ul><ul><li>Firefox, </li></ul><ul><li>Chrome, </li></ul><ul><li>Opera, </li></ul><ul><li>and Safari. </li></ul>
  8. 9. What JavaScript can do? <ul><li>JavaScript gives HTML designers a programming tool. </li></ul><ul><li>Can put dynamic text into an HTML page. </li></ul><ul><li>Can react to events. </li></ul><ul><li>Can read and write HTML elements. </li></ul><ul><li>Can be used to validate data. </li></ul><ul><li>Can be used to create cookies. </li></ul>
  9. 10. CSS CSS stands for C ascading S tyle S heets
  10. 11. What is CSS? <ul><li>Styles define how to display HTML elements. </li></ul><ul><li>External Style Sheets can save a lot of work. </li></ul><ul><li>External Style Sheets are stored in CSS files. </li></ul>
  11. 12. PHP H ypertext P re p rocessor
  12. 13. PHP Introduction <ul><li>PHP is a server-side scripting language. </li></ul><ul><li>PHP is a powerful tool for making dynamic and interactive Web pages. </li></ul><ul><li>PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft's ASP. </li></ul><ul><li>Script execution results are sent to a browser as an HTML page </li></ul><ul><li>PHP is a type-less language </li></ul>
  13. 14. PHP Structure <ul><li>Tradition: Start with an HTML file </li></ul><ul><li>Add special tags to separate PHP code from HTML statements </li></ul><ul><li>(i.e. <?php </li></ul><ul><li>…… </li></ul><ul><li>?>) </li></ul><ul><li>Web server parses the PHP file, producing HTML </li></ul>
  14. 15. Example: myfirst.php <ul><ul><li><html> </li></ul></ul><ul><ul><li><body> </li></ul></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>//A comment </li></ul></ul><ul><ul><li>/*Or comment like this*/ </li></ul></ul><ul><ul><li>print(&quot;<b>Hello world</b>&quot;); </li></ul></ul><ul><ul><li>$v = 5; </li></ul></ul><ul><ul><li>print(&quot;<p>Hello again &quot; . $v ); </li></ul></ul><ul><ul><li>print(&quot;<p>Hello a third time $v&quot;); </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><ul><li></body> </li></ul></ul><ul><ul><li></html> </li></ul></ul>
  15. 16. Variables <ul><li>All variables start with a dollar sign, $ </li></ul><ul><ul><li>$u = 5; </li></ul></ul><ul><ul><li>$v = “hello”; </li></ul></ul><ul><ul><li>$w = 1.22; </li></ul></ul><ul><ul><li>$x = $u + $v; //arithmetic with + - ONLY </li></ul></ul><ul><ul><li>$y = $v . $v; //concatenation with period operator </li></ul></ul><ul><ul><li>$x = $u . $u; //produces 55, not 10 </li></ul></ul>
  16. 17. Printing <ul><ul><li>$u = 5; </li></ul></ul><ul><ul><li>print( “5 hello” ); //print anything in quotes </li></ul></ul><ul><ul><li>print( $u . “hello” ); //prints 5hello </li></ul></ul><ul><ul><li>print( “$u Hello” ); //prints 5 Hello </li></ul></ul><ul><ul><li>echo ‘Hello’; </li></ul></ul><ul><ul><li>echo ‘Hello’ . $u; </li></ul></ul>
  17. 18. String-Related Functions <ul><ul><li>$v = “hello”; </li></ul></ul><ul><ul><li>strlen( $v); //returns 5 </li></ul></ul><ul><ul><li>trim( $v); //trims any spaces on either side of a string </li></ul></ul><ul><ul><li>$x = strtolower ($v); //$x has hello </li></ul></ul><ul><ul><li>$x = strtoupper($v); //$x has HELLO </li></ul></ul><ul><ul><li>$str = “abcdef”; </li></ul></ul><ul><ul><li>$a = substr( $str, 3, 3 ); </li></ul></ul># of characters to copy Start position, zero indexed Source string “ def” Can be negative to start from right side
  18. 19. Getting HTML Form Data <ul><li>There are 3 ways to get form data in PHP </li></ul><ul><ul><li>Global variables – this is a bad way because of security problems. PHP creates a variable with a name matching the form field name in the source HTML. </li></ul></ul><ul><ul><li>POST variable associative array </li></ul></ul><ul><ul><ul><li>Prior to version 4.1, $HTTP_POST_VARS </li></ul></ul></ul><ul><ul><ul><li>4.1 and after, </li></ul></ul></ul><ul><ul><ul><li>$_POST </li></ul></ul></ul><ul><ul><li>GET variable associative array </li></ul></ul><ul><ul><ul><li>Same as POST, but use GET </li></ul></ul></ul>
  19. 20. Examples <ul><li>Global variables (HTML has field with name ‘abc’) </li></ul><ul><ul><ul><li>print ($abc); </li></ul></ul></ul><ul><li>POST </li></ul><ul><ul><ul><li>print($_POST(‘abc’)); //4.1 and after </li></ul></ul></ul><ul><li>GET </li></ul><ul><ul><ul><li>print($_GET(‘abc’)); //4.1 and after </li></ul></ul></ul>
  20. 21. Comparing Strings <ul><li>strcmp( $a, $b ); //works like C function </li></ul><ul><li>Returns: </li></ul><ul><li> – 1 if first string less than second string </li></ul><ul><li> 0 if the same </li></ul><ul><li>1 if first string greater than second string </li></ul><ul><li>It is case sensitive </li></ul>
  21. 22. PHP Syntax Similarities <ul><li>Has a switch statement </li></ul><ul><li>for loop is the same, but uses PHP variable syntax </li></ul><ul><ul><li>for ($i=0; $i < 10; $i++ ){ …. } </li></ul></ul><ul><li>while and if are also what you’d expect </li></ul><ul><li>Standard logical operators: ==, &&, <, > … </li></ul>
  22. 23. Other Useful Functions <ul><li>round ($a); //rounds a number </li></ul><ul><li>is_numeric($v); //returns true/false </li></ul><ul><li>rand($start, $end); //Produces int rand </li></ul>
  23. 24. Creating Functions <ul><ul><li>function myfunc( $a, $b, $c ) { </li></ul></ul><ul><ul><li>//this is my code </li></ul></ul><ul><ul><li>return $x; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  24. 25. External PHP Files <ul><li>Can use require or include </li></ul><ul><ul><li>Require will produce a fatal error if the file cannot be found </li></ul></ul><ul><ul><li>Include will just ignore a missing script file </li></ul></ul><ul><ul><li>require(‘my.php’); </li></ul></ul><ul><ul><li>include(‘my.php’); </li></ul></ul><ul><ul><li>The files can contain PHP and/or HTML </li></ul></ul>
  25. 26. Arrays <ul><li>Creating an array </li></ul><ul><ul><li>$a = array(1,2,3,4,5); </li></ul></ul><ul><li>Accessing array elements </li></ul><ul><ul><li>$v = $a[2]; </li></ul></ul><ul><ul><li>$a[2] = 5; </li></ul></ul><ul><ul><li>$a[] = 1; $a[] = 2; $a[] = 3; //appends to array </li></ul></ul>
  26. 27. Iterating Over Arrays <ul><ul><li>for ($i=0; $i<count($a); $i++ ) { </li></ul></ul><ul><ul><li>print ($a[i]); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>foreach( $a as $item ) { </li></ul></ul><ul><ul><li>print( “<p>$item”); </li></ul></ul><ul><ul><li>} </li></ul></ul>Array variable Local variable. Set to next array element each iteration.
  27. 28. Other Array Functions <ul><ul><li>$m = max($a); //returns max value in array </li></ul></ul><ul><ul><li>$m = min($a); //returns min value in array </li></ul></ul><ul><ul><li>$s = array_sum($a); //returns sum or array values </li></ul></ul><ul><ul><li>sort($a); //sorts the items in the array. Changes </li></ul></ul><ul><ul><li>//the array itself </li></ul></ul><ul><ul><li>Optional second argument is “sort flags” to control the sort. </li></ul></ul>
  28. 29. Associative Arrays <ul><li>Arrays containing key/value pairs </li></ul><ul><ul><li>$s = array( ‘a’=>’alpha’, ‘b’=>’beta’, … ); </li></ul></ul><ul><ul><li>print ( $s[‘b’] ); </li></ul></ul><ul><li>The parameter to the left of => is the key. </li></ul><ul><li>The right parameter is the value. </li></ul>
  29. 30. SQL – Structured Query Language <ul><li>A language for accessing relational databases </li></ul><ul><li>Relational databases have tables </li></ul><ul><li>Tables have fields and contain rows of data </li></ul>
  30. 31. SQL Syntax – SELECT <ul><li>Used for retrieving data from a database </li></ul><ul><ul><li>SELECT [fields] FROM [tables] WHERE [expr] </li></ul></ul><ul><ul><li>Examples </li></ul></ul><ul><ul><li>select * from users </li></ul></ul><ul><ul><li>select abc, def from mytable where ghi=5 </li></ul></ul>
  31. 32. SQL Syntax – INSERT <ul><li>Used to insert new data in a table </li></ul><ul><ul><li>INSERT INTO [table] [field names] VALUES [values] </li></ul></ul><ul><ul><li>Examples </li></ul></ul><ul><ul><li>insert into users (abc,def,ghi) values (‘111’,22,’cc) </li></ul></ul><ul><ul><li>insert into xyz values (1,2,3,4,5) </li></ul></ul>
  32. 33. SQL Syntax – UPDATE <ul><li>Updating one or more values in existing rows in a table </li></ul><ul><ul><li>UPDATE [table] SET [name=value pairs] WHERE [expression] </li></ul></ul><ul><ul><li>Examples </li></ul></ul><ul><ul><li>update mytable set a=‘aaa’, b=55 where c=11 </li></ul></ul>
  33. 34. PHP and MySQL Database <ul><li>5 steps </li></ul><ul><ul><li>Connect to the Mysql DBMS </li></ul></ul><ul><ul><li>Pick a database </li></ul></ul><ul><ul><li>Execute an SQL statement </li></ul></ul><ul><ul><li>If the SQL was a ‘select’, retrieve the data </li></ul></ul><ul><ul><li>Close the connection </li></ul></ul>
  34. 35. Connecting to MySQL DBMS <ul><ul><li>$con = mysql_connect( /* 3 arguments */ ); </li></ul></ul><ul><ul><li>Your MySQL DBMS server process network location </li></ul></ul><ul><ul><li>Your MySQL user ID </li></ul></ul><ul><ul><li>Your MySQL user password </li></ul></ul><ul><ul><li>For tonight only, </li></ul></ul><ul><ul><li>mysql_connect(‘’,’upeworkshop’,’upeworkshop’); </li></ul></ul>
  35. 36. Selecting a Database <ul><ul><li>mysql_select_db( ‘upeworkshop’ ); </li></ul></ul>
  36. 37. Executing an SQL Statement <ul><ul><li>mysql_query( /*SQL statement*/ ); </li></ul></ul><ul><ul><li>Proper form for any SQL not a Select </li></ul></ul><ul><ul><li>if ( !mysql_query ( “update a ….” ); ) { </li></ul></ul><ul><ul><li>echo mysql_error(); //for easy debugging, not for final user-oriented website </li></ul></ul><ul><ul><li>} //returns true/false if it worked, or not </li></ul></ul>
  37. 38. For Select SQL Statements <ul><ul><li>$r = mysql_query( ‘select * from abc’ ); </li></ul></ul><ul><ul><li>while ( $row = mysql_fetch_row( $r ) ) { … } </li></ul></ul><ul><ul><li>$row contains a row of data </li></ul></ul><ul><ul><li>returns false when no more rows available </li></ul></ul><ul><ul><li>Iterating through the fields in the row </li></ul></ul><ul><ul><li>foreach ( $row as $item ) { … } </li></ul></ul><ul><ul><li>OR access the fields using index position (zero indexed) </li></ul></ul><ul><ul><li>OR put results in an associative array – less error prone </li></ul></ul><ul><ul><li>while ($row = mysql_fetch_assoc($r)){ </li></ul></ul><ul><ul><li>echo $row[‘firstname’]; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  38. 39. Closing a Connection <ul><ul><li>mysql_close( $con ); </li></ul></ul>
  39. 40. What is other than core PHP ?
  40. 41. What is CMS? <ul><li>A content management system is software that keeps track of every piece of content on your Web site, much like your local public library keeps track of books and stores them. </li></ul><ul><li>Content can be simple text, photos, music, video, documents, or just about anything you can think of. </li></ul>
  41. 42. Famous CMS
  42. 43. Joomla <ul><li>Joomla is an free and open source content management system (CMS), which enables you to build Web sites and powerful online applications. </li></ul><ul><li>Extensible </li></ul><ul><li>Written in PHP </li></ul><ul><li>It uses OOP techniques </li></ul><ul><li>Based on MVC framework. </li></ul>
  43. 44. Real world examples <ul><li>Corporate Web sites or portals </li></ul><ul><li>Online magazines, newspapers, and publications </li></ul><ul><li>E-commerce and online reservations </li></ul><ul><li>Government applications </li></ul><ul><li>Small business Web sites </li></ul><ul><li>Non-profit and organizational Web sites </li></ul><ul><li>Community-based portals </li></ul>
  44. 45. Servers <ul><li>WAMP </li></ul><ul><li>LAMP </li></ul><ul><li>MAMP </li></ul><ul><li>XAMPP </li></ul>
  45. 46. Source <ul><li>HTML/CSS/JS : </li></ul><ul><li>PHP : </li></ul><ul><li>MySQL : </li></ul><ul><li>Server : </li></ul><ul><li>Joomla : </li></ul>