Sie sind auf Seite 1von 28

PHP

HYPERTEXT
PREPROCESSOR
PHP Introduction

PHP is a recursive acronym for “PHP:


Hypertext Preprocessor” -- It is a widely-
used open source general-purpose scripting
language that is especially suited for web
development and can be embedded into
HTML.
PHP Introduction
Server Side Programming
• Provides web site developers to utilise resources
on the web server
• Non-public resources do not require direct
access from the clients
• Allows web sites to be client agnostic (unless
JavaScript is used also)
• Most server side programming script is
embedded within markup (although does not
have to be, sometimes better not to)
PHP - What is it / does it do?
• PHP: PHP Hypertext Pre-processor
• Programming language that is interpreted
and executed on the server
• Execution is done before delivering
content to the client
• Contains a vast library of functionality
that programmers can harness
• Executes entirely on the server, requiring
no specific features from the client
PHP - What is it / does it do?
• Static resources such as regular HTML are simply output
to the client from the server
• Dynamic resources such as PHP scripts are processed on
the server prior to being output to the client
• PHP has the capability of connecting to many database
systems making the entire process transparent to the
client
Web Page Request Load PHP File

PHP Engine –
Run Script
HTML Response PHP Results
User Web Server
PHP Language Basics

• Look at the building blocks of the PHP


language
– Syntax and structure
– Variables, constants and operators
– Data types and conversions
– Decision making IF and switch
– Interacting with the client application (HTML
forms)
PHP - Syntax and Structure
• PHP is similar to C
• All scripts start with <?php and with with ?>
• Line separator: ; (semi-colon)
• Code block: { //code here } (brace brackets)
• White space is generally ignored (not in strings)
• Comments are created using:
– // single line quote
– /* Multiple line block quote */
• Precedence
– Enforced using parentheses
– E.g. $sum = 5 + 3 * 6; // would equal 23
PHP - Variables

• Prefixed with a $
• Assign values with = operator
• Example: $author = “Trevor Adams”;
• No need to define type
• Variable names are case sensitive
– $author and $Author are different
PHP - Constants

• Constants are special variables that cannot


be changed
• Use them for named items that will not
change
• Created using a define function
– define(‘milestokm’, 1.6);
– Used without $
– $km = 5 * milestokm;
PHP - Operators
• Standard mathematical operators
– +, -, *, / and % (modulus)
• String concatenation with a period (.)
– $car = “SEAT” . “ Altea”;
– echo $car; would output “SEAT Altea”
• Basic Boolean comparison with “==”
– Using only = will overwrite a variable value
– Less than < and greater than >
– <= and >= as above but include equality
PHP - Data Types
• PHP is not strictly typed
– Different to JAVA where all variables are declared
• A data type is either text or numeric
– PHP decides what type a variable is
– PHP can use variables in an appropriate way
automatically
• E.g.
– $vat_rate = 0.175; /* VAT Rate is numeric */
– echo $vat_rate * 100 . “%”; //outputs “17.5%”
– $vat_rate is converted to a string for the purpose of
the echo statement
• Object, Array and unknown also exist as types,
PHP Operators
Operators are used to operate on values.
There are four classifications of operators:

> Arithmetic
> Assignment
> Comparison
> Logical
PHP Conditional Statements
•> if statement - use this statement to
execute some code only if a specified
condition is true
•> if...else statement - use this statement
to execute some code if a condition is true
and another code if the condition is false
•> if...elseif....else statement - use this
statement to select one of several blocks of
code to be executed
•> switch statement - use this statement to
PHP Arrays

•> An array variable is a storage area


holding a number or text. The problem is, a
variable will hold only one value.
•> An array is a special variable, which can
store multiple values in one single variable.
PHP Arrays
• In PHP, there are three kind of arrays:
• > Numeric array - An array with a
numeric index
• > Associative array - An array where
each ID key is associated with a value
• > Multidimensional array - An array
containing one or more arrays
PHP Loops
•> while - loops through a block of code
while a specified condition is true
•> do...while - loops through a block of
code once, and then repeats the loop as long
as a specified condition is true
•> for - loops through a block of code a
specified number of times
•> foreach - loops through a block of code
for each element in an array
PHP Loops - While
The while loop executes a block of code
while a condition is true. The example below
defines a loop that starts with
i=1. The loop will
continue to run as
long as i is less
than, or equal to 5.
i will increase by 1
each time the loop
runs:
PHP Loops - While
PHP Loops – Do ... While
The do...while statement will always execute
the block of code once, it will then check the
condition, and repeat the loop while the
condition is true.

The next example defines a loop that starts


with i=1. It will then increment i with 1, and
write some output. Then the condition is
checked, and the loop will continue to run as
long as i is less than, or equal to 5:
PHP Loops – Do ... While
PHP Loops - For
• Parameters:
• > init: Mostly used to set a counter (but
can be any code to be executed once at
the beginning of the loop)
• > condition: Evaluated for each loop
iteration. If it evaluates to TRUE, the loop
continues. If it evaluates to FALSE, the
loop ends.
• > increment: Mostly used to increment a
counter (but can be any code to be
PHP Loops - For
The example below defines a loop that starts
with i=1. The loop will continue to run as
long as i is less than, or equal to 5. i will
increase by 1 each time the loop runs:
PHP Functions
A function will be executed by a call to the
function.

> Give the function a name that reflects what


the function does
> The function name can start with a letter
or underscore (not a number)
PHP Forms - $_GET Function

•> The built-in $_GET function is used to


collect values from a form sent with
method="get".
•> Information sent from a form with the
GET method is visible to everyone (it will be
displayed in the browser's address bar) and
has limits on the amount of information to
send (max. 100 characters).
PHP Forms - $_GET Function

Notice how the URL carries the information after the file name.
PHP Forms - $_POST Function
•> The built-in $_POST function is used to
collect values from a form sent with
method="post".
•> Information sent from a form with the
POST method is invisible to others and has
no limits on the amount of information to
send.
•> Note: However, there is an 8 Mb max size
for the POST method, by default (can be
PHP Forms - $_POST Function

And here is what the code of action.php might look like:

Das könnte Ihnen auch gefallen