PHP Workshop Notes


Published on

Put on by USC's Upsilon Pi Epsilon as part of Wonderful World of Web2.0 Workshop Series.

Published in: Technology
  • IT Pathshala, Noida-Sector-63, offers Guaranteed JOB with PHP Training Programs.


    CALL US - +91-9871350999 or +91-8800394970

    Technology Centre Address: H-110, Sector-63, Noida (UP)

    Program Takeaways:

    - GUARANTEED JOB in your technology domain
    - Live Major Project development
    - Experience Letter on Training Completion
    - Certification on Training Completion
    - Project Source Code available in CD

    Program Highlights:

    - 100% Guaranteed Job – Don’t pay us if you don’t get a JOB!
    - Industry Ready Courses
    - LIVE International Project Experience
    - Exposure to corporate culture at our IT services facility
    - Networking & Interaction with Industry Experts
    - Training Provided by a reputed IT Company
    - Live Project Source Code made available in CD
    - Technology certification and experience letter at program completion
    - Online Study Content

    CALL US - +91-9871350999 or +91-8800394970

    Our website:
    Are you sure you want to  Yes  No
    Your message goes here
  • nice points
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

PHP Workshop Notes

  1. 1. PHP A scripting language design to produce HTML pages
  2. 2. PHP Introduction <ul><li>PHP serves the same purpose as Java Server Pages (JSP) and Active Server Pages (ASP) </li></ul><ul><li>All are server-side languages “parsed” by a web server </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>
  3. 3. 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>Web server parses the PHP file, producing HTML </li></ul><ul><li>Now can be used to output XML, image, PDF, just by setting content-type </li></ul>
  4. 4. 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>
  5. 5. 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>
  6. 6. 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>
  7. 7. 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
  8. 8. 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, $_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>
  9. 9. 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>
  10. 10. 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>
  11. 11. 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>
  12. 12. 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>
  13. 13. Current Date/Time <ul><li>Use date function to get the current date. </li></ul><ul><li>Takes a format string to provide the date in a format you want. See . </li></ul><ul><li>Use time function to get the current time. </li></ul><ul><li>Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). </li></ul>
  14. 14. 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>
  15. 15. 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>
  16. 16. 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>
  17. 17. 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.
  18. 18. 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>
  19. 19. 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>
  20. 20. 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>
  21. 21. 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>
  22. 22. 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>
  23. 23. 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>
  24. 24. 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>
  25. 25. 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>
  26. 26. Selecting a Database <ul><ul><li>mysql_select_db( ‘upeworkshop’ ); </li></ul></ul>
  27. 27. 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>
  28. 28. 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>
  29. 29. Closing a Connection <ul><ul><li>mysql_close( $con ); </li></ul></ul>