PHP Tutorial - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

PHP Tutorial

Description:

... the string is contained in double quotes. 12/26/09. 6. PHP Variables ... Echo 'Jacky Yo'; Echo 'My name is '; printName(); 12/26/09. 24. PHP Functions (Cont. ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 34
Provided by: george85
Category:

less

Transcript and Presenter's Notes

Title: PHP Tutorial


1
PHP Tutorial
  • I-Ming Lee

2
PHP Hypertext Preprocessor
  • PHP is a server-side interpreted programming
    language used by web developers to create dynamic
    website which can interact and change based on
    users actions. PHP is used in over 3.5 million
    websites. It exists as one of the most widely
    used dynamic web scripting languages.

3
PHP Functions
  • PHP runs on different platforms (Windows, Linux,
    Unix, MacOS).
  • PHP is a Server-Side Scripting language.
  • PHP scripts are executed on the server.
  • PHP code is embedded in HTML code.
  • PHP has syntax very similar to other high-level
    languages (C, Python, Perl, ASP).
  • PHP is an Open-Source software and is free to
    download and use.
  • PHP is designed to perform very well with other
    open source web solutions (MySQL, Apache).

4
PHP Installation
  • Download PHP http//www.php.net/downloads.php
  • Download MySQL Database http//www.mysql.com/down
    loads/index.html
  • Download Apache Server http//httpd.apache.org/do
    wnload.cqi

5
PHP Syntax
  • CODE
  • lt?php
  • echo hello world!
  • //prints the string hello world!
  • ?gt
  • All PHP code starts and ends with lt?php or lt? and
    ?gt tags
  • Comments all begin with //
  • echo is for output and the string is contained in
    double quotes.

6
PHP Variables
  • Variables are used for storing a values, like
    text strings, numbers or arrays.
  • When a variable is declared, it can be used over
    and over again in users script.
  • All variable in PHP start with a sign symbol.

7
PHP Variable Example
  • Creating a variable containing a string, and a
    variable containing a number
  • lt?php
  • txt Hello World!
  • x 16
  • ?gt

8
Naming Rules for Variables
  • A variable name must start with a letter or an
    underscore _
  • A variable name can only contain alpha- numeric
    characters and underscore (a-z, A-Z, 0-9, _).
  • A variable name should not contain spaces. If a
    variable name is more than one word, it should be
    separated with an _ (my_txt), or with
    capitalization (myTxt).

9
PHP String Variables
  • The PHP script assigns the text Hello World to
    a string variable called txt
  • lt?php
  • txt Hello World
  • Echo txt
  • ?gt
  • The output Hello World

10
PHP String Variables (Cont.)
  • The concatenation operator (.) is used to put two
    string values together.
  • lt?php
  • txt1 Hello World!
  • txt2 Whats up!
  • echo txt1 . . txt2
  • ?gt
  • The output Hello World! Whats up!

11
PHP String Variables (Cont.)
  • The strlen() function is used to return the
    length of a string.
  • lt?php
  • echo strlen(Hello World!)
  • ?gt
  • The output 12.

12
PHP String Variables (Cont.)
  • The strpos() function is used to search for
    character within a string. If a match is found,
    it will return the position of the first match.
    If not found, it will return FALSE.
  • lt?php
  • echo strpos(Hello World!, World)
  • ?gt
  • The output 6
  • Note that the first position in the string is 0.

13
PHP Operators
  • Arithmetic Operators
  • Assignment Operators

14
PHP Operators (Cont.)
  • Assignment operators
  • Comparison operators

15
PHP IfElse Statements
  • The if statement
  • lt?php
  • d date(D)
  • if (d Fri) echo Have a nice weekend
  • ?gt
  • The ifelse statement
  • lt?php
  • d date(D)
  • if (d Fri) echo Have a nice weekend
  • else echo Have a nice day!
  • ?gt

16
PHP IfElse Statement (Cont.)
  • The ifelseifelse statement
  • lt?php
  • d date(D)
  • if (d Fri)
  • echo Have a nice weekend
  • elseif (d Sun)
  • echo Have a nice Sunday!
  • else echo Have a nice day!
  • ?gt

17
PHP Switch Statement
  • Use the switch statement to select one of many
    blocks of code to be executed.
  • lt?php
  • Switch (x)
  • case 1 echo Number 1 break
  • case 2 echo Number 2 break
  • case 3 echo Number 3 break
  • default echo No number match
  • ?gt

18
PHP Arrays
  • Numeric Arrays
  • //the index are automatically assigned
  • cars array(Honda, Volvo,, BMW,
    Toyota)
  • //assign the index manually
  • cars0 Honda
  • cars1 Volvo
  • cars2 BMW
  • cars3 Toyota

19
PHP Arrays (Cont.)
  • Associative Arrays each ID key is associated
    with a value.
  • ages array(Petergt32, Johngt30,
    Raygt28)
  • OR
  • agesPeter 32
  • agesJohn 30
  • agesRay 28

20
PHP Arrays (Cont.)
  • Multidimensional arrays
  • families array
  • (
  • Griffin gt array
  • ( Peter, Lois, Megan),
  • Quagmire gt array
  • (Glenn),
  • Brown gt array
  • (Cleveland,Junior)
  • )
  • echo Is.familiesGriffin1.in Griffin
    family?
  • Output Is Lois in Griffin family?

21
PHP While Loop
  • While Loop
  • lt?php
  • i 0
  • num 10
  • While (ilt num)
  • echo i
  • i
  • ?gt
  • DoWhile Loop
  • lt?php
  • i 0
  • num 10
  • do
  • echo i
  • i
  • while (iltnum)
  • ?gt

22
PHP For Loops
  • The for Loop
  • lt?php
  • For(i0 ilt5 i)
  • echo i
  • ?gt
  • The foreach loop is used to loop thru arrays.
  • lt?php
  • xarray(one,two, three)
  • Foreach(x as value)
  • echo value.
  • ?gt

23
PHP Functions
  • Create PHP function
  • Give the function a name that reflects what the
    function does.
  • The function name can start with a letter or
    underscore. No number.

lt?php Function printName() Echo Jacky
Yo Echo My name is printName() ?gt
24
PHP Functions (Cont.)
  • Adding parameter
  • lt?php
  • function printName(fname)
  • echo fname.Yo.ltbr/gt
  • Echo My name is
  • printNmae(Jacky)
  • ?gt

25
PHP Functions (Cont.)
  • To let a function return a value, use the return
    statement.
  • lt?php
  • function add (x, y)
  • total x y
  • return total
  • echo 1 10 .add(1,10)
  • ?gt

26
PHP Forms and User Input
  • The PHP _GET and _POST variables are used to
    retrieve information from forms, like user input.
  • The most important thing to notice when dealing
    with HTML forms and PHP is that any form element
    in an HTML page will automatically be available
    to your PHP scripts.

27
PHP Forms and User Input (Cont.)
  • The example contains an HTML form with two input
    fields and a submit button
  • lthtmlgt
  • ltbodygt
  • ltform actionwelcome.php methodpostgt
  • Nameltinput typetext namefname/gt
  • Ageltinput typetext nameage/gt
  • ltinput typesubmit/gt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

28
PHP Forms and User Input (Cont.)
  • After fill out the form and click on submit
    button, the form data is sent to a PHP file,
    called welcome.php
  • //welcome.php
  • lthtmlgt
  • ltbodygt
  • Welcome lt?php echo _POSTfname ?gt! ltbr/gt
  • You are lt?php echo _POSTage ?gt years old.
  • lt/bodygt
  • lt/htmlgt

29
PHP _GET Function
  • The build-in _GET function is used to collect
    values in a form with methodget.
  • ltform actionwelcome.php methodgetgt
  • Nameltinput typetext namefname/gt
  • Ageltinput typetext nameage/gt
  • ltinput typesubmit/gt
  • lt/formgt
  • Clicks the submit button, the URL sent to the
    server could look like this
  • http//localhost/welcome.php?fnameJackyage28

30
PHP _POST Function
  • The built-in _POST function is used to collect
    values from a form with methodpost.
  • ltform actionwelcome.php methodpostgt
  • Nameltinput typetext namefname/gt
  • Ageltinput typetext nameage/gt
  • ltinput typesubmit/gt
  • lt/formgt
  • Clicks the submit button, the URL sent to the
    server could look like this
  • http//localhost/welcome.php

31
PHP _GET and _POST Function
  • Methodget should not be used when sending
    passwords or other sensitive information. It is
    possible to bookmark the page because the
    variables are displayed in the URL. The variable
    values cannot exceed 100 characters.
  • Methodpost is invisible to others and has no
    limits on the amount of information to send. It
    is not possible to bookmark the page.

32
PHP _REQUEST Function
  • The built-in _REQUEST function contains the
    contents of both _GET, _POST, and _COOKIE. It
    can be used to collect form data sent with both
    the GET and POST methods.
  • Welcome lt?php echo _REQUESTfname ?gt! ltbr /gt
  • You are lt?php echo _REQUESTage ?gt years old.

33
References
  • W3schools.com Learn PHP http//www.w3schools.com
    /php/default.asp
  • Wikipedia PHP http//en.wikipedia.org/wiki/PHP
  • PHP A simple tutorial Manual
    http//php.net/manual/en/tutorial.php
Write a Comment
User Comments (0)
About PowerShow.com