Cosc 4750 - PowerPoint PPT Presentation

1 / 51
About This Presentation
Title:

Cosc 4750

Description:

String String data is always in double quotes ... echo $car; /body /html Constants. Don't use $, instead use a special keyword to create them ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 52
Provided by: ridic
Category:
Tags: cosc

less

Transcript and Presenter's Notes

Title: Cosc 4750


1
Cosc 4750
  • Beginning PHP

2
PHP
  • PHP gt "PHP Hypertext Preprocessor"
  • Designed for the web
  • Cross-platform Runs on both UNIX and windows
    (with everything installed correctly)
  • PHP is mix of PHP langauge and HTML code
  • Runs on the Sever-side, specifically on the web
    server
  • Light weight compared to PERL, since it runs
    inside the web-server itself.
  • In many ways PHP is perl, but stripped down with
    most of the "web" modules already installed.

3
NOTE
  • I'm assuming you know several things
  • HTML
  • SQL
  • PERL
  • I'll use PERL to explain PHP.
  • Also, all files are saved with the .php extension
    on them, instead of .html

4
Example PHP code
  • file helloworld.php
  • lthtmlgt
  • ltheadgt
  • lttitlegtPHP Testlt/titlegt
  • lt/headgt
  • ltbodygt
  • lt?php
  • echo "ltpgtHello Worldlt/pgt"
  • ?gt
  • lt/bodygt
  • lt/htmlgt
  • Output to browser
  • lthtmlgt
  • ltheadgt
  • lttitlegtPHP Testlt/titlegt
  • lt/headgt
  • ltbodygt
  • ltpgtHello Worldlt/pgt
  • lt/bodygt
  • lt/htmlgt

5
Comments
  • PHP has single line comments like C
  • uses the // to start a comment
  • // This is a comment
  • var 10 //bah bah

6
variables
  • like PERL
  • All variables start with a
  • Unlike PERL
  • Variables can be typed or typeless
  • types
  • String String data is always in double
    quotes
  • integer, double, array (numeric and hash index),
    object, and unknown type
  • Boolean (value true or false)
  • Displaying variables
  • echo variablename

7
Using Variables
  • Remember this is inside html code!
  • lthtmlgtltbodygt
  • lt? php
  • CarType "Ford"
  • enginesize "3.0"
  • car cartype . " " . enginesize
  • echo car
  • ?gt
  • lt/bodygt
  • lt/htmlgt

8
Constants
  • Don't use , instead use a special keyword to
    create them
  • define(constantname, value)
  • Examples
  • define("FREEZING", 0)
  • define(INDEPENCEDAY, "4th of July")
  • echo "Independence Day is ". INDEPENDENCEDAY

9
Variables and operators
  • Same as with PERL
  • With typed variables
  • numeric operators for numeric data types
  • string operators for string data types
  • such as the dot is used for concatenation with
    strings
  • can not str str1 str2

10
Getting data from forms
  • ltform action"test.php" methodGETgt
  • lt! Example input box--gt
  • ltinput NAME"var1" Type"text"gt
  • lt/formgt
  • OR
  • ltform action"test.php" methodPOSTgt
  • ltinput NAME"var1" Type"text"gt
  • lt! Example input box--gt
  • lt/formgt
  • The name comes into the PHP script as a variable
  • lt?php
  • echo var1
  • ?gt
  • NOTE The name must be the same including case!

11
Radio buttons and Check Box
  • ltinput name"choice" type"Checkbox"gt
  • check boxes have the value of on or off
  • unless you use the value tag
  • ltinput name"choice" type"Checkbox"
    value"Computers"gt
  • Same with Radio buttons.

12
Filling arrays from forms
  • ltform methodget action"test2.php"gt
  • ltinput namearr typetextgt
  • ltinput namearr typetextgt
  • ltinput typesubmitgt
  • lt/formgt
  • In test2.php
  • arr0 has the value of the first input and
    arr1 has the value from the second.

13
HTML code input
  • To better "secure" your input you can use a the
    function HTMLSpecialChars(value)
  • This convert html codes into text for display.
  • string HTMLSpecialChars("ltBgthilt/Bgt")
  • echo string
  • Will display ltBgthilt/bgt to the web browser instead
    of hi.

14
Control Structures
  • If statements
  • Again like PERL
  • if (boolean value) statements
  • elseif (boolean value) statements
  • else statements
  • Boolean operators are the same as in PERL, except
    you don't have to make any distinctions between
    string and numerical operators
  • lt lt gt gt !, AND, , OR, , ! (NOT)

15
Control Structures (2)
  • switch statement
  • NOT in perl, just like the C/C statement
  • switch (variable)
  • case value1 statements break
  • case value2 statements break
  • default statements
  • like C/C you can leave off the break

16
Control Structures (3)
  • Loops
  • while (boolean expression)
  • statements
  • do
  • statements
  • while (boolean expression)

17
Control Structures (4)
  • for loops
  • for (set loop counter test loop counter
    increment/decrement loop counter)
  • statements

18
Arrays structure
  • foreach structure
  • similar to PERL
  • foreach (array As variable)
  • statements
  • foreach (array As arrIndex gt var)
  • statements
  • second format makes the array index value
    available in the loop as well.

19
More on Arrays
  • Initialization (with number index values)
  • arr0 "value1"
  • arr1 "value2"
  • OR allow PHP fill in the numbers
  • arr "value1"
  • arr "value2"
  • OR
  • arr array ("value1", "value2", , "valueX")
  • OR (for hashing)
  • arr"v1" "value1"
  • arr"v2" "vavlue2"

20
More on Arrays (2)
  • Iterating through non-sequential arrays and hash
    arrays.
  • list and each functions
  • while ( list(indexvalue, element) each(
    array) )
  • echo "The value of element indexvalue is
    element"

21
More on Arrays (3)
  • sorting (with index as numbers)
  • sort(array)
  • sorting hash array
  • asort(hasharray)
  • NOTE you can use sort on hashes, but the keys
    (string indices) are replaced with numbers.
  • reverse alphabetical order sort
  • rsort(array) and arsort(hasharray)
  • ksort(hasharray)
  • Sorts based on the keys, instead of the element
    contents.

22
More on Arrays (4)
  • array_push() and array_pop()
  • Allows you to use an array as a stack.
  • implode and explode functions
  • string implode("delimiter", array)
  • like join statement in PERL
  • arr explode("delimiter", string)
  • like split statement in PERL

23
functions
  • function functionname (parameters)
  • statements
  • return
  • OR
  • return value //or variable
  • Calling the function
  • functionname(parameters)
  • variable functionname(parameters)

24
functions (2)
  • pass by value
  • function f1 (var)
  • // var can be altered but won't change the
  • // calling variable
  • pass by reference
  • function f2 (var)
  • //var value will now change the calling
    variable

25
functions (3)
  • default parameter values
  • function f3 (var2)
  • //f3 can be called with no parameters
  • // and var will have the value of 2
  • parameter order, since you don't have pass values
    to the parameters
  • function f4(v12, v2) statements
  • f4(12) //v1 12, v2 0

26
functions (4)
  • Scope of variables
  • Variables "declared" outside of functions are
    global
  • Variables "declared" inside functions are local
    variables to the function
  • "declared" is the first occurrence of the
    variable.
  • creating global variables in functions uses the
    keyword global
  • global variable
  • static variables
  • static variable
  • the variable will retain it's value between
    function calls.

27
functions (5)
  • final notes.
  • You can nest functions inside functions
  • the inner function will cause an error
    (redeclaration error) on the second time the
    outer function is called.
  • Recursion
  • allowed

28
including files
  • include("filename")
  • Execute a separate PHP script at the point in the
    code
  • include commonly used PHP functions that can be
    called by this script.
  • define variables and/or constants, etc
  • Can be text or html that will be displayed.

29
File I/O
  • similar to PERL file I/O
  • open
  • filehandle fopen("filename", mode)
  • fp fopen("data.txt","r") //read from file
    data.txt
  • modes
  • "r" read only
  • "w" for writing only
  • "a" append, create if doesn't exist.
  • close
  • fclose (filehandle)

30
File I/O (2)
  • reading
  • variable fread(filehandle,bytes)
  • data fread(fp, 20) // read 20 bytes from the
    file
  • Writing
  • fwrite(filehandle, data)
  • fwrite(fp, "abc")

31
File I/O (3)
  • c fgetc(fp) read file one character at a time
  • feof(fp) returns true if it the end of the file
  • arr file(filename) read in an entire file
  • Each line is an element in the arr
  • copy(filename1,filename2) copy f1 to f2
  • rename(f1, f2) rename f1 to f2
  • unlink(filename) del filename
  • may not work in windows
  • use system or exec function (see PERL for
    descriptions)

32
Directory I/O
  • chdir() change directory
  • rmdir() delete directory
  • mkdir() delete directory
  • opendir() open a directory for "reading"
  • readdir() read the directory
  • closedir() close the directory

33
PHP database connectivity
  • Using mysql as the database to connect to, since
    it included with our Redhat installation.
  • First you need to connect to the database
  • link_id mysql_connect("localhost", "username",
    "password")
  • like mysql command in unix
  • mysql u username p password h host

34
PHP database connectivity (2)
  • Now we need to select the database to use
  • mysql_select_db("database", link_id)
  • Where link_id is from the mysql_connect command
  • mysql command
  • mysqlgt use database

35
PHP database connectivity (3)
  • After finishing with the database, you need to
    close the connection
  • mysql_close(link_id)

36
PHP commands For lab 6
  • lt?php
  • // make the connection
  • link_id mysql_connect("localhost", "testacc",
    "csteach")
  • //choose the database
  • mysql_select_db("webacc", link_id)
  • //now ready to retreive,modify, or delete data
  • mysql_close(link_id)
  • ?gt

37
Accessing the database
  • PHP uses one command to send SQL commands to the
    database (for mysql)
  • result mysql_query("SQL command")
  • Another to retreive results (from say a select),
    one row of data at a time
  • arr mysql_fetch_row(result)
  • //gets first row of data
  • arr mysql_fetch_row(result)
  • //gets next row of data
  • etc..

38
With a select
  • result mysql_query("Select from test")
  • while(query_data mysql_fetch_row(result))
  • echo "query_data0 query_data1
    queary_data2 ltBRgt"
  • //prints to the browser the contexts of the table
    test, with three rows.

39
With a select (2)
  • Or can use the field names, for better
    readablity.
  • result mysql_query("Select from test")
  • while(query_data mysql_fetch_row(result))
  • echo "query_data"name" query_data"ip"
    queary_data"owner" ltBRgt"
  • //prints to the browser the contexts of the table
    test, with three rows.

40
With an Insert
  • sql "Insert into test values
    (csteach31,129.72.218.41,cosc)"
  • mysql_query(sql)
  • NOTE lack of return value.
  • NOTE inserting "odd" characters such as ' as in
    I'm won't work.
  • There is a function to fix it called addslashes
  • var addslashes("I'm") // result "I\'m"
  • Removed with the stripslashes() later.

41
Delete and update
  • Use the same php command
  • sql "delete from test where name 'k2' "
  • mysql_query(sql)
  • sql "update test set ip'129.72.14.123' where
    name 'esig' "
  • mysql_query(sql)

42
results from insert/update/delete
  • mysql will tell you how many how many rows were
    effected by insert, update or delete
  • There is a function to get that information from
    mysql
  • var mysql_affected_rows(link_id)
  • var contains a the number of effected rows.

43
example
  • A php script that displays the contains of a the
    database on a webpage
  • lt?php
  • // make the connection
  • link_id mysql_connect("localhost", "testacc",
    "csteach")
  • //choose the database
  • mysql_select_db("webacc", link_id)
  • result mysql_query("Select from test")
  • while(query_data mysql_fetch_row(result))
  • echo "query_data"name" query_data"ip"
    queary_data"owner" ltBRgt"
  • mysql_close(link_id)
  • ?gt

44
PHP and e-mail
  • Sending e-mail in PHP is very simple
  • On windows you need set a mailer,
  • command mail( )
  • It takes 4 arguments
  • The intended recipient
  • subject line
  • body of the e-mail
  • Extra headers (optional)
  • Example
  • lt?php
  • mail("seker_at_uwyo.edu","test", "this is test
    message")
  • ?gt

45
Extra headers field
  • Add header like
  • From
  • Reply-To
  • Cc
  • Bcc
  • Others you want to create.
  • You need to include the \r\n between the extra
    headers.

46
Example
  • lt?php
  • to "seker_at_uwyo.edu"
  • subject "Test message"
  • body "This is a test message"
  • headers "Fromseker_at_uwyo.edu\r\nCc
    venkat_at_uwyo.edu"
  • mail(to,subject,body,headers)
  • ?gt

47
Example with html pages
  • mail.html
  • lthtmlgtltbodygt
  • ltform methodpost action"e-mail.php"gt
  • to ltinput nameto size20gt
  • From ltinput namefrom size20gt
  • subject ltinput namesub size30gt
  • Message lttextarea namebody rows5 cols30gt
  • lt/formgt
  • e-mail.php
  • lthtmlgt
  • ltbodygt
  • lt?php
  • header "From ".from
  • mail(to,sub,body,headers)
  • ?gt
  • You e-mail to to has been sent.
  • lt/bodygt
  • lt/htmlgt

48
Attachments
  • You can also set it up to accept attachments
  • but we don't have the time to spend on the
    details.

49
additional features built in PHP
  • additional features
  • sessions
  • use cookies
  • Persistence of user data between HTTP requests
  • PHP and XML
  • generating graphics
  • Working with the browser client and getting info.
  • And many functions I skipped over.
  • Such as regular expression functions, network
    functions, etc.

50
Reference
  • Information was found from the web and the book
  • Beginning PHP4, Choi ..., WROX, 2003

51
Q
A
Write a Comment
User Comments (0)
About PowerShow.com