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

Advanced-Web-System

The document provides an introduction to PHP, a server-side scripting language essential for web development. It covers PHP's history, advantages, syntax, variable types, and methods for handling user input through GET and POST. The tutorial aims to establish a foundational understanding of PHP for students and professionals in the software engineering field.

Uploaded by

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

Advanced-Web-System

The document provides an introduction to PHP, a server-side scripting language essential for web development. It covers PHP's history, advantages, syntax, variable types, and methods for handling user input through GET and POST. The tutorial aims to establish a foundational understanding of PHP for students and professionals in the software engineering field.

Uploaded by

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

Click to edit Master title style

Advanced
Web System
E m m a n u e l T. F l o r e s , M I T

1
Click to edit Master title style

Introduction
Intro to Web Development: PHP

2 2
Click
PHP to edit Master title style

The PHP Hypertext Preprocessor (PHP) is a programming


language that allows web developers to create dynamic content
that interacts with databases. PHP is basically used for
developing web based software applications. This tutorial helps
you to build your base with PHP.

3 3
Click
PHP to edit Master title style

PHP started out as a small open source project that evolved as more and more people found out how
useful it was. Rasmus Lerdorf unleashed the first version of PHP way back in 1994.
• PHP is a MUST for students and working professionals to become a great Software Engineer specially
when they are working in Web Development Domain. I will list down some of the key advantages of
learning PHP:
• PHP is a recursive acronym for "PHP: Hypertext Preprocessor".
• PHP is a server side scripting language that is embedded in HTML. It is used to manage dynamic
content, databases, session tracking, even build entire e-commerce sites.
• It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase,
Informix, and Microsoft SQL Server.
• PHP is pleasingly zippy in its execution, especially when compiled as an Apache module on the Unix
side. The MySQL server, once started, executes even very complex queries with huge result sets in
record-setting time.
• PHP supports a large number of major protocols such as POP3, IMAP, and LDAP. PHP4 added support
for Java and distributed object architectures (COM and CORBA), making n-tier development a
possibility for the first time.
4 4
• PHP is forgiving: PHP language tries to be as forgiving as possible.
Click
Commonto edit
usesMaster
of PHP title style
As mentioned before, PHP is one of the most widely used
language over the web. I'm going to list few of them here:

• PHP performs system functions, i.e. from files on a system it can create,
open, read, write, and close them.
• PHP can handle forms, i.e. gather data from files, save data to a file,
through email you can send data, return data to the user.
• You add, delete, modify elements within your database through PHP.
• Access cookies variables and set cookies.
• Using PHP, you can restrict users to access some pages of your
website.
• It can encrypt data.

5 5
Click to edit Master title style
Hello World

If you examine the HTML output of the above example,


you'll notice that the PHP code is not present in the file
sent from the server to your Web browser. All of the PHP
present in the Web page is processed and stripped from
the page; the only thing returned to the client from the
Web server is pure HTML output.
6 6
All PHP code must be included inside one of the three
Click
PHP -to edit Master
Syntax title- style
Overview Escaping to PHP
The PHP parsing engine needs a way to differentiate PHP code
from other elements in the page. The mechanism for doing so is
known as 'escaping to PHP'. There are four ways to do this −

Canonical PHP tags


The most universally effective PHP tag style is −

If you use this style, you can be positive that your


tags will always be correctly interpreted.

7 7
Click
PHP -to edit Master
Syntax title- style
Overview Escaping to PHP
ASP-style tags
ASP-style tags mimic the tags used by Active Server Pages to
delineate code blocks. ASP-style tags look like this −

To use ASP-style tags, you will need to set the


configuration option in your php.ini file.

HTML script tags


HTML script tags look like this

8 8
Click
PHP -to edit Master
Syntax title style
Overview

Commenting PHP Code


A comment is the portion of a program that exists only for the
human reader and stripped out before displaying the programs
result. There are two commenting formats in PHP.

1. Single-line comments − They are generally used for short


explanations or notes relevant to the local code. Here are
the examples of single line comments.

9 9
Click
PHP -to edit Master
Syntax title style
Overview

Commenting PHP Code

2.Multi-lines comments − They are generally used to provide


pseudocode algorithms and more detailed explanations when
necessary. The multiline style of commenting is the same as in
C.
Here are the example of multi lines comments.

10
10
Click
PHP -to edit Master
Syntax title style
Overview

PHP is whitespace insensitive


• Whitespace is the stuff you type that is typically invisible on
the screen, including spaces, tabs, and carriage returns (end-
of-line characters).
• PHP whitespace insensitive means that it almost never
matters how many whitespace characters you have in a
row.one whitespace character is the same as many such
characters.
• For example, each of the following PHP statements that
assigns the sum of 2 + 2 to the variable $four is equivalent −

11
11
Click
PHP -to edit Master
Syntax title style
Overview

PHP is case sensitive


This will produce the following result:

12
12
Click
PHP -to edit Master
Syntax title style
Overview

Statements are expressions terminated by semicolons:

A statement in PHP is any expression that is followed by a semicolon


(;).Any sequence of valid PHP statements that is enclosed by the PHP tags
is a valid PHP program. Here is a typical statement in PHP, which in this
case assigns a string of characters to a variable called $greeting

13
13
Click
PHP -to edit Master
Variable Typestitle style

The main way to store information in the middle of a PHP program is by using a variable.
Here are the most important things to know about variables in PHP.
• All variables in PHP are denoted with a leading dollar sign ($).
• The value of a variable is the value of its most recent assignment.
• Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be
evaluated on the right.
• Variables can, but do not need, to be declared before assignment.
• Variables in PHP do not have intrinsic types - a variable does not know in advance whether it will be used to
store a number or a string of characters.
• Variables used before they are assigned have default values.
• PHP does a good job of automatically converting types from one to another when necessary.
• PHP variables are Perl-like.

14
14
Click
PHP -to edit Master
Variable Typestitle style

PHP has a total of eight data types which we use to construct our variables:
• Integers − are whole numbers, without a decimal point, like 4195.
• Doubles − are floating-point numbers, like 3.14159 or 49.1.
• Booleans − have only two possible values either true or false.
• NULL − is a special type that only has one value: NULL.
• Strings − are sequences of characters, like 'PHP supports string operations.'
• Arrays − are named and indexed collections of other values.
• Objects − are instances of programmer-defined classes, which can package up both other
kinds of values and functions that are specific to the class.
• Resources − are special variables that hold references to resources external to PHP (such as
database connections).

15
15
Click
PHP -to edit Master
Variable Typestitle style
- Integers

They are whole numbers, without a decimal point, like 4195. They are the simplest type .they
correspond to simple whole numbers, both positive and negative. Integers can be assigned to
variables, or they can be used in expressions.

16
16
Click
PHP -to edit Master
Variable Typestitle style
- Doubles

They like 3.14159 or 49.1. By default, doubles print with the minimum number of decimal places
needed. For example, the code −

17
17
Click
PHP -to edit Master
Variable Typestitle style
- Boolean

They have only two possible values either true or false. PHP provides a couple of constants
especially for use as Booleans: TRUE and FALSE, which can be used like so -

18
18
Click
PHP -to edit Master
Variable Typestitle style
- Boolean

Interpreting other types as Booleans


Here are the rules for determine the "truth" of any value not already of the Boolean type −
• If the value is a number, it is false if exactly equal to zero and true otherwise.
• If the value is a string, it is false if the string is empty (has zero characters) or is the string "0",
and is true otherwise.
• Values of type NULL are always false.
• If the value is an array, it is false if it contains no other values, and it is true otherwise. For an
object, containing a value means having a member variable that has been assigned a value.
• Valid resources are true (although some functions that return resources when they are
successful will return FALSE when unsuccessful).
• Don't use double as Booleans.

19
19
Click to edit Master title style
PHP - Boolean

Each of the following variables has the truth value embedded in its name when it is used in a
Boolean context.

20
20
Click to edit Master title style
PHP - NULL

NULL is a special type that only has one value: NULL. To give a variable the NULL value, simply
assign it like this −

The special constant NULL is capitalized by convention, but actually it is case insensitive; you
could just as well have typed −

A variable that has been assigned NULL has the following properties −
• It evaluates to FALSE in a Boolean context.
• It returns FALSE when tested with IsSet() function.
21
21
Click to edit Master title style
PHP - Strings

They are sequences of characters, like "PHP supports string operations".


NOTE − Built-in string functions is given in function reference PHP String Functions
Following are valid examples of string:

22
22
Click
PHP -to edit Master title style
Strings

There are no artificial limits on string length - within the bounds of available memory, you ought
to be able to make arbitrarily long strings.
Strings that are delimited by double quotes (as in "this") are preprocessed in both the following
two ways by PHP:
• Certain character sequences beginning with backslash (\) are replaced with special characters
• Variable names (starting with $) are replaced with string representations of their values.

The escape-sequence replacements are:


• \n is replaced by the newline character
• \r is replaced by the carriage-return character
• \t is replaced by the tab character
• \$ is replaced by the dollar sign itself ($)
• \" is replaced by a single double-quote (")
• \\ is replaced by a single backslash (\) 23
23
Click
PHP -to editConcatenation
String Master title style
Operator

To concatenate two string variables together, use the dot (.)


operator:

If we look at the code above you see that we used the


Output: concatenation operator two times. This is because we
had to insert a third string.

Between the two string variables we added a string


with a single character, an empty space, to separate
the two variables.
24
24
Click
PHP –to edit Master
String title
Using the stylefunction
strlen()
The strlen() function is used to find the length of a string.
Let's find the length of our string "Hello world!" −

Output:

The length of a string is often used in loops or other functions, when it is important to know
when the string ends. (i.e. in a loop, we would want to stop the loop after the last character
in the string)
25
25
Click
PHP –to edit Master
String title
Using the stylefunction
strpos()
The strpos() function is used to search for a string or character
within a string.
If a match is found in the string, this function will return the
position of the first match. If no match is found, it will return
FALSE.
Let's see if we can find the string "world" in our string −

Output:

As you see the position of the string "world" in our string is


position 6. The reason that it is 6, and not 7, is that the
first position in the string is 0, and not 1.

26
26
Click to edit
PHP - GET Master
& POST title style
Methods
There are two ways the browser client can send information to
the web server.
• The GET Method
• The POST Method

Before the browser sends the information, it encodes it using a scheme called
URL encoding. In this scheme, name/value pairs are joined with equal signs and
different pairs are separated by the ampersand.

Spaces are removed and replaced with the + character and any other nonalphanumeric
characters are replaced with a hexadecimal values. After the information is encoded it is
sent to the server.

27
27
Click to edit
PHP - The GETMaster
Method title style
The GET method sends the encoded user information
appended to the page request. The page and the encoded
information are separated by the ? character.

• The GET method produces a long string that appears in your server logs, in the browser's
Location: box.
• The GET method is restricted to send upto 1024 characters only.
• Never use GET method if you have password or other sensitive information to be sent to the
server.
• GET can't be used to send binary data, like images or word documents, to the server.
• The data sent by GET method can be accessed using QUERY_STRING environment variable.
• The PHP provides $_GET associative array to access all the sent information using GET method.

28
28
Click to edit
PHP - The GETMaster
Method title style
The GET method sends the encoded user information
appended to the page request. The page and the encoded
information are separated by the ? character.

• The GET method produces a long string that appears in your server logs, in the browser's
Location: box.
• The GET method is restricted to send upto 1024 characters only.
• Never use GET method if you have password or other sensitive information to be sent to the
server.
• GET can't be used to send binary data, like images or word documents, to the server.
• The data sent by GET method can be accessed using QUERY_STRING environment variable.
• The PHP provides $_GET associative array to access all the sent information using GET method.

29
29
Click to edit
PHP - The GETMaster
Method title style
The GET method sends the encoded user information
appended to the page request. The page and the encoded
information are separated by the ? character.

• The GET method produces a long string that appears in your server logs, in the browser's
Location: box.
• The GET method is restricted to send upto 1024 characters only.
• Never use GET method if you have password or other sensitive information to be sent to the
server.
• GET can't be used to send binary data, like images or word documents, to the server.
• The data sent by GET method can be accessed using QUERY_STRING environment variable.
• The PHP provides $_GET associative array to access all the sent information using GET method.

30
30
Click to edit
PHP - The POSTMaster
Methodtitle style
The POST method transfers information via HTTP headers. The
information is encoded as described in case of GET method and
put into a header called QUERY_STRING.

• The POST method does not have any restriction on data size to be sent.
• The POST method can be used to send ASCII as well as binary data.
• The data sent by POST method goes through HTTP header so security depends on HTTP
protocol. By using Secure HTTP you can make sure that your information is secure.
• The PHP provides $_POST associative array to access all the sent information using POST
method.

31
31
Click to edit Master title style
PHP - Functions
PHP functions are similar to other programming languages. A
function is a piece of code which takes one more input in the
form of parameter and does some processing and returns a
value.

There are two parts which should be clear to you −


• Creating a PHP Function
• Calling a PHP Function

32
32
Click to edit Master
PHP Creating title style
PHP Function
PHP functions are similar to other programming
languages. A function is a piece of code which takes
one more input in the form of parameter and does
some processing and returns a value.

• Its very easy to create your own PHP function.


Suppose you want to create a PHP function which will
simply write a simple message on your browser when
you will call it.

• Following example creates a function called


writeMessage() and then calls it just after creating it.

• Note that while creating a function its name should


start with keyword function and all the PHP code
should be put inside { and } braces as shown in the
following example below −

33
33
Click to edit Master title style
PHP Functions with Parameters

PHP gives you option to pass your parameters inside a function. You can pass as many as parameters your
like.
These parameters work like variables inside your function. Following example takes two integer parameters
and add them together and then print them.

34
34

You might also like