Introducing php
A Server Scripting Language
What Is php?
Stands for PHP: Hypertext Processor
Recursive acronym
“a powerful tool for making dynamic and interactive Web
pages.” –w3schools.com
PHP runs on different platforms (Windows, Linux, Unix, Mac
OS X, etc.)
PHP is compatible with almost all servers used today (Apache,
IIS, etc.)
PHP has support for a wide range of databases
PHP is free. Download it from the official PHP resource:
www.php.net
PHP is easy to learn and runs efficiently on the server side
What Can PHP Do?
PHP can generate dynamic page content
PHP can create, open, read, write, and close files on the
server
PHP can collect form data
PHP can send and receive cookies
PHP can add, delete, modify data in your database
PHP can restrict users to access some pages on your
website
PHP can encrypt data
With PHP you are not limited to output HTML. You can
output images, PDF files, and even Flash movies.You can
also output any text, such as XHTML and XML.
How To Use php?
PHP files can contain text, HTML, JavaScript code, and
PHP code
PHP code are executed on the server, and the result is
returned to the browser as plain HTML
PHP files have a default file extension of ".php"
Two parts
PHP source code
HTML page with embedded PHP to display output
PHP Syntax
The PHP script is executed on the server, and the plain
HTML result is sent back to the browser.
The default file extension for PHP files is ".php".
A PHP file normally contains HTML tags, and some PHP
scripting code.
A PHP script can be placed anywhere in the document.
A PHP script starts with <?php and ends with ?>:
<?php
// PHP code goes here
?>
Embedded PHP Script Example
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
More PHP Syntax
Each code line in PHP must end with a semicolon. The
semicolon is a separator and is used to distinguish one
set of instructions from another.
With PHP, there are two basic statements to output text
in the browser: echo and print.
PHP Variables
Variables are "containers" for storing information:
Example
<?php
$x=5;
$y=6;
$z=$x+$y;
echo $z;
?>
Much Like Algebra
x=5
y=6
z=x+y
In algebra we use letters (like x) to hold values (like 5).
From the expression z=x+y above, we can calculate the value of z to be 11.
In PHP these letters are called variables.
PHP Variables
As with algebra, PHP variables can be used to hold values (x=5) or
expressions (z=x+y).
Variable can have short names (like x and y) or more descriptive names
(age, carname, totalvolume).
Rules for PHP variables:
A variable starts with the $ sign, followed by the name of the variable
A variable name must begin with a letter or the underscore character
A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
A variable name should not contain spaces
Variable names are case sensitive ($y and $Y are two different variables)
Both PHP statements and PHP variables are case-sensitive.
Creating (Declaring) PHP Variables
PHP has no command for declaring a variable.
A variable is created the moment you first assign a value
to it:
$txt="Hello world!";
$x=5;
After the execution of the statements above, the variable
txt will hold the value Hello world!, and the variable x
will hold the value 5.
Note: When you assign a text value to a variable, put
quotes around the value.
PHP Arrays
An array stores multiple values in one single variable:
Example
<?php
$cars[0]="Volvo";
$cars[1]="BMW";
$cars[2]="Toyota";
echo "I like " . $cars[0] . ", " .
$cars[1] . " and " . $cars[2] . ".";
?>
What is an Array?
An array is a special variable, which can hold more than one value at a time.
If you have a list of items (a list of car names, for example), storing the cars
in single variables could look like this:
$cars1="Saab";
$cars2="Volvo";
$cars3="BMW";
However, what if you want to loop through the cars and find a specific one?
And what if you had not 3 cars, but 300?
The solution is to use an array!
An array can hold many values under a single name, and you can access the
values by referring to an index number.
In PHP, there are three types of arrays:
Numeric array - Arrays with numeric index
Associative array - Arrays with named keys
Multidimensional array - An array containing one or more arrays
Associative Arrays
Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array:
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
or:
$age['Peter']="35";
$age['Ben']="37";
$age['Joe']="43";
The named keys can then be used in a script:
Example
<?php
$age['Peter']="35";
$age['Ben']="37";
$age['Joe']="43";
echo "Peter is " . $age['Peter'] . " years old.";
?>
PHP foreach Loop
The foreach loop is used to loop through arrays.
Syntax
foreach ($array as $value)
{
code to be executed;
}
For every loop iteration, the value of the current array
element is assigned to $value (and the array pointer is
moved by one) - so on the next loop iteration, you'll be
looking at the next array value.
PHP foreach Loop Example
The following example demonstrates a loop that will print the values of the
given array:
<html>
<body>
<?php
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br>";
}
?>
</body>
</html>
Output:
one
two
three
PHP Built-In Functions
The real power of PHP comes from its functions.
In PHP, there are more than 700 built-in functions.
Examples:
fetch() Fetches the next row from a result set
query($sql) Executes an SQL statement, returning a result
set as a PDOStatement object
PHP Examples
Download source code from online textbook website:
www.sitepoint.com/books/phpmysql5/
PHP Source Code Example
<?php
$pdo = new
PDO('mysql:host=localhost;dbname=database_name',
‘username', ‘password');
$sql = 'SELECT `artist_name` FROM `artist`LEFT OUTER JOIN
`album` ON `artist`.`artist_id` = `album`.`artist_id`
WHERE `album_name` = "Let Love In" LIMIT 0 , 30';
$result = $pdo->query($sql);
while ($row = $result->fetch())
{
$artists[] = $row['artist_name'];
}
include 'output.html.php';
PHP HTML Output Example
<!DOCTYPE html>
<html lang= "en">
<head>
<meta charset='utf-8'>
<title>Script Output</title>
</head>
<body>
<p>
<p> Here is the artist for the album <em>Let Love In</em></p>
<?php foreach ($artists as $art): ?>
<?php echo $art; ?>
<?php endforeach; ?>
</p>
</body>
</html>
Attribution
http://www.w3schools.com/php/default.asp
© Copyright 2026 Paperzz