PHP - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

PHP

Description:

fred = 44; single quotes all the characters in the string are part of the string ... fred = rand(5, 10); $fred will hold an integer greater than or equal to ... – PowerPoint PPT presentation

Number of Views:147
Avg rating:3.0/5.0
Slides: 27
Provided by: sit4
Category:
Tags: php | fred

less

Transcript and Presenter's Notes

Title: PHP


1
PHP
  • SI 539
  • Fall 2004
  • Prof. Sandra Bartlett

2
What is PHP?
  • PHP Hypertext Pre-processor (formerly known as
    Personal Home Page)
  • Widely used
  • General-purpose
  • Server-side scripting language
  • Especially suited for Web development
  • Can be embedded in HTML

3
Advantages
  • Access a remote database
  • Script is invisible to the end user, unlike
    JavaScript (JavaScript) (PHP)
  • Browser needs no plug-in
  • Parts can be implemented in compiled languages
    like C/C
  • Thin client

4
Disadvantages
  • Web server does more work has to create the
    page as well as send it
  • If user saves the page, it looses its
    functionality
  • Pages may not load properly when you click the
    reload button
  • A bookmarked page may not work

5
What is a Programming Language
  • Makes it easier for people to tell the computer
    what to do
  • Translates English-like text into 0s and 1s
    that the computer can understand
  • Types
  • Compiled translate before running
  • Interpreted translate and run at the same time
  • Both

6
What kind is PHP?
  • Interpreted
  • Web server does the work (with JavaScript, the
    browser does the work)
  • Looks a lot like C, C, Java, JavaScript,
    Fortran, Pascal, etc.
  • What you need to learn
  • Vocabulary and syntax
  • Built-in functions

7
Comments
  • Help you and others read the source
  • Fix bugs
  • Make changes
  • Add new stuff
  • Types
  • // single line comment
  • / block comment
  • cant be nested /

8
Variables
  • Make you pages dynamic, not static
  • Hold information to use later
  • Only need to change the information once and it
    will be correct in all the places you use it

9
Variable Names
  • Case sensitive
  • Must be meaningful
  • Start with , then a letter, then any number of
    letters, digits, _, but not space
  • Examples
  • fred fred2
  • fred_fred fredFred (course style)

10
Creating Variables
  • var fred
  • fred now exists
  • fred has no value it is empty
  • mary hi
  • mary now exists
  • mary holds the string hi

11
Assign value
  • Tell computer what you want it to remember
  • is symbol for assignment
  • Put variable name on left of Put value on the
    right and a at the end
  • Examples
  • price 5
  • t2 Doctor

12
Kinds of Values (data types)
  • Numbers
  • Signed integers (-3 0 456344 etc.)
  • Floats (1.234 1.2e3 7E-10 )
  • Booleans
  • TRUE FALSE (not case sensitive)
  • Also can be treated as false 0 0 0.0 NULL
  • Also can be treated as true 1 -1 .3 fred
  • Strings

13
Floats
  • Size is platform dependent
  • the biggest and smallest values can be different
    on different machines
  • Precision
  • Sometimes floating point math comes out with the
    wrong answer
  • Not all floating point numbers can be exactly
    represented in the computer
  • 1/3
  • Round off error

14
Strings
  • Characters inside double quotes ( ) or single
    quotes ( )
  • Characters are all the things you can type on a
    keyboard
  • A b c etc.
  • 1 2 3 etc.
  • Space, tab, return, etc.
  • F1 delete etc.
  • Foreign language characters etc.

15
Assigning Values
  • fred 3.5
  • fred me!
  • fred -3.14159
  • fred TRUE
  • PHP variable are not typed any one variable can
    hold any thing.

16
Which Quotes For Strings?
  • fred 44
  • single quotes all the characters in the
    string are part of the string
  • this is fred
  • this is fred
  • double quotes
  • Understands escape characters
  • Expands variables

17
Escape Characters
  • Start with back slash \
  • \n newline
  • \t tab
  • \ (not part of a variable name)
  • \ double quote
  • \\ \
  • \ single quote (inside single quotes only)

18
Expanding Variables
  • fred 44
  • single quotes all the characters in the
    string are part of the string
  • this is fred
  • this is fred
  • double quotes variables are expanded
  • this is fred
  • this is 44

19
Expansion is GREEDY!!
  • Takes as many letters as it can for the variable
    name
  • food chocolate'
  • We ate foods. -gt We ate foods.
  • We ate foods. -gt no such variable
  • We ate foods. -gt We ate chocolates.

20
Implicit Type Conversion
  • foo 4"  // foo is string
  • foo foo 2  // foo is now an integer (6)
  • foo foo 1.3  // foo is now a float (3.3)
  • foo 5 "10 Little Piggies" // foo is
    integer (15)
  • foo foo "10 Small Pigs"  // foo is
    integer (25)

21
Explicit Type Conversion
  • foo 4"  // foo is string
  • foo (int) foo  // foo is now an integer (4)
  • foo (bool) foo// foo is now boolean
  • foo (string) foo // foo is now 1

22
How to Print
  • echo( ) - language construct () optional
  • Just does the printing
  • Can print multiple strings (dont use () for
    this)
  • print() - function () optional
  • Returns 1 always
  • Can only print one string

23
Print Example
  • print()ing in PHP

24
How Can the Server Tell PHP From HTML?
  • TAGS!!!! ?
  • For the course and XHTML
  • lt?php ?gt
  • Short tags, not always available
  • lt? ?gt
  • Also works (asp style)
  • ltscript language"php"gt lt/scriptgt

25
Example
  • Mixing PHP and HTML

26
For Lab
  • rand(int min, int max)
  • Built-in function
  • Returns a random integer between min and max,
    inclusive
  • fred rand(5, 10)
  • fred will hold an integer greater than or equal
    to 5 and less than or equal to 10
  • 5 6 7 8 9 10
Write a Comment
User Comments (0)
About PowerShow.com