Вы находитесь на странице: 1из 20

PHP tutorial

contents
Introduction
Variables, statements and operators
Conditional statements and loops
Arrays and functions
Using files, cookies and sessions
Introduction-PHP
PHP:
a widely used general-purpose scripting language that is
especially suited for web development and can be
embedded into HTML
the main goal of the language is to allow web developers to
write dynamically generated web pages quickly
popular choice for data-driven web applications because of
its wide support for different database systems
PHP code is embedded inside a regular HTML document,
and is recognized and executed by the web server when the
document is requested through a browser
PHP code works independently of the users web browser
Is open-source server-side scripting language thats both
easy to learn and extremely powerful to use- why so popular
Introduction-PHP
PHP:
is available free of charge on the Internet, for a variety of
platforms and architectures(linux, Win, Mac)
is an interpreted language
is server side scripting language
Server side scripts run on the web server, therefore, can be
used to access server resources, such as databases or external
files, and perform more sophisticated tasks than regular client-
side scripting
The large majority of server-side scripts are related to either
getting information from the user and saving it
somewhere(DB), or retrieving information from somewhere(DB)
and presenting it
Current version of PHP 5.0
Introduction-PHP
PHP features:
Simplicity
Portability
Open source
Speed
Extensibility
XML and DB support
PHP originally stood for Personal Home Page Tools.
When PHP 3.0 was released, it was changed into a
recursive acronym meaning PHP: Hypertext
Preprocessor
Introduction-PHP
These are among the things you can do with server-side
scripts and an RDBMS:
Build a search engine that responds to user queries
Record user input provided through web forms and save it
for future reference
Create web sites that dynamically update themselves with
new content
Manage a blog (or more than one)
Process electronic payments and track customer orders
Build customized bar graphs, pie charts, and other
statistical reports from raw numeric data
Carry out online surveys and polls, and create reports of the
results
Variables, statements and operators

Embedding PHP in HTML


<?php
... PHP code ...
?>
short version:
<?
... PHP code
?>
Variables, statements and
operators
Example embedding php in html:
<html>
<head><basefont face="Arial"></head>
<body>
<?php
// print output
echo '<h2><i> Hello World</i></h2>';
?>
</body>
</html>
Variables, statements and
operators
Steps to run php file
We need to have web server first, since php is
executed on web server(e.g XAMPP control panel,
Apache Web server, Tomcat, etc)
Then save file filename.php in htdocs folder of
web server
Start web server
Use address of web server and name of php file to
execute the file(e.g http://localhost/filename.php
for XAMPP web server)
Variables, statements and
operators
When you requested the previous script through
your browser, the web server intercepted your
request and handed it off to PHP
PHP then parsed the script, executing the code
between the <?php...?> marks and replacing it
with the resulting output
The result was then handed back to the web
server and transmitted to the client
Because the output contained valid HTML, the
browser was able to render it for display to the
user
Variables, statements and
operators
a PHP script consists of one or more statements,
with each statement ending in a semicolon
Comments:
<?php
// this is a single-line comment
# so is this
/* and this is a
multiline
comment */
?>
Variables
Variables are the building blocks of any programming language.
A variable can be thought of as a programming construct used to
store both numeric and nonnumeric data.
The contents of a variable can be altered during program
execution, and variables can be compared and manipulated using
operators
PHP supports a number of different variable typesBooleans,
integers, floating point numbers, strings, arrays, objects,
resources, and NULL
the language can automatically determine variable type by the
context in which it is being used
Every variable has a name, which is preceded by a dollar ($)
symbol, and it must begin with a letter or underscore character,
optionally followed by more letters, numbers, and underscores
For example, $popeye, $one_day, and $INCOME are all valid PHP
variable names,
while $123 and $48hrs are invalid variable names
Variables
Variable names in PHP are case-sensitive; $count is different
from $Count or $COUNT
<html>
<head><basefont face="Arial"></head>
<body>
<<?php
// define variable
$answer = hello world
// print output
echo "<h2><i>$answer</i></h2>";
?>
</body>
</html>
Assigning value to
variables
To assign a value to a variable, use the assignment
operator, the equality (=) symbol.
This operator assigns a value (the right side of the
equation) to a variable (the left side).
The value being assigned need not always be
fixed; it could also be another variable, an
expression, or even an expression involving other
variables
e.g<?php
$age = $dob + 15;
?>
Saving Form input into
variable
First html form like this:
<html>
<head></head>
<body>
<form action="message.php" method="post">
Enter your message: <input type="text" name="msg" size="30">
<input type="submit" value="Send">
</form>
</body>
</html>
the method attribute of the <form> tag specifies the manner in
which form data will be submitted (POST), while the action attribute
specifies the name of the server-side script (message.php) that will
process the entered into the form.
Saving form input into
variable
Message.php like this:
<?php
// retrieve form data in a variable
$input = $_POST['msg'];
// print it
echo "You said: <i>$input</i>";
?>
To access the value of the form variable, use its
name inside the $_POST container
The $_GET and $_POST variables are a special type
of animal called an array
Detecting Variable type
Simply use gettype(var_name) method
<?php
// define variables
$auth = true;
$age = 27;
$name = 'Bobby';
$temp = 98.6;
// returns "string"
echo gettype($name);
// returns "boolean"
echo gettype($auth);
// returns "integer"
echo gettype($age);
// returns "double"
echo gettype($temp);
?>
String values
String values enclosed in double quotes are automatically
parsed for variable names; if variable names are found,
they are automatically replaced with the appropriate
variable value
<?php
$identity = 'James Bond';
$car = 'BMW';
// this would contain the string "James Bond drives a BMW"
$sentence = "$identity drives a $car";
// this would contain the string "$identity drives a $car"
$sentence = '$identity drives a $car';
?>
operators
Using Conditional
Statements and Loops

Вам также может понравиться