PHP and MySQL

562 views

Published on

These are some basics you need to start with PHP, Any queries, get in touch!!

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

  • Be the first to like this

No Downloads
Views
Total views
562
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

PHP and MySQL

  1. 1. By Sanketkumar Biswas 1
  2. 2. Client Server Arch. 2
  3. 3. Server Side ScriptingGenerates dynamic web pages.Practically invisible to end - user. 3
  4. 4. Web ApplicationAn application that runs within a web browser.And can be accessed over a network. 4
  5. 5. What is PHP ?It is a server side scripting language used forweb development.Created by Rasmus Lerdorf in 1995 5
  6. 6. Why PHP ?Open SourceVery good set of built in functionsVery good database supportGreat support communityMany frameworks 6
  7. 7. Getting started with PHP<?phpecho "Hello World";?> 7
  8. 8. Including filesinclude(/filepath/filename/)include_once(/filepath/filename/)require(/filepath/filename/)require_once(/filepath/filename/) 8
  9. 9. PHP SyntaxPHP is programmer centric.HTML and PHP are not same.PHP is white space insensitive.PHP is sometimes case sensitive.Termination by semicolon (;)Braces define blocks. 9
  10. 10. Programmer CentricFocuses on convenience for programmer.Does not focus much on correctness. 10
  11. 11. HTML Vs. PHPHTML is client side.PHP is server side. 11
  12. 12. White space Insensitive<?php$val = 2 + 2; //single spaces$val = 2 + 2; //spaces and tabs$val =2+ 2; //multiple lines?> 12
  13. 13. Case Sensitive<?php$val = 20;echo "variable value is $val";echo "variable value is $VaL";?> 13
  14. 14. Case Sensitive<?phpHELLO();function hello() {EcHo "Hello!!";}?> 14
  15. 15. Termination<?phpecho "Hello World";?> 15
  16. 16. Braces define block<?phpif (1 == 1) echo "Hello World";if (1 == 1) { echo "Hello "; echo "World";}?> 16
  17. 17. Commenting<?php// this is a single line comment.# this is also a single line comment./* this is a multi line comment. */?> 17
  18. 18. VariablesVariables are denoted with a leading ($) sign.Value of variable is the most recent assignment.Variable is assigned using (=) operator, to its leftis the variable and to right is the expression tobe evaluated. 18
  19. 19. Variables can, but do not need to be declaredbefore assignment.Variables have no type other than the type of itscurrent value.Variables should always start with an alphabet.Numerical values for a variable can be INT,HEX, OCT, FLOAT and e. 19
  20. 20. In variables for text we can use () as escapecharacter.Boolean values is not case sensitive, TRUE,TRuE, true, trUE, etc.Prefix for HEX is "0x" and for OCT is "0".Range of INT varies with respect to processorbits, i.e. different for 32 and 64 bit processor. 20
  21. 21. $x = Hello; $x = "Hello"; $x = <<<varHellovar; Single quotes display as it is, whereas double quotes and heredoc do interpretation. 21
  22. 22. Creating a variable$x = 3; Changing value$x = $x + 5; Changing value as well as type$x = $x + 0.5; 22
  23. 23. Global Variables:$x = "HELLO";function test() { global $x; return $x;}echo test(); Super Global Variables: $_POST, $_GET, etc. 23
  24. 24. Outputtingecho "Hello World n Welcome to PHP";echo "Hello World <br> Welcome to PHP";echo "$var1 n $var2"; 24
  25. 25. ArraysCreating arrays$array = array(1, -2, cat, TRUE);$array[] = 2.6;$array[5] = 012;$array[] = 0xA;$array[7] = 5e2; 25
  26. 26. Creating arrays with index$array = array(0 => 1,1 => -2,2 => cat,3 => TRUE); 26
  27. 27. Creating arrays with string index$array = array(INT => 1,NINT => -2,STRING => cat,BOOLEAN => TRUE); 27
  28. 28. Creating multi dimensionsl arrays$array = array( INT => array( POS => 1, NEG => -2 )); 28
  29. 29. Deleting arraysunset($array[2]);Unset is not the same as,$array[2] = ""; 29
  30. 30. Iterating arrays$array = array(INT => 1, NINT => -2, STRING => cat);foreach($array as $element) { echo $element."<br>";} 30
  31. 31. OperatorsArithmetic > Comparison > LogicalArithmetic: +, -, *, /, %Comparison: ==, ===, !=, !==, ......Logical: and, or, !, .......break: used to stop loops.continue: used to stop iteration. 31
  32. 32. Arithmetic Operators 32
  33. 33. Comparison Operators 33
  34. 34. Logical Operators 34
  35. 35. ConstantsDefining a constantdefine("CONSTANT_NAME","VALUE"); 35
  36. 36. Type Casting$variable = (TYPE) VALUE;$x = 5.5;$y = (INT) $x; // $y = 5 36
  37. 37. FunctionsDeclaration:function NAME(ARG1, ARG2=DEFAULT);Eg:$x = "Hello";function app($x,$s="World") { return $x . $s;}$a = app($x); // HelloWorld$b = app($y,"India"); // HelloIndia 37
  38. 38. Some Important Functionsstrlen(): gives length of string.strcmp(x,y): case sensitive string compare.strcasecmp(x,y): case in sensitive stringcompare.strncmp(x,y,n): checks first n characters. 38
  39. 39. strtoupper(x): converts to uppercase.strtolower(x): converts to lowercase.trim(x): removes white space.substr(x,m,n): creates substring of x from m to ncharacters.ord(x): gives int value for character. 39
  40. 40. count(x): gives no. Of elements in array.isset(x): checks if has value.sort(x): sorts array wrt values.ksort(x): sorts array wrt keys.array_merge(x,y): merge arrays to a new one. 40
  41. 41. array_slice(x,m,n): creates a sub array of x fromm key to the next n keys. 41
  42. 42. Branching if... else...if ($a > $b) { $c = $a - $b; echo $c;} else { $c = $b - $a; echo $c;} 42
  43. 43. switchswitch($a) { case 1: echo "one"; break; case 2: echo "two"; break; case 3: echo "three"; break; default: echo "no number"; break;} 43
  44. 44. Looping while$count = 1;while ($count <= 10) { echo "Count is $count n"; $count++;} 44
  45. 45. do - while$count = 45;do { echo "Count is $count n"; $count++;} while ($count <= 10); 45
  46. 46. for$limit = 5;for($count=0; $count<$limit; $count++){ echo "Count is $count n";} 46
  47. 47. PHP and FormsHTTP is a stateless protocolData is sent using GET POST 47
  48. 48. To collect data for PHP processing we use superglobal variables. $_GET $_POST 48
  49. 49. PHP and MySQLInitialise a database connectionmysql_connect(host,username,pass);mysql_select_db(dbname);Firing a querymysql_query(Query); 49
  50. 50. <?phpmysql_connect(localhost,root,);mysql_select_db(demo);$res = mysql_query(SELECT * FROM table);while ($row = mysql_fetch_assoc($res)){ echo $row[name]; }?> 50
  51. 51. SessionPHP Session variable is super global.It has values of a single user across many pagesin an application.It is located server side. 51
  52. 52. Starting Session session_start();Session Variable $_SESSION[username]Destroying Session session_destroy(); 52
  53. 53. CookieCookie is also super global variable.It is used to store information about user and itsvisit on users computer.It is located client side. 53
  54. 54. Setting cookiesetcookie(name,rohit,time()+(3600)); Cookie variable $_COOKIE[name] 54
  55. 55. Thank YouFollow me on:Facebook: http://fb.com/sanketkumarbiswas/Twitter: @sankeybiswasLinkedIn: http://linkedin.com/in/sanketkumarbiswas/Web: http://www.sankeybiswas.com/Cell: +91 9820 477 377 55

×