week 5
TRANSCRIPT
Introduction to Programming
1
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
Web Technology Stack
Structure – What does this logically mean?
Presentation – What does it look like?
Behavior – What does it do?
Richness of the
Experience
Behavior – What does it do?
Data – What does it know?
Basic Server Architecture
Web Server
PHP
Script
Database Server
HTTP request
HTTP response`
Web Browser
A server is a computer optimized to share resources, such as files, printers, web sites, databases, and email, over a network.
This is the model!
What is our physical architecture?
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
Stats of Web Server types
http://www.greatstatistics.com/serverstats.php
What the Busiest 1M Websites use
Totals for Active Servers Across All Domains - May 2010
Apache
59%Microsoft
19%
13%
nginx
9%
lighttpd
0%
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
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
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.
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
What are the Attributes of Good Programmers?
• Humility
• Love of Learning
• Detail-orientedness
• Adaptability
• Passion
12
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
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,
Perl, Python, Basic, C, C++, Java, etc.
14
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
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
*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
The programming cycle
• Edit
– Write code
• Execute
– Run a program
– Get a webpage
• Test & Debug
– “Inspect what you expect”
– If you find a bug, back to editing!
• Edit and Save <1hello1.php>
• Execute <1hello1.php>
– Browser: http://localhost/bwa/introprog/1hello1.php
• Test & Debug the results
– Review results for a variety of inputs
18
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
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
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
Memory & Variables - Remember Me!
• Most all ‘useful’ programs:
– Get input
– Process the input
– Produce output
22
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
Naming Memory
>> Teacher = “Mr. Morton”
>> print Teacher
• What do you expect from this “pseudo-code” code snippet?
Mr. Morton
<2teacher1.php>
• What happened?– We created a thing that
is made up of (a string of) characters and gave it a name, Teacher
• The equal sign ‘=‘ says to assign or “make equal to” – You assigned the name
Teacher to the string of letters “Mr. Morton”
24
But wait…
• It’s just like if someone said, “Write down your address.”
– What would you write?
25
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
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
Using Variables
print 5 + 3;
$First = 5;
$Second = 3;
print $First + $Second;
$Third = $First + $Second;
print $Third;
28
<3variables1.php>
What’s her name?
$MyTeacher = “Mrs. Goodyear”;
$YourTeacher = $MyTeacher;
print $MyTeacher;
Mrs. Goodyear
print $YourTeacher;
Mrs. Goodyear
29
What if…
30
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
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
Numbers as StringsStrings as Numbers
$first = 5;
$second = 3;
print $first + $second;
$first = „5‟;
$second = „3‟;
print $first + $second;
• Huh? Adding two strings? – Is that what we mean to do?
33
<4concat1.php>
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:
$newstring = $string1 . $string2;
34
<4concat2.php>
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
How variable is variable?
$teacher = “Mr. Morton”;
$teacher = “Mr. Smith”;
$print teacher;
36
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!
37
<5increment1.php>
Incrementing a variable
38
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
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
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
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
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
Gotchas will getcha
• Be careful casting an unknown fraction (float) to an integerprint (int)( (0.1+0.7) * 10 );
• Result is …
• 7 Why?
• There are many helper functions for casting:– floor()
– ceil()
– round()
44
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
• End here.
46
• Exercises
47
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
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