Week 5
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,276
On Slideshare
1,274
From Embeds
2
Number of Embeds
1

Actions

Shares
Downloads
2
Comments
0
Likes
0

Embeds 2

http://www.deinega.com 2

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • Separation of presentation and structure (CSS and HTML);Separation of behavior and structure (JavaScript and HTML);Separation of behavior and presentation (JavaScript and CSS)
  • Different languages run on the different components.Browser: HTML, CSS, JavascriptWeb Server: PHP, ASP, JSP, CFDatabase Server: SQL
  • What could be wrong? Not the write keyword, used the keyword incorrectly, logic problems, and lots of other reasons.
  • Somewhere in a chunk of your computer’s memory, the letters “Mr. Morton” exist. You don’t need to know exactly where. You said that the name for that series of letters is Teacher, and that’s how you will refer to it from now on. A name is like a label or tag or sticky note that you attach to something to identify what it is.When you put quotes around something, most computer languagestake it literally. It prints exactly what is in the quotes. When you don’t put quotes around the thing, the languagehas to figure out what the thing is. It could be a number (like 5), an expression (like 5 + 3), or a name (like Teacher).
  • In this example, instead of doing the sum in the print instruction, we took the thing named First and the thing named Second and added them together, creating a new thing, called Third. Third is the sum of First and Second.
  • But you do have to use the same kind of quotes at the start and the end of the string.
  • Whenever we make a variable equal something, the variable always appears on the left side of the equal sign (=). The trick is that the variable can also appear on the right. This turns out to be quite useful, and you’ll see it in a lot of programs. The most common use is to increment a variable (increase it by a certain amount), like we just did, or the opposite, to decrement a variable (decrease it by a certain amount).

Transcript

  • 1. Introduction to Programming 1
  • 2. Where are we?• Web Technology Stack• 10 Minute Digression on Servers – Client/server model – Server Architecture – Server Software Background• The physical vs. logical – Where do my files live? – Where are they processed?• Introduction to Programming 2
  • 3. Web Technology Stack Data – What does it know? Behavior – What does it do? Behavior – What does it do? Presentation – What does it look like? Structure – What does this logically mean? Richness of theExperience
  • 4. Basic Server Architecture HTTP request ` HTTP response Web Browser Web Server Database Server A server is a computer PHPThis is the model! Script optimized to share resources, such as files, printers, webWhat is our physical sites, databases, andarchitecture? email, over a network.
  • 5. About Apache• The Apache http server project is an effort to develop and maintain an open source http server for modern operating systems (Linux, MS, OSX, …). The goal is to provide a secure, efficient, and extensible server that meets all standards• http://httpd.apache.org• Maintained by the Apache foundation
  • 6. Stats of Web Server types http://www.greatstatistics.com/serverstats.php
  • 7. What the Busiest 1M Websites use Totals for Active Servers Across All Domains - May 2010 nginx lighttpd 9% 0% Google 13% Apache Microsoft 59% 19%
  • 8. About PHP• PHP stood for Personal Home Page (like a shell script)• PHP now stands for PHP: Hypertext Preprocessor (yes, it’s a recursive definition)• PHP is HTML-centric and lives inside/along side HTML• PHP is used to generate dynamic web pages• PHP runs on the web server – When the user requests a dynamic web page (typically a .php file), the web server calls the PHP interpreter to read the requested file – The PHP interpreter parses the PHP commands (code) and typically generates an .html file, which is returned to the user – PHP can use data passed to it from the web page and access data in a database server
  • 9. About MySQL• MySQL is an open source database server that is available for all/most operating systems – SQL: Structured Query Language• Introduced in 1995 with version 3.23• Now, version 5.X• How open source is MySQL? – MySQL is owned by MySQL AB, a for profit firm – In 2008, Sun Microsystems acquires MySQL AB – In 2009, Oracle Corporation acquires Sun Microsystems
  • 10. Apache, PHP & MySQL• These open source products are easily installed by developers and used commercially• Your OS, Apache, MySQL, PHP – [X A M P]! – LAMP, WAMP, and MAMP• LAMP stacks are widely used to serve many content applications – Webmail, Blogs, Wikis, CMSs, etc.• Great for Virtual Hosting! – It is the server stack you get for $5/month from HostGator, GoDaddy, etc.
  • 11. Introduction to Programming• Slides are from a course teaching “young people” to program – Apologize if too elementary – Apologize if too advanced• There will be some opportunities to program – All examples are online and expectation is that you will run the examples and review them here and for homework 11
  • 12. What are the Attributes of Good Programmers?• Humility• Love of Learning• Detail-orientedness• Adaptability• Passion 12
  • 13. What is programming?• Very simply, programming means telling a computer to do something – Computers are dumb machines• A computer program is made up of a number of instructions – An instruction is a basic command given to a computer, usually to do a single, specific thing• Software is a program or a collection of programs that run on your computer – Sometimes the software runs on another computer that yours is connected to, like a web server. 13
  • 14. Programming Languages • Inside, all computers use a binary (0/1) language – Humans don’t speak binary very well • A programming language lets humans write in ways we “understand” – It is then translated into binary for the computer to use • There are lots of programming languages – HTML, CSS, Javascript, PHP, Pe rl, Python, Basic, C, C++, Java, etc. 14
  • 15. Vocabulary• Instructions – echo “Hello there!”;• Keywords – A keyword is a special word that is part of a programming language (also called a reserved word).• Coding/Editing – Writing a number of instructions that will perform a specific set of functions• Compiling – Converting the “human readable” instructions into the binary language that the computer understands• Executing – A fancy way of saying “running your program”• Interpreting – Sometimes, converting to binary and executiing takes place instruction by instruction, this is interpreting• Debugging – The process of finding errors (bugs) and correcting them 15
  • 16. What could go wrong?• Syntax bugs – Errors prior to execution (perhaps in your editor) – Syntax is the spelling and grammar rules for a programming language, a syntax error means that something is typed that just won’t work• Runtime bugs - Errors during execution – Runtime errors usually crash or stop the execution of your program with some clues, called a Traceback - like what file, line number, or even error 16
  • 17. *Class caveats & kludges*• All programming examples are on my server: – http://jamesmarcus.net/bwa/introprog• We will use our browsers as an output device to print simple results – our test jig! – We are not printing (real) web pages (yet) – We do not have the ability to get input values (yet)• The example PHP code is embedded in a comment tag and can be viewed using “View Source” – Examples must be changed to run on your web server 17
  • 18. The programming cycle• Edit • Edit and Save – Write code <1hello1.php>• Execute • Execute <1hello1.php> – Run a program – Browser: – Get a webpage http://localhost/bwa/int roprog/1hello1.php• Test & Debug – “Inspect what you • Test & Debug the expect” results – If you find a bug, back – Review results for a to editing! variety of inputs 18
  • 19. Our first PHP program• XAMPP Control – start web server!• Create in /htdocs/bwa/introprog the file <1hello1.php><?php print “Hello”; print “ and Hello to you.”;?>• Start and end of a PHP code block• A PHP code block• PHP instructions end with a ‘;’ 19
  • 20. Analyzing our first PHP program• File naming convention – Files that contain PHP code have a .php extension• PHP code blocks start and end with: – <?php and ?> – PHP instructions end with a ‘;’• PHP has two simple output statements: – print – echo• Using the browser as a text output device – Yet, it is still a browser• We can add simple text formatting. How? 20
  • 21. Playing with our first PHP program• Add simple text/HTML formatting to <1hello1.php> to create <1hello2.php>• Introduce errors to <1hello2.php> – Syntax errors: Where/when do syntax errors occur? – Runtime errors: Where/when do runtime errors occur?• Multiple code blocks: <1hello3.php> – What is going on in between the code blocks?: <1hello4.php> 21
  • 22. Memory & Variables - Remember Me!• Most all ‘useful’ programs: – Get input – Process the input – Produce output 22
  • 23. Feed Me, Seymour• The computer needs input – But in order to do something with the input, the computer has to remember it or keep it somewhere – The computer keeps things, including input (and the program itself), in its memory• Computer memory - a bunch of on/off switches – You can write to the memory (set the switches) – You can read from the memory (look at how the switches are set, without changing them) 23
  • 24. Naming Memory>> Teacher = “Mr. Morton” • What happened?>> print Teacher – We created a thing that is made up of (a string of) characters and gave it• What do you expect a name, Teacher from this “pseudo- • The equal sign ‘=‘ says code” code snippet? to assign or “make equal to” – You assigned the nameMr. Morton Teacher to the string of letters “Mr. Morton”<2teacher1.php> 24
  • 25. But wait… • It’s just like if someone said, “Write down your address.” – What would you write? 25
  • 26. Variables• Programmers do not have to think about how and where memory stores things (like the string of letters) – We assign a value to a name and then retrieve the value later• The name assigned to the value, like Teacher, is called a variable 26
  • 27. Types of Variables• Variables exist for – Strings (series of characters) – Numbers (integers, reals) – And other interesting stuff • Boolean: true/false • Groups of groups of letters or numbers – arrays!• Explicit variable declaration• Implicit variable declaration• And in between 27
  • 28. Using Variablesprint 5 + 3;$First = 5; <3variables1.php>$Second = 3;print $First + $Second;$Third = $First + $Second;print $Third; 28
  • 29. What’s her name?$MyTeacher = “Mrs. Goodyear”;$YourTeacher = $MyTeacher;print $MyTeacher; Mrs. Goodyearprint $YourTeacher; Mrs. Goodyear 29
  • 30. What if… 30
  • 31. What’s in a name?• In most modern programming languages, you can call a variable anything you want (well, almost):• It can be as long as you want• It can have letters and numbers in it, as well as special characters, like the underscore character (_)• It usually is case-sensitive (uppercase and lowercase matter) – Are teacher and TEACHER two different names?• It cannot have spaces• It may require a special starting character• In PHP, variables start with $, i.e., $Teacher• In other languages, read the documentation 31
  • 32. String Variables• A character, or series of characters (letters, numbers, or punctuation), is called a string – The way you state that you are making a string is to put quotes around the characters – PHP and other languages are not too fussy about whether you use single or double quotes$teacher = “Mr. Morton”;$teacher = „Mr. Morton‟; 32
  • 33. Numbers as Strings Strings as Numbers$first = 5;$second = 3;print $first + $second; <4concat1.php>$first = „5‟;$second = „3‟;print $first + $second;• Huh? Adding two strings? – Is that what we mean to do? 33
  • 34. Fancy word time: concatenate• It’s not really correct to say we want to “add” strings. (Though PHP lets us do so.)• When you put characters or strings together to make a longer string, we call it concatenation.• In PHP, two strings are concatenated by the dot ‘.’ operator: <4concat2.php> $newstring = $string1 . $string2; 34
  • 35. How variable is a variable?• Variables are called “variables” for a reason – It’s because they are . . . well . . . variable! – The value assigned to them can vary or change – Remember the MyTeacher example 35
  • 36. How variable is variable?$teacher = “Mr. Morton”;$teacher = “Mr. Smith”;$print teacher; 36
  • 37. The new me!• You can make a variable equal to itself:$Score = 7;$Score = $Score;• Big woop! It is just the same old me.$Score = $Score + 1;print $Score;• Woop, woop! It is a whole new me! <5increment1.php> 37
  • 38. Incrementing a variable 38
  • 39. Variable Wrap-up!• A variable can be reassigned (the tag can be stuck on a new thing) at any time by a program – One of the most common “bugs” in programming is changing the wrong variable or changing the right variable at the wrong time – One way to prevent this is to use variable names that are easy to remember and have contextual meaning • $t = Mr. Morton or $x1796vc47blahblah = Mr. Morton work but have no meaning 39
  • 40. Quiz• Once you have created a variable, can you change the value assigned to it?• With variable names, is $TEACHER the same as $TEACHEr?• Is ‘Blah’ the same as “Blah”?• Is ‘4’ the same as 4?• Is “10” a number or a string?• How do you tell PHP that a variable is string or a number? 40
  • 41. Basic Math• The four basic operations: – Addition: + – Subtraction: - – Multiplication: * – Division: /• Exponentiation [PHP: pow($base, $exponent)] – Raising a number to a power: 25= 2*2*2*2*2• Modulus [PHP: %] – Clock arithmetic• Increment and decrement $Score = $Score + 1; ++$Score; $Score += 1;• Really big numbers, really small numbers – Overflow, underflow, e-notation 41
  • 42. Order of Operation• PEMDAS – Parentheses – Exponents – Multiply – Divide – Add – Subtract• But be kind and use parentheses! – (2 + 3) * 4 = 20 – 2 + (3 * 4) = 14 42
  • 43. Data Types• So far, we have talked about three data types – Strings – Integers – Reals/decimals (floats)• Sometimes we have have to be careful about what types we use• Fancy words alert: Data type Conversion – A programming language will have explicit ways to convert from one data type to another, also called type casting! – Integer to float: $newFloat = (float) $oldInteger; – Float to integer: $newInteger = (int) $oldFloat; – String to float/integer: $number = $stringasnumber; 43
  • 44. Gotchas will getcha• Be careful casting an unknown fraction (float) to an integer print (int)( (0.1+0.7) * 10 );• Result is …• 7 Why?• There are many helper functions for casting: – floor() – ceil() – round() 44
  • 45. Quiz• What symbol does PHP use for multiplication?• How would you get the remainder for 8 / 3?• How would you get the integer part for 8 / 3?• What’s another way of calculating 6 * 6 * 6 * 6 in PHP? 45
  • 46. • End here. 46
  • 47. • Exercises 47
  • 48. Exercise 1• Write a short PHP program, as we just did, that will print three lines: – Your name – Your birth date – Your favorite color • My name is James Marcus. • I was born January 26, 1961. • My favorite color is blue. 48
  • 49. Exercise 2• Calculate the number of minutes in a week using variables – $DaysPerWeek – $HoursPerDay – $MinutesPerHour• How many minutes are there in a week in the Bilky Way, a near parallel galaxy, if there are 26 hours on a day there? 49