1 / 64

XAMPP: Cross – Apache, MySQL , Php , Perl + FileZilla , Tomcat NetBeans : IDE

PHP Installation. XAMPP: Cross – Apache, MySQL , Php , Perl + FileZilla , Tomcat NetBeans : IDE. PHP - XAMPP. PHP – NetBeans IDE . Netbeans - default browser. Netbeans – Starts a project. PHP – Starting a project. Netbeans – Setting a project. Basic PHP Syntax.

dai
Download Presentation

XAMPP: Cross – Apache, MySQL , Php , Perl + FileZilla , Tomcat NetBeans : IDE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP Installation XAMPP: Cross – Apache, MySQL, Php, Perl + FileZilla, Tomcat NetBeans: IDE

  2. PHP - XAMPP

  3. PHP – NetBeans IDE

  4. Netbeans- default browser

  5. Netbeans – Starts a project

  6. PHP – Starting a project

  7. Netbeans – Setting a project

  8. Basic PHP Syntax • <html><body><?phpecho "Hello World";?></body></html>

  9. Comment in Php • <html><body><?php//This is a comment/*This isa commentblock*/?></body></html>

  10. PHP Variables • <?php$txt="Hello World!";$x=16; • echo $txt;?> • --------------- • Hello World!

  11. PHP Operators

  12. if • <?php$d=date("D");if ($d=="Fri") echo "Have a nice weekend!";?> • <?php$d=date("D");if ($d=="Fri")  echo "Have a nice weekend!";else  echo "Have a nice day!";?>

  13. if • <?php$d=date("D");if ($d=="Fri")  echo "Have a nice weekend!";elseif ($d=="Sun")  echo "Have a nice Sunday!";else  echo "Have a nice day!";?>

  14. switch • <?phpswitch ($x){case 1:  echo "Number 1";  break;case 2:  echo "Number 2";  break;case 3:  echo "Number 3";  break;default:  echo "No number between 1 and 3";}?>

  15. Numeric Arrays • $cars=array("Saab","Volvo","BMW","Toyota"); • -------------or • $cars[0]="Saab";$cars[1]="Volvo";$cars[2]="BMW";$cars[3]="Toyota"; echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";

  16. Associative Arrays • ID key => value • $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); • -------------------or----------------- • $ages['Peter'] = "32";$ages['Quagmire'] = "30";$ages['Joe'] = "34";echo "Peter is " . $ages['Peter'] . " years old."; • ---------------------------------- • Peter is 32 years old.

  17. Multidimensional Arrays

  18. Multidimensional Arrays • echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?"; • ---------------------------------- • Is Megan a part of the Griffin family?

  19. While Loops

  20. Do While Loops

  21. For Loops

  22. Foreach Loops

  23. Create a PHP Function

  24. Create a PHP Function

  25. Create a PHP Function

  26. Create a PHP Function

  27. $_GET variable • <form action="welcome.php" method="get">Name: <input type="text" name="fname" />Age: <input type="text" name="age" /><input type="submit" /></form> • ----------------------------- • http://localhost/welcome.php?fname=Peter&age=37 • ----------------------------- • Welcome <?php echo $_GET["fname"]; ?>.<br />You are <?php echo $_GET["age"]; ?> years old! • When using method="get" in HTML forms, all variable names and values are displayed in the URL.

  28. $_POST variable • <form action="welcome.php" method="post">Name: <input type="text" name="fname" />Age: <input type="text" name="age" /><input type="submit" /></form> • ------------------------------------ • Welcome <?php echo $_POST["fname"]; ?>!<br />You are <?php echo $_POST["age"]; ?> years old. • Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send. • However, because the variables are not displayed in the URL, it is not possible to bookmark the page.

  29. $_REQUEST Variable • Welcome <?php echo $_REQUEST["fname"]; ?>!<br />You are <?php echo $_REQUEST["age"]; ?> years old. • ---------- • The predefined $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. • The $_REQUEST variable can be used to collect form data sent with both the GET and POST methods.

  30. PHP Date() Function • date(format [, timestamp]) • <?phpecho date("Y/m/d") . "<br />";echo date("Y-m-d");?> • 2011/11/17 2011-11-17

  31. PHP Date() Function • mktime(hour,minute,second,month,day,year,is_dst) • <?php$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));echo "Tomorrow is ".date("Y/m/d", $tomorrow);?> • Tomorrow is 2011/11/18

  32. PHP include() Function • <html><body><?php include("header.php"); ?><h1>Welcome to my home page!</h1><p>Some text.</p></body></html>

  33. PHP include() Function

  34. PHP File Handling • Reading a File Line by Line <?php$file = fopen("welcome.txt", "r") or exit("Unable to open file!");//Output a line of the file until the end is reachedwhile(!feof($file))  {  echo fgets($file). "<br />";  }fclose($file);?>

  35. PHP File Handling • Reading a File Character by Character <?php$file=fopen("welcome.txt","r") or exit("Unable to open file!");while (!feof($file))  {  echo fgetc($file);  }fclose($file);?>

  36. PHP File Upload • Create an Upload-File Form <form action="upload_file.php" method="post"enctype="multipart/form-data"><label for="file">Filename:</label><input type="file" name="file" id="file" /> <br /><input type="submit" name="submit" value="Submit" /></form>

  37. Create The Upload Script • upload_file.php <?phpif ($_FILES["file"]["error"] > 0)  {  echo "Error: " . $_FILES["file"]["error"] . "<br />";  }else  {  echo "Upload: " . $_FILES["file"]["name"] . "<br />";  echo "Type: " . $_FILES["file"]["type"] . "<br />";  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";  echo "Stored in: " . $_FILES["file"]["tmp_name"];  }?>

  38. Save uploaded File • upload_file.php <?php// Upload file and Display result    if (file_exists("upload/" . $_FILES["file"]["name"]))      {      echo $_FILES["file"]["name"] . " already exists. ";      }    else      {move_uploaded_file($_FILES["file"]["tmp_name"],      "upload/" . $_FILES["file"]["name"]);      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];}?>

  39. Create a Cookie • setcookie(name, value, expire, path, domain); • <?phpsetcookie("user", "Alex Porter", time()+3600);?> • <?php$expire=time()+60*60*24*30;setcookie("user", "Alex Porter", $expire);?> • 60 sec * 60 min * 24 hours * 30 days

  40. Retrivea Cookie • <?phpif (isset($_COOKIE["user"]))  echo "Welcome " . $_COOKIE["user"] . "!<br />";else  echo "Welcome guest!<br />";?>

  41. Delete a Cookie • <?php// set the expiration date to one hour agosetcookie("user", "", time()-3600);?>

  42. PHP Session Variables • Starting a PHP Session • Before you can store user information in your PHP session, you must first start up the session. • Note: The session_start() function must apear BEFORE the <html> tag: • <?phpsession_start(); ?><html><body></body></html>

  43. PHP Session Variables • <?phpsession_start();// store session data$_SESSION['views']=1;?> • <?php//retrieve session dataecho "Pageviews=". $_SESSION['views'];?>

  44. PHP Session Variables • <?phpsession_start();if(isset($_SESSION['views']))$_SESSION['views']=$_SESSION['views']+1;else$_SESSION['views']=1;echo "Views=". $_SESSION['views'];?>

  45. PHP Destroying a Session • <?phpsession_destroy();?> • Free a session variable <?phpunset($_SESSION['views']);?>

  46. PHP mail() Function • <?php$to = "someone@example.com";$subject = "Test mail";$message = "Hello! This is a simple email message.";$from = "someonelse@example.com";$headers = "From:" . $from;mail($to,$subject,$message,$headers);echo "Mail Sent.";?>

  47. PHP mail() Function • <?phpif (isset($_REQUEST['email']))//if "email" is filled out, send email  {  //send email  $email = $_REQUEST['email'] ;  $subject = $_REQUEST['subject'] ;  $message = $_REQUEST['message'] ;  mail("someone@example.com", "$subject",  $message, "From:" . $email);  echo "Thank you for using our mail form";  }else//if "email" is not filled out, display the form  {  echo "<form method='post' action='mailform.php'>  Email: <input name='email' type='text' /><br />  Subject: <input name='subject' type='text' /><br />  Message:<br />  <textarea name='message' rows='15' cols='40'>  </textarea><br />  <input type='submit' />  </form>";  }?>

  48. Error Handling • <?phpif(!file_exists("welcome.txt"))  {  die("File not found");  }else  {  $file=fopen("welcome.txt","r");  }?>

  49. Error Handling • <?php//error handler functionfunction customError($errno, $errstr)  {  echo "<b>Error:</b> [$errno] $errstr<br />";  echo "Ending Script";  die();  }//set error handlerset_error_handler("customError",E_USER_WARNING);//trigger error$test=2;if ($test>1)  {trigger_error("Value must be 1 or below",E_USER_WARNING);  }?>

  50. Error Handling • <?php//create function with an exceptionfunction checkNum($number)  {  if($number>1){ throw new Exception("Value must be 1 or below");    }  return true;   }//trigger exception in a "try" blocktry{ checkNum(2);  //If the exception is thrown, this text will not be shown  echo 'If you see this, the number is 1 or below';   }//catch exceptioncatch(Exception $e){   echo 'Message: ' .$e->getMessage();   }?> • Message: Value must be 1 or below

More Related