SlideShare ist ein Scribd-Unternehmen logo
1 von 49
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
                       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 the
Experience
Basic Server Architecture


                     HTTP request

               `     HTTP response


       Web Browser                   Web Server      Database Server


                                                  A server is a computer
                                        PHP
This is the model!                      Script    optimized to share
                                                  resources, such as
                                                  files, printers, web
What is our physical                              sites, databases, and
architecture?                                     email, over a network.
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


                  nginx   lighttpd
                   9%        0%
     Google
      13%




                                                     Apache
  Microsoft                                           59%
    19%
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, Pe
                rl, 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                         • 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
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”   • 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 name
Mr. Morton                       Teacher to the string of
                                 letters “Mr. Morton”
<2teacher1.php>
                                                            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;
                                <3variables1.php>
$Second = 3;
print $First + $Second;
$Third = $First + $Second;
print $Third;
                                                    28
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 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
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
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!
                                     <5increment1.php>

                                                     37
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 integer
  print (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

Weitere ähnliche Inhalte

Was ist angesagt?

Web Development with Python and Django
Web Development with Python and DjangoWeb Development with Python and Django
Web Development with Python and DjangoMichael Pirnat
 
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...Michael Pirnat
 
Intro To Puppet.Key
Intro To Puppet.KeyIntro To Puppet.Key
Intro To Puppet.KeyWork
 
Performance tuning with zend framework
Performance tuning with zend frameworkPerformance tuning with zend framework
Performance tuning with zend frameworkAlan Seiden
 
Strategic Modernization with PHP on IBM i
Strategic Modernization with PHP on IBM iStrategic Modernization with PHP on IBM i
Strategic Modernization with PHP on IBM iAlan Seiden
 
DB2 and PHP in Depth on IBM i
DB2 and PHP in Depth on IBM iDB2 and PHP in Depth on IBM i
DB2 and PHP in Depth on IBM iAlan Seiden
 
Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016Clark Everetts
 
Website design & developemet
Website design & developemetWebsite design & developemet
Website design & developemetApurva Tripathi
 
C++ programming languages lectures
C++ programming languages lectures C++ programming languages lectures
C++ programming languages lectures jabirMemon
 
Zend con 2016 bdd with behat for beginners
Zend con 2016   bdd with behat for beginnersZend con 2016   bdd with behat for beginners
Zend con 2016 bdd with behat for beginnersAdam Englander
 
Content Management Systems and Refactoring - Drupal, WordPress and eZ Publish
Content Management Systems and Refactoring - Drupal, WordPress and eZ PublishContent Management Systems and Refactoring - Drupal, WordPress and eZ Publish
Content Management Systems and Refactoring - Drupal, WordPress and eZ PublishJani Tarvainen
 
A Bug Hunter's Perspective on Unix Drivers
A Bug Hunter's Perspective on Unix DriversA Bug Hunter's Perspective on Unix Drivers
A Bug Hunter's Perspective on Unix DriversJeremy Brown
 
PHP on IBM i Tutorial
PHP on IBM i TutorialPHP on IBM i Tutorial
PHP on IBM i TutorialZendCon
 
Create a welcoming development environment on IBM i
Create a welcoming development environment on IBM iCreate a welcoming development environment on IBM i
Create a welcoming development environment on IBM iAlan Seiden
 
Hhvm and wordpress
Hhvm and wordpressHhvm and wordpress
Hhvm and wordpressMark Kelnar
 
Making CLI app in ruby
Making CLI app in rubyMaking CLI app in ruby
Making CLI app in rubyHuy Do
 
From Zero to ZF: Your first zend framework project on ibm i
From Zero to ZF: Your first zend framework project on ibm iFrom Zero to ZF: Your first zend framework project on ibm i
From Zero to ZF: Your first zend framework project on ibm iAlan Seiden
 
Php internal architecture
Php internal architecturePhp internal architecture
Php internal architectureElizabeth Smith
 
EuroMPI 2013 presentation: McMPI
EuroMPI 2013 presentation: McMPIEuroMPI 2013 presentation: McMPI
EuroMPI 2013 presentation: McMPIDan Holmes
 

Was ist angesagt? (20)

Web Development with Python and Django
Web Development with Python and DjangoWeb Development with Python and Django
Web Development with Python and Django
 
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
 
Intro To Puppet.Key
Intro To Puppet.KeyIntro To Puppet.Key
Intro To Puppet.Key
 
Performance tuning with zend framework
Performance tuning with zend frameworkPerformance tuning with zend framework
Performance tuning with zend framework
 
Strategic Modernization with PHP on IBM i
Strategic Modernization with PHP on IBM iStrategic Modernization with PHP on IBM i
Strategic Modernization with PHP on IBM i
 
DB2 and PHP in Depth on IBM i
DB2 and PHP in Depth on IBM iDB2 and PHP in Depth on IBM i
DB2 and PHP in Depth on IBM i
 
Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016Php Dependency Management with Composer ZendCon 2016
Php Dependency Management with Composer ZendCon 2016
 
Website design & developemet
Website design & developemetWebsite design & developemet
Website design & developemet
 
C++ programming languages lectures
C++ programming languages lectures C++ programming languages lectures
C++ programming languages lectures
 
Zend con 2016 bdd with behat for beginners
Zend con 2016   bdd with behat for beginnersZend con 2016   bdd with behat for beginners
Zend con 2016 bdd with behat for beginners
 
Content Management Systems and Refactoring - Drupal, WordPress and eZ Publish
Content Management Systems and Refactoring - Drupal, WordPress and eZ PublishContent Management Systems and Refactoring - Drupal, WordPress and eZ Publish
Content Management Systems and Refactoring - Drupal, WordPress and eZ Publish
 
A Bug Hunter's Perspective on Unix Drivers
A Bug Hunter's Perspective on Unix DriversA Bug Hunter's Perspective on Unix Drivers
A Bug Hunter's Perspective on Unix Drivers
 
PHP on IBM i Tutorial
PHP on IBM i TutorialPHP on IBM i Tutorial
PHP on IBM i Tutorial
 
Create a welcoming development environment on IBM i
Create a welcoming development environment on IBM iCreate a welcoming development environment on IBM i
Create a welcoming development environment on IBM i
 
Hhvm and wordpress
Hhvm and wordpressHhvm and wordpress
Hhvm and wordpress
 
Making CLI app in ruby
Making CLI app in rubyMaking CLI app in ruby
Making CLI app in ruby
 
From Zero to ZF: Your first zend framework project on ibm i
From Zero to ZF: Your first zend framework project on ibm iFrom Zero to ZF: Your first zend framework project on ibm i
From Zero to ZF: Your first zend framework project on ibm i
 
Php internal architecture
Php internal architecturePhp internal architecture
Php internal architecture
 
EuroMPI 2013 presentation: McMPI
EuroMPI 2013 presentation: McMPIEuroMPI 2013 presentation: McMPI
EuroMPI 2013 presentation: McMPI
 
[HK Roni] C Programming Lectures
[HK Roni] C Programming Lectures[HK Roni] C Programming Lectures
[HK Roni] C Programming Lectures
 

Andere mochten auch

Week 6
Week 6Week 6
Week 6A VD
 
Mc Gladrey Health Care Services Overview
Mc Gladrey Health Care Services OverviewMc Gladrey Health Care Services Overview
Mc Gladrey Health Care Services OverviewLinkedInLeo
 
Kalanchoe (adans) taxonomy
Kalanchoe (adans)   taxonomyKalanchoe (adans)   taxonomy
Kalanchoe (adans) taxonomyanfelpa
 
Cadete a santa_catalina_030312
Cadete a santa_catalina_030312Cadete a santa_catalina_030312
Cadete a santa_catalina_030312sdlasalle
 
Infantil a la_victoria
Infantil a la_victoriaInfantil a la_victoria
Infantil a la_victoriasdlasalle
 
Week 8
Week 8Week 8
Week 8A VD
 
Primer pase inf.c y inf. b (15/01/2012)
Primer pase inf.c y inf. b (15/01/2012)Primer pase inf.c y inf. b (15/01/2012)
Primer pase inf.c y inf. b (15/01/2012)sdlasalle
 

Andere mochten auch (7)

Week 6
Week 6Week 6
Week 6
 
Mc Gladrey Health Care Services Overview
Mc Gladrey Health Care Services OverviewMc Gladrey Health Care Services Overview
Mc Gladrey Health Care Services Overview
 
Kalanchoe (adans) taxonomy
Kalanchoe (adans)   taxonomyKalanchoe (adans)   taxonomy
Kalanchoe (adans) taxonomy
 
Cadete a santa_catalina_030312
Cadete a santa_catalina_030312Cadete a santa_catalina_030312
Cadete a santa_catalina_030312
 
Infantil a la_victoria
Infantil a la_victoriaInfantil a la_victoria
Infantil a la_victoria
 
Week 8
Week 8Week 8
Week 8
 
Primer pase inf.c y inf. b (15/01/2012)
Primer pase inf.c y inf. b (15/01/2012)Primer pase inf.c y inf. b (15/01/2012)
Primer pase inf.c y inf. b (15/01/2012)
 

Ähnlich wie Week 5

Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python ProgrammingAkhil Kaushik
 
Php training in bhubaneswar
Php training in bhubaneswar Php training in bhubaneswar
Php training in bhubaneswar litbbsr
 
Php training in bhubaneswar
Php training in bhubaneswar Php training in bhubaneswar
Php training in bhubaneswar litbbsr
 
Introduction to PHP.pptx
Introduction to PHP.pptxIntroduction to PHP.pptx
Introduction to PHP.pptxMarianJRuben
 
computer languages
computer languagescomputer languages
computer languagesRajendran
 
ProgFund_Lecture_1_Introduction_to_Programming.pdf
ProgFund_Lecture_1_Introduction_to_Programming.pdfProgFund_Lecture_1_Introduction_to_Programming.pdf
ProgFund_Lecture_1_Introduction_to_Programming.pdflailoesakhan
 
Week01 jan19 introductionto_php
Week01 jan19 introductionto_phpWeek01 jan19 introductionto_php
Week01 jan19 introductionto_phpJeanho Chu
 
Putting Compilers to Work
Putting Compilers to WorkPutting Compilers to Work
Putting Compilers to WorkSingleStore
 
Php hypertext pre-processor
Php   hypertext pre-processorPhp   hypertext pre-processor
Php hypertext pre-processorSiddique Ibrahim
 
Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1Mohd Harris Ahmad Jaal
 
Embedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals masterEmbedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals masterHossam Hassan
 
Compiler Design Introduction
Compiler Design IntroductionCompiler Design Introduction
Compiler Design IntroductionKuppusamy P
 
Week01 jan19 introductionto_php
Week01 jan19 introductionto_phpWeek01 jan19 introductionto_php
Week01 jan19 introductionto_phpJeanho Chu
 

Ähnlich wie Week 5 (20)

Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
 
Php training in bhubaneswar
Php training in bhubaneswar Php training in bhubaneswar
Php training in bhubaneswar
 
Php training in bhubaneswar
Php training in bhubaneswar Php training in bhubaneswar
Php training in bhubaneswar
 
Introduction to PHP.pptx
Introduction to PHP.pptxIntroduction to PHP.pptx
Introduction to PHP.pptx
 
Php unit i
Php unit i Php unit i
Php unit i
 
computer languages
computer languagescomputer languages
computer languages
 
Intro to Dynamic Web Pages
Intro to Dynamic Web PagesIntro to Dynamic Web Pages
Intro to Dynamic Web Pages
 
JAVA INTRODUCTION - 1
JAVA INTRODUCTION - 1JAVA INTRODUCTION - 1
JAVA INTRODUCTION - 1
 
ProgFund_Lecture_1_Introduction_to_Programming.pdf
ProgFund_Lecture_1_Introduction_to_Programming.pdfProgFund_Lecture_1_Introduction_to_Programming.pdf
ProgFund_Lecture_1_Introduction_to_Programming.pdf
 
PHP ITCS 323
PHP ITCS 323PHP ITCS 323
PHP ITCS 323
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
Week01 jan19 introductionto_php
Week01 jan19 introductionto_phpWeek01 jan19 introductionto_php
Week01 jan19 introductionto_php
 
Putting Compilers to Work
Putting Compilers to WorkPutting Compilers to Work
Putting Compilers to Work
 
Php hypertext pre-processor
Php   hypertext pre-processorPhp   hypertext pre-processor
Php hypertext pre-processor
 
Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1
 
Embedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals masterEmbedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals master
 
Compiler Design Introduction
Compiler Design IntroductionCompiler Design Introduction
Compiler Design Introduction
 
Getting started with PHP on IBM i
Getting started with PHP on IBM iGetting started with PHP on IBM i
Getting started with PHP on IBM i
 
Learning to code
Learning to codeLearning to code
Learning to code
 
Week01 jan19 introductionto_php
Week01 jan19 introductionto_phpWeek01 jan19 introductionto_php
Week01 jan19 introductionto_php
 

Mehr von A VD

Part 2
Part 2Part 2
Part 2A VD
 
Contest
ContestContest
ContestA VD
 
Part 2
Part 2Part 2
Part 2A VD
 
Week 7
Week 7Week 7
Week 7A VD
 
Part 2
Part 2Part 2
Part 2A VD
 
Part 2
Part 2Part 2
Part 2A VD
 
Week 5
Week 5Week 5
Week 5A VD
 
Intro toprogramming part2
Intro toprogramming part2Intro toprogramming part2
Intro toprogramming part2A VD
 
Week 4
Week 4Week 4
Week 4A VD
 
Week 3
Week 3Week 3
Week 3A VD
 
Week 1
Week 1Week 1
Week 1A VD
 
Week 2
Week 2Week 2
Week 2A VD
 

Mehr von A VD (12)

Part 2
Part 2Part 2
Part 2
 
Contest
ContestContest
Contest
 
Part 2
Part 2Part 2
Part 2
 
Week 7
Week 7Week 7
Week 7
 
Part 2
Part 2Part 2
Part 2
 
Part 2
Part 2Part 2
Part 2
 
Week 5
Week 5Week 5
Week 5
 
Intro toprogramming part2
Intro toprogramming part2Intro toprogramming part2
Intro toprogramming part2
 
Week 4
Week 4Week 4
Week 4
 
Week 3
Week 3Week 3
Week 3
 
Week 1
Week 1Week 1
Week 1
 
Week 2
Week 2Week 2
Week 2
 

Kürzlich hochgeladen

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 

Kürzlich hochgeladen (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 

Week 5

  • 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 the Experience
  • 4. Basic Server Architecture HTTP request ` HTTP response Web Browser Web Server Database Server A server is a computer PHP This is the model! Script optimized to share resources, such as files, printers, web What is our physical sites, databases, and architecture? 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 name Mr. 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 Variables print 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. Goodyear print $YourTeacher; Mrs. Goodyear 29
  • 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
  • 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
  • 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

Hinweis der Redaktion

  1. Separation of presentation and structure (CSS and HTML);Separation of behavior and structure (JavaScript and HTML);Separation of behavior and presentation (JavaScript and CSS)
  2. Different languages run on the different components.Browser: HTML, CSS, JavascriptWeb Server: PHP, ASP, JSP, CFDatabase Server: SQL
  3. What could be wrong? Not the write keyword, used the keyword incorrectly, logic problems, and lots of other reasons.
  4. 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).
  5. 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.
  6. But you do have to use the same kind of quotes at the start and the end of the string.
  7. 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).