introduction to php development
DESCRIPTION
Introduction to PHP development. Daniel Reeves Web Applications Developer ResNET UNC Chapel Hill. My Assumptions. Introductory class Skill Level Programming concepts Topics Covered Future Presentations Mind Numbing Materials. Outline. Introduction PHP Basics Programming and PHP - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/1.jpg)
Introduction to PHP development
Daniel ReevesWeb Applications Developer
ResNET UNC Chapel Hill
![Page 2: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/2.jpg)
resnet.unc.edu 2
My Assumptions
Introductory class Skill Level Programming concepts Topics Covered Future Presentations Mind Numbing Materials
![Page 3: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/3.jpg)
resnet.unc.edu 3
Outline
Introduction PHP Basics Programming and PHP PHP Topics Questions
![Page 4: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/4.jpg)
Introduction
![Page 5: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/5.jpg)
Questions for you
![Page 6: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/6.jpg)
resnet.unc.edu 7
Materials
Location• http://webmasters.unc.edu/presentation
s/
• ZIP file of all materials (ppt, handout, code)
Handout• Terms, tips, Google search strategies
PowerPoint Video
![Page 7: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/7.jpg)
resnet.unc.edu 8
To PHP or not to PHP? The Pros
Great Documentation Allows coding to modern standards Open Source Global and Diverse Developer
Community Don’t have to reinvent the wheel Total Programmer Control Easy to deploy
![Page 8: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/8.jpg)
resnet.unc.edu 9
To PHP or not to PHP? The Cons
Inconsistent function names Only Basic scoping Constantly Evolving Total Programmer Control Not most efficient language
![Page 9: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/9.jpg)
resnet.unc.edu 10
Google it !!!!!
When in doubt, search! Search before you build Properly formed Searches on Google
• Google’s autocomplete is For PHP searches, start by typing
“php”• Suggestions will start to show
![Page 10: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/10.jpg)
resnet.unc.edu 11
Programming Philosophy
Architecture Analogy• Structure: Problem/Constraints/Solution• Materials• Tools (data structures)
4 general uses: • automation, iteration/reuse, functionality,
uniformity Know Thy Enemy Pure problem solving
![Page 11: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/11.jpg)
resnet.unc.edu 12
Programming Tips
Process Sequentially Save often, keep multiple versions Test incrementally Reuse code, copy and paste Start small and build Solving problems is a mental process Walk away and come back
![Page 12: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/12.jpg)
PHP Basics
![Page 13: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/13.jpg)
resnet.unc.edu 14
Client/Server
Client = User’s Browser Server = Websites, Online files PHP server only Can only process when …
• When a page is requested• After a user action has submitted the
page
![Page 14: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/14.jpg)
resnet.unc.edu 15
What can one do with PHP?
Database Programming
File Read/Write Form Processing Form Validation Ajax Interaction PDF Creation File Search and
Archive
Interact with web APIs
Interact with MS Office
Image Processing and
Chart Creation XML file Read/Write Sending Email Writing Code
![Page 15: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/15.jpg)
resnet.unc.edu 16
Topics for Today
Basic PHP/HTML interaction• Creating HTML with PHP
PHP form validation and processing• Server request interaction• Input validation
Basic debugging and error trapping• Methods for finding and correcting errors
![Page 16: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/16.jpg)
resnet.unc.edu 17
What you need
Server space where PHP is enabled• http://help.unc.edu/108
Editing program to write the code in• Notepad++ good, free editor for coding
HTML Browser for viewing created pages• Firefox is my preference
Second monitor – recommended
![Page 17: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/17.jpg)
Programming PHP
![Page 18: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/18.jpg)
resnet.unc.edu 19
Writing Clean Code
Writing Clean Code• Using tabs for code, white space, line
breaks• Ex: which looks cleaner, easier to read?<?php echo “test”; $foo++; ?>OR<?php
echo “test”;$foo++;
?>
![Page 19: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/19.jpg)
resnet.unc.edu 20
PHP file, <?php tag, & ‘;’
Files end in .php (ex: contact_form.php)
Server recognizes code within <?php tag
Requires ?> closing tag When writing opening tag, write
closing Code not in PHP tags displays as
HTML End statements with ;
![Page 20: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/20.jpg)
resnet.unc.edu 21
Example – php_tags
<?php
?>testing
![Page 21: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/21.jpg)
resnet.unc.edu 22
Errors and Error Messages
Empty Page• Difficult, frustrating when page is just
blank Turn on Errors
• On Handout: ini_set('display_errors',1); Shows warnings and errors Dangerous for production code
![Page 22: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/22.jpg)
resnet.unc.edu 23
Example – php_errors
<?phpini_set('display_errors',1);
?>testing
![Page 23: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/23.jpg)
resnet.unc.edu 24
PHP Programming – Hello World
How do I know if PHP is turned on?• php_info();
“Hello World” Echo statement Single/Double quotes in PHP
• echo “testing”; same as echo ‘testing’;• Use double quotes for variables
![Page 24: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/24.jpg)
resnet.unc.edu 25
Example – Hello World
<?phpini_set('display_errors',1);php_info();echo “Hello World”;
?>testing
![Page 25: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/25.jpg)
resnet.unc.edu 26
What are Variables ?
Basic building block of programming Stores information/data 3 basic parts: name, type and value Variables store different types of data
• Numbers, Strings, Boolean (True/False) PHP Initialization PHP Types
![Page 26: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/26.jpg)
resnet.unc.edu 27
PHP Variable Syntax
All variables start with “$” Any combination of letters, numbers,
“_” Variable Type Examples
• Ex: $customer = “Daniel Reeves”;• Ex: $cost = 1.25;• Ex: $is_administrator = true;
Will use $foo as default variable• $foo, $foo1, $foo2, etc.
![Page 27: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/27.jpg)
resnet.unc.edu 28
Manipulating Variables
Set Variables • $foo = 1; $foo = “test”; $foo1 =
$foo Display Variables
• echo $foo; echo “$foo”; Change and Combine text
• $foo = “testing”.“ is fun”;• $foo1 = “Foo’s Value is: $foo”;
![Page 28: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/28.jpg)
resnet.unc.edu 29
And Arrays?
Collections of variable values Access various elements from index Index number or keyword
• 1st element starts at 0 Format: $array_name[‘index’] Array elements same as variables Mostly for form processing
• $_GET, $_POST
![Page 29: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/29.jpg)
resnet.unc.edu 30
PHP Array Syntax
Create• $numbers = array(1,2,3,4,5);• $names = array(‘fn’=>”Bob”,
‘ln’=>”Smith”); Numbers
5 = $numbers[4]; Strings
• “Bob” = $names[‘fn’];• “Bob” = $names[0];
![Page 30: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/30.jpg)
resnet.unc.edu 31
Conditionals – If/Then/Else
Need to test truth values If/ Else If/Else Establish consequences based on
test Allows for options Example: Contact form
• Category for student major• When submitted, choice would be tested• Assign a different action per major
![Page 31: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/31.jpg)
resnet.unc.edu 32
Conditionals – PHP Syntax
Syntax: if( testcondition ){ code } Syntax for if, else:
• if( testcondition ){ code }• else if( testcondition ){ code }• else {code}
Test values with: <, <=, >, >=, !, ==, !=
![Page 32: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/32.jpg)
resnet.unc.edu 33
PHP Programming – Functions
Reuse Separate code Elements
• Name, Parameters, Code, Return Many system functions available
• include(); php_info(); echo “”; Ex: function strlen($foo);
• Returns number of characters in string
![Page 33: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/33.jpg)
resnet.unc.edu 34
Review – PHP Basics
<?php and ?> tags, required “;” Turn on errors and error messages Variables
• Strings, Numbers, Boolean (T/F) Arrays Conditionals Functions
![Page 34: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/34.jpg)
PHP topics – basic PHP and HTML
![Page 35: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/35.jpg)
resnet.unc.edu 36
Adding Dynamic Content
Use variables 2 Philosophies 2 ways to display information Echo
• echo “First Name: $first_name”; In HTML
• First Name: <?= $first_name ?>
![Page 36: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/36.jpg)
resnet.unc.edu 37
Example – hello_world2
<?php$first_name = “Bob”;$last_name = “Smith”;echo “<html><body>”;
?><p>My Name is: <?= $first_name ?> <?= $last_name?></p></body></html>
![Page 37: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/37.jpg)
resnet.unc.edu 38
Creating HTML with PHP
HTML Break and new line• To write new line to document, use \n• To add new line to rendered HTML, use
<br> Build then display HTML put just below php on page
• Nice segregation of server, client sides• Much easier to comment and debug
Create JS with PHP too
![Page 38: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/38.jpg)
resnet.unc.edu 39
Display HTML uniformity
Includes• Headers, footers, menus
Widths, heights• Easily keep for element sizes uniform
Naming conventions• Prefix for names or css classes
Other repeated code• Especially if it may ever change
![Page 39: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/39.jpg)
resnet.unc.edu 40
Review – PHP and HTML
Adding dynamic content Use Variables Output to screen: echo, <?= ?> Breaks <br> and New Lines \n Display Uniformity
• Includes, reuse, naming, cohesive form items
![Page 40: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/40.jpg)
PHP topics – PHP form processing and validation
![Page 41: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/41.jpg)
resnet.unc.edu 42
PHP and form submission
Process of filling out a form Where PHP gets access to form GET and POST PHP and request information
• GET and POST arrays• $_GET[‘form element name’];• $_POST[‘form element name’];
![Page 42: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/42.jpg)
resnet.unc.edu 43
Getting data from your request
Good first test: use GET• Can see parameters you are sending• Don’t need form• Print entire $_GET array: print_r($_GET);
Set items from GET to variables• Use same name as name you sent
Once variable, can process further
![Page 43: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/43.jpg)
resnet.unc.edu 44
Example - get
<?phpprint_r($_GET);if($_GET){
$foo = $_GET['passed_value'];} else { $foo = "Empty"; }?>Value is: <?= $foo ?>
![Page 44: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/44.jpg)
resnet.unc.edu 45
EXAMPLE – contact_form
<html><body><form action=“contact_form.php” method=“get”>
Input: <input type=“text” name=“passed_value” value=“<?= $foo ?>”><input type=“submit” value=“Submit”>
</form></body></html>
![Page 45: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/45.jpg)
resnet.unc.edu 46
Validation Options
Cleaning user input discussed later Multiple validations of data Client = JavaScript, Server = PHP
• JS can be turned off from client, PHP cannot
• Test both ends, safer, better use experience
Validation proportional to importance Separate from security
![Page 46: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/46.jpg)
resnet.unc.edu 47
Using PHP for validation
Test multiple issues for same value Test with conditionals Test for empty values
• If($foo == “”) { do something } Flags = Boolean variables (T/F) Use variable to verify validation
• Set to true• If any validation fails, set it to false
![Page 47: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/47.jpg)
resnet.unc.edu 48
EXAMPLE – contact_form (above
form)<?phpif($_GET){
$foo = $_GET[‘passed_value’];$form_passes = true;if($foo == “”){ $form_passes = false; }if(strlen($foo) < 5) {$form_passes =
false; }if($form_passes){ finish processing }
}?>
![Page 48: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/48.jpg)
resnet.unc.edu 49
Redisplay and Error notification
Good idea to redisplay form• Can redisplay all submitted information
Easy to find & read error messages• DIV at top of page, red and bold text• Tailored message for each issue
Interact with JS if helpful PHP mark the fields in error
![Page 49: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/49.jpg)
resnet.unc.edu 50
Process for Validation
User Submits page to server Use PHP to get information Test information
• Not empty, length Errors and Redisplay
• Show messages• Mark errors
![Page 50: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/50.jpg)
resnet.unc.edu 51
Review – PHP and Forms
PHP gets form data at submission $_GET, $_POST and variables Validation
• Conditionals• Functions
Error notification and Redisplay• Communicate clearly with user
![Page 51: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/51.jpg)
PHP topics – Debugging Errors
![Page 52: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/52.jpg)
resnet.unc.edu 64
The fun of errors
Programming requires proper syntax One error, entire page wont run Bad code can crash servers (unlikely) Example: Mariner 1 Rocket Realistic Worries
• Angering ITS Gods (Gogan does not approve)
• Loss, Compromise of data
![Page 53: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/53.jpg)
resnet.unc.edu 65
Common mistakes
Misspelling variable names Leaving semicolon off Not closing PHP tag Quotes within quotes Refresh page Not saving work
![Page 54: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/54.jpg)
resnet.unc.edu 66
Commenting Code
Comments• PHP does not run in comments
2 Uses• Removing code from page• Document what the code does
2 ways: // or /* … */• Single vs. Multi-line comments
![Page 55: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/55.jpg)
resnet.unc.edu 67
Example - comments
<?phpini_set('display_errors',1);/* Broken Code */echo “broke“//Working Codeecho “works";
?>testing
![Page 56: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/56.jpg)
resnet.unc.edu 68
Example – comments
<?phpini_set('display_errors',1);/* Broken Codeecho “broke“ *///Working Codeecho “works";
?>testing
![Page 57: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/57.jpg)
resnet.unc.edu 69
die(); to the rescue
PHP system function Will stop any PHP immediately Process to find errors
• Start with error message, may be simple• Put a die statement at top of page• Move down page section by section• When die no longer works, have found
error• Error is in the last block of code you past
![Page 58: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/58.jpg)
resnet.unc.edu 70
Example – die();
<?phpdie(‘testing1’);ini_set('display_errors',1);echo "works";echo "broke"
?>testing
![Page 59: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/59.jpg)
resnet.unc.edu 71
Example – die();
<?phpini_set('display_errors',1);die(‘testing1’);echo “works";echo “broke“
?>testing
![Page 60: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/60.jpg)
resnet.unc.edu 72
Example – die();
<?phpini_set('display_errors',1);echo “works";die(‘testing1’);echo “broke“
?>testing
![Page 61: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/61.jpg)
resnet.unc.edu 73
Example – die();
<?phpini_set('display_errors',1);echo “works";echo “broke“die(‘testing1’);
?>testing
![Page 62: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/62.jpg)
resnet.unc.edu 74
Review – Debugging PHP
Errors are fun Common Mistakes Comment Code Die function Process for finding errors
![Page 63: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/63.jpg)
Final words on PHP
![Page 64: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/64.jpg)
resnet.unc.edu 76
My take
PHP highly powerful, useful web tool• Can do everything I need efficiently• Unique properties great bonus• PHP my favorite by far
Use what you like• Make sure its open source if you can• Coding should be challenging but fun
Big picture, how programming can help
![Page 65: Introduction to PHP development](https://reader036.vdocuments.us/reader036/viewer/2022081502/5681632e550346895dd3aa65/html5/thumbnails/65.jpg)
Questions?