0% found this document useful (0 votes)
21 views

Web Programming 05

The document provides an overview of PHP including that it is a popular server-side scripting language for dynamic web pages, how it is platform independent, and how PHP code is embedded in HTML documents. It also discusses PHP variables, arrays, forms, cookies, and connecting to and querying MySQL databases.

Uploaded by

youssefwaeel97
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Web Programming 05

The document provides an overview of PHP including that it is a popular server-side scripting language for dynamic web pages, how it is platform independent, and how PHP code is embedded in HTML documents. It also discusses PHP variables, arrays, forms, cookies, and connecting to and querying MySQL databases.

Uploaded by

youssefwaeel97
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Web Programming

CS333-CS644

Dr Safa’a Saleh

1
Lecture 5-A

2
3
4
PHP: Hypertext Preprocessor, has become the most
popular server-side scripting language for creating
dynamic web pages.

PHP is open source and platform independent—


implementations
 PHP exists for all major UNIX, Linux, Mac and Windows
operating systems.
 PHP also supports a large number of databases.

5
The power of the web resides
 Not only in serving content to users,
 But also, in responding to requests from users
 And generating web pages with dynamic content.
PHP code is embedded directly into text-based documents, such
as HTML, though these script segments are interpreted by a
server before being delivered to the client.
PHP script file names end with .php.
In PHP, code is inserted between the scripting delimiters
<?php and
?>.
PHP code can be placed anywhere in HTML5 markup, as long as
the code is enclosed in these delimiters.
Variables are preceded by a $ and are created the first time
they’re encountered.
PHP statements terminate with a semicolon (;).

6
Single-line comments which begin with two forward slashes
(//) or a pound sign (#).
 Text to the right of the delimiter is ignored by the interpreter.
 Multiline comments begin with delimiter /* and end with delimiter */.
When a variable is encountered inside a double-quoted ("")
string, PHP inserts the variable. In other words, PHP inserts
the variable’s value where the variable name appears in the
string.
PHP variables are loosely typed—they can contain different
types of data at different times.

7
8
Type conversions can be performed using function settype.
This function takes two arguments—a variable whose type is to
be changed and the variable’s new type.
Variables are typed based on the values assigned to them.
Function gettype returns the current type of its argument.
Calling function settype can result in loss of data.
 For example, doubles are truncated when they are converted to integers.
When converting from a string to a number, PHP uses the value of
the number that appears at the beginning of the string. If no
number appears at the beginning, the string evaluates to 0.
Another option for conversion between types is casting (or type
casting). Casting does not change a variable’s content—it creates
a temporary copy of a variable’s value in memory.
The concatenation operator (.) combines multiple strings.
A print statement split over multiple lines prints all the data that
is enclosed in its parentheses.

9
10
11
Function define creates a named constant.
It takes two arguments—the name and value of the constant.
An optional third argument accepts a boolean value that
specifies whether the constant is case insensitive—constants
are case sensitive by default.

12
13
14
15
16
Arrays are divided into elements that behave as individual
variables.
Array names, like other variables, begin with the $ symbol.
Individual array elements are accessed by following the array’s
variable name with an index enclosed in square brackets ([]).
Function count returns the total number of elements in the array.
Function array creates an array that contains the arguments
passed to it.
The first item in the argument list is stored as the first array element (index 0),
the second item is stored as the second array element and so on.
Arrays with nonnumeric indices are called associative arrays.
You can create an associative array using the operator =>, where
the value to the left of the operator is the array index and the
value to the right is the element’s value.

17
19.5 Arrays

18
19.5 Arrays

19
PHP provides functions for iterating through the
elements of an array.

Each array has a built-in internal pointer, which


points to the array element currently being
referenced.

Function reset sets the internal pointer to the first


array element.
Function key returns the index of the element
currently referenced by the internal pointer, and
function next moves the internal pointer to the next
element.

20
19.5 Arrays

21
19.5 Arrays

22
Superglobal arrays are associative arrays predefined by
PHP that hold variables obtained from user input, the
environment or the web server.
Arrays $_GET and $_POST retrieve information sent to
the server by HTTP get and post requests, respectively.

23
Using method = "post" appends form data to the
browser request that contains the protocol and the
requested resource’s URL.
Scripts located on the web server’s machine can
access the form data sent as part of the request.

Function die terminates script execution. The


function’s optional argument is a string, which is
printed as the script exits.

24
25
26
27
28
29
Function mysql_connect connects to the MySQL database.
It takes three arguments—
 the server’s hostname
 a username
 a password
and returns a database handle—a representation of PHP’s
connection to the database, or false if the connection fails.
Function mysql_select_db selects and opens the database
to be queried.
 The function returns true on success or false on failure.
To query the database, we call function mysql_query,
specifying the query string and the database to query.
 This returns a resource containing the result of the query, or false
if the query fails.
 It can also execute SQL statements such as INSERT or DELETE that
do not return results.
The mysql_error function returns any error strings from
the database.
mysql_close closes the connection to the database
specified in its argument.
The mysql_fetch_row function returns an array
containing the values for each column in the current row of
the query result ($result).
30
19.9 Reading from a Database

31
32
33
34
35
A cookie is a piece of information that’s stored by a server in
a text file on a client’s computer to maintain information
about the client during and between browsing sessions.
A server can access only the cookies that it has placed on the
client.
Function setcookie takes the name of the cookie to be set
as the first argument, followed by the value to be stored in
the cookie.
The optional third argument indicates the expiration date of
the cookie.
If no expiration date is specified, the cookie lasts only until
the end of the current session—that is, when the user closes
the browser. This type of cookie is known as a session
cookie, while one with an expiration date is a persistent
cookie.

36
If only the name argument is passed to function
setcookie, the cookie is deleted from the client’s
computer.
Cookies defined in function setcookie are sent to
the client at the same time as the information in
the HTTP header; therefore, setcookie needs to
be called before any other output
PHP creates the superglobal array $_COOKIE, which
contains all the cookie values indexed by their
names, similar to the values stored in array $_POST
when an HTML5 form is posted

37
38
39
40
41
42
43
44
The isset function determines whether the
$_POST array contains keys representing the
various form fields.
The notation$$variable specifies a variable
variable, which allows the code to reference
variables dynamically.
You can use this expression to obtain the value
of the variable whose name is equal to the value
of $variable.
The function mysql_real_escape_string
inserts a backslash (\) before any special
characters in the passed string.

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

You might also like