Introduction To Php For Wit2009

1,110 views

Published on

This is a quick introduction to the basics of PHP.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,110
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
33
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Introduction To Php For Wit2009

  1. 1. Intro to PHP An introduction to web application development and the PHP programming language
  2. 2. What is PHP? "a widely-used Open Source general-purpose , server-side scripting language that is especially suited for Web development and can be embedded into HTML "
  3. 3. dejargonification <ul><li>Open Source - free, as in costs no money and modifiable </li></ul><ul><li>general purpose - can do all kinds of different things </li></ul><ul><li>server-side - the browser/user gets a normal web page </li></ul><ul><li>scripting - connotes loose, easy, and fast </li></ul><ul><ul><li>programming rather than layout </li></ul></ul><ul><ul><ul><li>Variables </li></ul></ul></ul><ul><ul><ul><li>Conditionals </li></ul></ul></ul><ul><ul><ul><li>Looping </li></ul></ul></ul><ul><li>suited to web development - good with text, lots of built-in support </li></ul><ul><li>embedded into HTML - intermixed with normal web page HTML code </li></ul>
  4. 4. Regular (HTML) Page vs PHP Page <ul><li>HTML Page </li></ul><ul><li>User puts a URL in browser (‘client’) </li></ul><ul><li>Browser sends request to host </li></ul><ul><li>Server (on host) receives request </li></ul><ul><li>Server reads requested file from hard drive </li></ul><ul><li>Server sends contents of file to client </li></ul><ul><li>HTML (file contents) goes to client </li></ul><ul><li>Client (browser) displays rendered HTML </li></ul><ul><li>PHP Page </li></ul><ul><li>User puts URL in browser </li></ul><ul><li>Browser sends request to host </li></ul><ul><li>Server receives request </li></ul><ul><li>Server notes it’s a PHP request, and passes it to the PHP handler </li></ul><ul><li>PHP handler reads PHP file from hard drive </li></ul><ul><li>PHP handler executes PHP code to generate HTML </li></ul><ul><li>PHP handler passes results back to server </li></ul><ul><li>Server sends results to client </li></ul><ul><li>HTML goes to the client </li></ul><ul><li>Client displays rendered HTML </li></ul>
  5. 5. Why bother with PHP? <ul><li>PHP is dynamic – can create HTML based on external, changing information </li></ul><ul><ul><li>System info (e.g. date and time) </li></ul></ul><ul><ul><li>Request info (e.g. IP address of client) </li></ul></ul><ul><ul><li>Form info (e.g. from info submitted by client) </li></ul></ul><ul><ul><li>Info on server (e.g. files, or a DB) </li></ul></ul><ul><li>PHP is modular </li></ul><ul><ul><li>Re-use sections of code (functions) </li></ul></ul><ul><ul><li>Include files (e.g. navigation headers, libraries) </li></ul></ul><ul><li>PHP can do processing in addition to returning HTML </li></ul><ul><ul><li>Send info in an email </li></ul></ul><ul><ul><li>Store info in a DB </li></ul></ul>
  6. 6. General Info <ul><li>PHP is typically written in a text editor (e.g. notepad, textpad) or a development environment (e.g. Dreamweaver). You write source code, which is executed/run when the file is requested over the web. </li></ul><ul><li>PHP is intermixed with HTML. You need to know HTML, and all that you do know about HTML will be useful when figuring out what your PHP code should do. </li></ul>
  7. 7. What and Where <ul><li>PHP is in blocks mixed in with regular HTML </li></ul><ul><li>A PHP block is marked by <?php to begin it, and ?> to end it </li></ul><ul><li><html> </li></ul><ul><li><head><title>A Page</title></head> </li></ul><ul><li><body> </li></ul><ul><li><h1>A Page Title, BIG</h1> </li></ul><ul><li><?php </li></ul><ul><li>echo 'text made by PHP code'; </li></ul><ul><li>?> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  8. 8. Basic Statements <ul><li>NOTE: Each statement ends with a ; </li></ul><ul><li>Some basic commands: </li></ul><ul><li>echo and print – creates text (HTML) that is sent back to the client </li></ul><ul><ul><li>echo 'hello'; </li></ul></ul><ul><ul><li>print '<h2>Yikes</h2>'; </li></ul></ul><ul><li>include – inserts the contents of another file or web page. If including a PHP page that code is executed. </li></ul><ul><ul><li>include 'menu.php'; </li></ul></ul>
  9. 9. Variables <ul><li>Variables are signified by a $ at the start of a word. The word is the variable name. Variable names can have letters and numbers and underscores, and are case-sensitive </li></ul><ul><ul><li>$v </li></ul></ul><ul><ul><li>$person1 </li></ul></ul><ul><ul><li>$first_name </li></ul></ul><ul><li>Assign (change) a variable using = </li></ul><ul><ul><li>$i = 5; </li></ul></ul>
  10. 10. Using Variables <ul><li>Store a value in a variable, then use it later </li></ul><ul><ul><li>$name = 'Chris'; </li></ul></ul><ul><ul><li>echo $name; </li></ul></ul><ul><li>Use them in a double quoted string </li></ul><ul><ul><li>echo &quot;My name is $name&quot;; </li></ul></ul><ul><li>use them to do math </li></ul><ul><ul><li>$length = 5; </li></ul></ul><ul><ul><li>$width = 8; </li></ul></ul><ul><ul><li>$area = $length * $width; </li></ul></ul>
  11. 11. Arrays <ul><li>Arrays are created with the array() command </li></ul><ul><ul><li>$people = array(‘Ayesha',‘Pinsi',‘Bret'); </li></ul></ul><ul><li>Stored values are accessed by an index number, which is in [] after the variable name. NOTE :The index starts at 0! </li></ul><ul><ul><li>$ people[0] is Ayesha </li></ul></ul><ul><ul><li>$ people[2] is Bret </li></ul></ul><ul><li>Arrays can hold any kind of value (including other arrays </li></ul><ul><ul><li>$mixed = array(4,'Fred',2.5, array(‘red’,’green’)); </li></ul></ul><ul><li>Use an array variable with an index the same way you'd use any other variable </li></ul><ul><ul><li>$results[3] = 5+6; </li></ul></ul>
  12. 12. Hashes (Associative Arrays) <ul><li>Where an array uses a number as the index, a hash (a.k.a. associative array) uses a string. </li></ul><ul><ul><li>$person = array(‘name’ => ‘Jeff’, </li></ul></ul><ul><ul><li>‘ class’ => ‘2010’, </li></ul></ul><ul><ul><li>‘ sport’ => ‘cross country’); </li></ul></ul><ul><li>Values are references using the relevant string index (called the ‘key’). </li></ul><ul><ul><li>$person[‘name’] is Jeff – the key is ‘name’ and the value is ‘Jeff’ </li></ul></ul>
  13. 13. Operators <ul><li>Math operators </li></ul><ul><ul><li>+, -, *, / standard math </li></ul></ul><ul><li>String operators </li></ul><ul><ul><li>. join two strings together </li></ul></ul><ul><li>Comparison operators (a.k.a. comparators) </li></ul><ul><ul><li>>, <, <=, >=, == </li></ul></ul><ul><li>Boolean (true/false) operators </li></ul><ul><ul><li>&& (AND, ‘and also’) || (OR, ‘either or both’) </li></ul></ul><ul><ul><ul><li>T && T = T T || T = T </li></ul></ul></ul><ul><ul><ul><li>T && F = F T || F = T </li></ul></ul></ul><ul><ul><ul><li>F && T = F F || T = T </li></ul></ul></ul><ul><ul><ul><li>F && F = F F || F = F </li></ul></ul></ul><ul><li>Order of operation –when in doubt use () </li></ul>
  14. 14. Control Structures - Types <ul><li>Control structures are special commands that determine which pieces of PHP actually get executed </li></ul><ul><li>There are two main types: conditionals (if statements) and loops </li></ul><ul><li>Conditional – code is executed only when a condition is true. </li></ul><ul><ul><li>if (condition is true) { do this } </li></ul></ul><ul><ul><ul><li>else if (condition) {do this instead} </li></ul></ul></ul><ul><ul><ul><li>else {do this if no conditions are true} </li></ul></ul></ul><ul><li>Loop – execute the same section of code repeatedly </li></ul><ul><ul><li>while (condition is true) {do this} </li></ul></ul><ul><ul><li>for (specific number of times) {do this} </li></ul></ul><ul><ul><li>foreach (element in array) {do this} </li></ul></ul>
  15. 15. Control Structures - Conditions <ul><li>A condition is anything that is evaluated to true or false </li></ul><ul><li>For basic values: 0, empty string(''), and empty array( () ) are false, everything else is true </li></ul><ul><li>More conditions are created using comparators, boolean operators (&&, ||), and functions (e.g. is_numeric($foo)) </li></ul><ul><li>Combine simple conditions to create more complex ones </li></ul><ul><ul><li>(($foo == 'cow') || ($days < 4)) </li></ul></ul>
  16. 16. The if statement <ul><li>Use when you want an action to depend on something: </li></ul><ul><li>if ($value > 10) { </li></ul><ul><li>echo ‘good buy’; </li></ul><ul><li>} elseif ($value > 5) { </li></ul><ul><li>echo ‘consider it’; </li></ul><ul><li>} else { </li></ul><ul><li>echo ‘not worth it’; </li></ul><ul><li>} </li></ul>
  17. 17. Loops – while, for, and foreach <ul><li>Loops are used to repeat actions (e.g. creating a table row by row) </li></ul><ul><li>while loop – repeat until a certain condition is met </li></ul><ul><ul><li>$numLines = 0; </li></ul></ul><ul><ul><li>while (thereAreMoreLinesToProcess()) { </li></ul></ul><ul><ul><li>echo “a line!<br /> ”; </li></ul></ul><ul><ul><li>$numLines++; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>for loop – repeat a certain number of times </li></ul><ul><ul><li>for ($i = 0; $i < 10; $i++) { </li></ul></ul><ul><ul><li>echo “line $i<br /> ”; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>The foreach loop iterates over an array or hash </li></ul><ul><ul><li>foreach ($ar as $elt) { … } </li></ul></ul><ul><ul><li>foreach ($ha as $key => $val) { … } </li></ul></ul>
  18. 18. Functions <ul><li>A section of code / an action that can be used repeatedly – a function is kind of a mini-program </li></ul><ul><li>A function has a name </li></ul><ul><li>A function is used (called) by using its name </li></ul><ul><ul><li>Each time the function is called it does its action </li></ul></ul><ul><ul><li>After the function is done it returns to the place from which it was called </li></ul></ul><ul><li>The effects of a function may be controlled through parameters </li></ul><ul><ul><li>The parameters are the things in parentheses that come after the function name – e.g. in displayUser(‘Azd’) the name of the function is displayUser and the parameter is the string ‘Azd’ </li></ul></ul><ul><li>Functions may return values </li></ul><ul><ul><li>E.g. $area = squareIt(5); </li></ul></ul>
  19. 19. Built-in Functions <ul><li>Built-in functions are pre-made pieces of code that do specific things with or to whatever you give them </li></ul><ul><li>In many ways, the built in function are the language </li></ul><ul><li>PHP has a large set of them </li></ul><ul><li>http://us3.php.net/manual/en/ </li></ul>
  20. 20. Creating Your Own Functions <ul><li>There are four main things to deal with when creating a function: the name, the parameters, the code, and the returned value. </li></ul><ul><li>Functions are created using the special ‘function’ key word </li></ul><ul><ul><li>function someName($param1, $param2) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>code; </li></ul></ul><ul><ul><li>code; </li></ul></ul><ul><ul><li>return value; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  21. 21. Example Functions <ul><li>function sayHi() </li></ul><ul><li>{ </li></ul><ul><li>echo “Hello!”; </li></ul><ul><li>} </li></ul><ul><li>function isAllowed($userName,$area) </li></ul><ul><li>{ </li></ul><ul><li>$retVal = false; </li></ul><ul><li>if (($area == ‘public’) || ($userName == ‘admin’)) </li></ul><ul><li>{ </li></ul><ul><li>$retVal = true; </li></ul><ul><li>} </li></ul><ul><li>return $retVal; </li></ul><ul><li>// PERHAPS BETTER: return (($area == ‘public’) || ($userName == ‘admin’)); </li></ul><ul><li>} </li></ul>
  22. 22. The include command <ul><li>The ‘include’ command is very important. It lets you add a existing file into the current page. There are two main ways people use this: </li></ul><ul><ul><li>they create segments (header, footer, nav menu, etc.) and then include them on each page – this makes maintenance easier. </li></ul></ul><ul><ul><li>they create collections of code and functions (generally called ‘libraries’), so they don’t have to re-define a useful function or repeat code in every page in which they want to use it. </li></ul></ul>
  23. 23. On Your Own <ul><li>PHP Manual: </li></ul><ul><li>http://us3.php.net/manual/en/ </li></ul><ul><li>XAMPP: </li></ul><ul><li>http://www.apachefriends.org/en/xampp.html </li></ul>

×