Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Intro to-php-19 jun10


Published on

A very basic introduction to the PHP programming language for people interested in learning more about it.

Published in: Technology
  • Be the first to comment

Intro to-php-19 jun10

  1. 1. <ul><li>Introduction to PHP </li></ul><ul><li>19 June 2010 </li></ul><ul><li>Kathy Reid </li></ul><ul><li>@KathyReid | [email_address] </li></ul>
  2. 2. What are we going to cover? <ul><li>What PHP is, what it can do and how it differs from HTML </li></ul><ul><li>How web servers work and the HTTP request-response lifecycle </li></ul><ul><li>Basic statements in PHP and an introduction to procedural programming and forms and databases overview </li></ul><ul><li>An overview to common PHP tools </li></ul><ul><li>How to get more help with PHP </li></ul>
  3. 3. What is PHP? <ul><li>PHP: Hypertext preprocessor </li></ul><ul><li>Server-side scripting language (has to be run on a server, not in your browser like Javascript – that's a client-side language) </li></ul><ul><li>Open source and free to download (this means that the code behind PHP is freely available - this often means PHP hosting is cheaper than other platforms, such as ASP) </li></ul><ul><li>Particularly suitable for web applications, but doesn't have to be used for this purpose </li></ul>
  4. 4. What can PHP do? <ul><li>PHP is a general purpose scripting language. This means it can do lots of programming tasks. It is particularly suited to; </li></ul><ul><ul><li>Adding dynamic content to websites </li></ul></ul><ul><ul><li>Processing HTML forms </li></ul></ul><ul><ul><li>Connecting to databases and storing, searching and retrieving information from databases </li></ul></ul><ul><ul><li>Personalising and customising websites </li></ul></ul>
  5. 5. Why should I learn PHP? <ul><li>To grow an evil army of programmers to take over the world MWHAHAHAHAHAHA … but seriously there are some good reasons </li></ul><ul><li>Easy to learn (difficult to master) </li></ul><ul><li>Used widely in third party tools – WordPress, Drupal, CiviCRM, Facebook applications etc </li></ul><ul><li>Becoming much more mature and adopted as an enterprise-level programming language </li></ul>
  6. 6. What do I need to get started with PHP? <ul><li>Access to a web server with PHP installed </li></ul><ul><ul><li>This might be through a hosting company, and you would access the server using an FTP program, like FileZilla . The files would be accessed from a URL like </li></ul></ul><ul><ul><li>If you're more advanced, you might wish to have a go installing PHP on your own computer. There are builds available for Windows, most Linux variants and even MacOS. http://localhost/myfile.php </li></ul></ul>
  7. 7. What do I need to get started with PHP (cont'd) <ul><li>It helps to have a good understanding of how HTTP works (covered in this tutorial) </li></ul><ul><li>It helps to have at least a basic understanding of HTML (any version of HTML to be honest) </li></ul><ul><li>Your text editor of choice. Syntax highlighting is a great feature to have in a PHP editor. I use Eclipse , but there are many others to choose from; </li></ul>
  8. 8. Easy to learn, difficult to master <ul><li>One of the great features of PHP is that it is easy to learn. It is also difficult to master; </li></ul><ul><ul><li>Writing maintainable code </li></ul></ul><ul><ul><li>Reusable code (through objects) </li></ul></ul><ul><ul><li>Designing applications to be secure </li></ul></ul><ul><ul><li>Designing for scalability </li></ul></ul><ul><ul><li>Designing for portability (between different databases and server operating environments) </li></ul></ul><ul><li>There's always more to learn about PHP!! </li></ul>
  9. 9. Why is PHP different to HTML? <ul><li>HTML is static </li></ul><ul><li><html> <body> <h1>Hello world! </h1> </body> </html> </li></ul><ul><li>Will always display 'Hello world!' </li></ul>
  10. 10. Why is PHP different to HTML? (cont'd) <ul><li>PHP is dynamic </li></ul><ul><li><?php $greeting = 'Hola!'; ?> <html> <body> <h1> <?php echo $greeting; ?> </h1> </body> </html> </li></ul><ul><li>What will this display? </li></ul>
  11. 11. How web servers work <ul><li>So what makes PHP dynamic ? </li></ul><ul><li>Web servers and user agents (browsers) work in what's called a 'client-server' relationship. </li></ul><ul><li>The user agent (browser) sends a request to the web server </li></ul><ul><li>The web server interprets that request, and sends a response to the web server </li></ul>
  12. 12. How web servers work (cont'd) <ul><li>User agent </li></ul><ul><ul><li>Browser – such as Firefox, Internet Explorer, Safari – on a computer, mobile phone, iPhone or iPad etc </li></ul></ul><ul><ul><li>Issues requests to a web server </li></ul></ul><ul><li>For example, going to this URL in your browser causes the browser to issue this request: </li></ul><ul><li>GET /index.php HTTP/1.1 </li></ul><ul><li>Host: </li></ul>
  13. 13. How web servers work (con'td) <ul><li>Web server </li></ul><ul><ul><li>Many varieties of web server – such as IIS (Internet information services), Apache httpd etc. </li></ul></ul><ul><ul><li>PHP is primarily used with Apache httpd, but can be used with IIS </li></ul></ul><ul><ul><li>Interprets requests from a user agent, acts on them and issues responses </li></ul></ul><ul><li>For example, a web server may respond to the previous request with this: </li></ul>
  14. 14. How web servers work (cont'd) <ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li><h1>My index page</h1> </li></ul><ul><li><p>This is the content on the index page!</p> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  15. 15. How web servers work (cont'd) <ul><li>The browser then interprets the request , which is usually in HTML format, and renders the HTML on screen. </li></ul><ul><li>This is why the same HTML can look different between different browsers – for instance Firefox and Internet Explorer – because the rendering engines inside the browsers are different. </li></ul><ul><ul><li>This is a key source of frustration for web developers! </li></ul></ul>
  16. 16. How PHP works with the web server <ul><li>Okay, so we've seen how HTTP requests and responses work. But this is exactly how HTML works! How does PHP come into it? </li></ul><ul><ul><li>Before the web server (for example Apache) issues the response, the PHP interpreter is invoked and processes the information in the PHP script. </li></ul></ul><ul><ul><li>This is why PHP files are called 'filename.php' and normal (static) html files are called 'filename.html' – it helps the web server determine whether to invoke the PHP interpreter or not. </li></ul></ul>
  17. 17. How PHP works with the web server (cont'd) <ul><li>For example, let's say I put this code in a file; <html> <body> <h1> <?php echo date(&quot;F j, Y, g:i a&quot;); ?> </h1> </body> </html> </li></ul><ul><li>… and I call the file 'something. html ' </li></ul><ul><li>What will be displayed on the page? </li></ul>
  18. 18. How PHP works with the web server (cont'd) <ul><li>Rather than the current date and time, it will display something like this; </li></ul><ul><li><?php echo date(&quot;F j, Y, g:i a&quot;); ?> </li></ul><ul><li>This is because the PHP interpreter was not invoked due to the file being named something.html – the PHP script is not interpreted, and values substituted before the web server issued the response! </li></ul>
  19. 19. How PHP works with the web server (cont'd) <ul><li>If on the other hand, the file was named something. php , the PHP interpreter would be invoked and you would see something similar to; </li></ul><ul><li>June 19, 2010, 2.30 pm </li></ul><ul><li>This is because the PHP interpreter was invoked , and the PHP script processed before the response was sent to the user agent. </li></ul>
  20. 20. Quick recap before we take a look at some code <ul><li>PHP is a dynamic scripting language generally used with a web server </li></ul><ul><li>User agents (browsers) issue requests to web servers. Web servers process these requests. They may invoke the PHP interpreter. The web server then issues a response back to the user agent, usually in HTML. The user agent then renders the HTML, and that's why we see a web page :-) </li></ul>
  21. 21. PHP Basics: Variables <ul><li>Examples of declaring variables in PHP </li></ul><ul><li><?php </li></ul><ul><li>$myString = 'Hello world'; </li></ul><ul><li>$myInt = 999; </li></ul><ul><li>$myFloat = 12345; </li></ul><ul><li>$myArray = array(); </li></ul><ul><li>$myObject = new someObject(); </li></ul><ul><li>?> </li></ul>
  22. 22. PHP Basics: Variables <ul><li>Variables in PHP don't have to be explicitly declared – but it is best practice to do so for readability and for debugging </li></ul><ul><li>Variables in PHP are loosely typed – the type of value is not stated when the variable is declared – it is implicit. This can also be difficult to debug – as variables can unknowingly be cast into different types – with unexpected results! </li></ul>
  23. 23. PHP Basics: Variables <ul><li>Basic example. What might happen with $total being declared implicitly here? </li></ul><ul><li><?php </li></ul><ul><li>$cost = 10; // declared explicitly </li></ul><ul><li>$units = 5; // declared explicitly </li></ul><ul><li>// do some stuff in here </li></ul><ul><li>$total = $cost * $units; // implicit </li></ul><ul><li>?> </li></ul>
  24. 24. PHP Basics: Control structures <ul><li>Control structures alter the flow of execution of a PHP script. </li></ul><ul><li>if...then...else </li></ul><ul><ul><li>If something is true, do x, else do y </li></ul></ul><ul><li>While ... </li></ul><ul><ul><li>While something is true, keep doing x </li></ul></ul><ul><li>Switch </li></ul><ul><ul><li>If the condition is a, do action for a </li></ul></ul>
  25. 25. PHP Basics: Control structures <ul><li>try...throw...catch </li></ul><ul><ul><li>Try to do something, but if the something fails, then throw an error. The error can then be caught and processed, such as by displaying an appropriate error message </li></ul></ul><ul><ul><li>This is a more advanced control structure, but it is very worthwhile learning, particularly if you plan to use object oriented programming. </li></ul></ul>
  26. 26. PHP Basics: Arrays <ul><li>Arrays in PHP are incredibly powerful. There are a large number of array functions available to choose from. </li></ul><ul><li>Arrays in PHP are essentially collections of key-value pairs . In some programming languages, the key is always a number. In PHP, the key can be a string or a number. This is referred to as an associative array . </li></ul>
  27. 27. PHP Basics: Arrays <ul><li><?php </li></ul><ul><li>$array1 = array(10, 20, 40, 80, 160); </li></ul><ul><li>$array2 = array('banana', 'pear', 'apricot'); </li></ul><ul><li>$array3 = array(0 => 4, 3 => 3838); </li></ul><ul><li>$array4 = array('beans' => 'borlotti', </li></ul><ul><li>'icecream' => 'chocolate'); </li></ul><ul><li>?> </li></ul>my
  28. 28. PHP Basics: Working with forms <ul><li>One of the common uses for PHP is to process web based forms. When a HTML form is submitted to a PHP script, a number of variables are made available to PHP. </li></ul><ul><li>To make a HTML form submit to a PHP script, the path to the PHP script has to be given in the FORM element; </li></ul><ul><li><form name=&quot;myForm&quot; action=&quot;/myscript.php/&quot;> </li></ul>
  29. 29. PHP Basics: Working with forms <ul><li>Once the form has been submitted, the values will be available in $_POST superglobal array. We can iterate through this array to process the form, send emails etc. <form name=&quot;myForm&quot; action=&quot;/myscript.php/&quot;> <input type=”text” name=”myTextField”> </form> </li></ul>
  30. 30. PHP Basics: Working with forms <ul><li>The data from this input field will be available in the variable; </li></ul><ul><li>$_POST['myTextField'] </li></ul><ul><li>This data can then be used for instance to put into an email, insert into a database etc. </li></ul>
  31. 31. PHP Basics: Working with databases <ul><li>PHP can connect to a database using functions specific to the database </li></ul><ul><li>For example MySQL </li></ul><ul><li>Database connection usually requires username, password and database name </li></ul><ul><li>Not enough time here to cover all database concepts, but can cover a brief introduction to database theory if you'd like? </li></ul>
  32. 32. Commonly used free and open source software which utilises PHP <ul><li>Wordpress – blogging and content management software </li></ul><ul><li>Drupal – content management software </li></ul><ul><li>PHPMyAdmin – provides an easy to use web interface to MySQL databases </li></ul>
  33. 33. Commonly used free and open source software which utilises PHP (cont'd) <ul><li>Zen Cart – online shopping cart </li></ul><ul><li>Moodle – Learning Management Software </li></ul><ul><li>Mahara – e-Portfolios </li></ul><ul><li>Media Wiki – wiki software </li></ul>
  34. 34. PHP frameworks <ul><li>A framework is basically a collection of components that make development easier by abstracting commonly used functions. The developer can then focus on the 'core features' of the application they are creating, rather than worrying about formatting, database connections etc </li></ul><ul><li>Examples include Zend, Symfony </li></ul>
  35. 35. More information and resources <ul><li>What is PHP? </li></ul><ul><li>What can PHP do? </li></ul><ul><li>Introductory tutorial (from </li></ul>
  36. 36. More information and resources (cont'd) <ul><li>Other good introductory tutorials (mostly from Slideshare) </li></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul>
  37. 37. User groups <ul><li>PHP Melbourne irc://freenode/phpmelb </li></ul><ul><li>PHPWomen (XY welcome, we exist to be inclusive and help support women in learning and mastering PHP) irc://freenode/phpmelb @PHPWomen on Twitter </li></ul>
  38. 38. Related user groups and developer communities <ul><li>Melbourne WordPress group </li></ul><ul><li>Melbourne Drupal group </li></ul><ul><li>Melbourne MySQL user group </li></ul>