Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

WebDevelopmet Lec2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

PHP

WEB APPLICATION
DEVELOPMENT COURSE

LECTURE 2
Introduction
2

 PHP Hypertext Preprocessor is a server scripting


language, and a powerful tool for making dynamic
and interactive Web pages.
 PHP is an open source, widely-used, free, fast and
efficient alternative to competitors such as ASP and
JSP.
 PHP code is executed on the server.
Features of PHP
3

 PHP can generate dynamic page content.


 PHP can create, open, read, write, delete, 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 be used to control user-access.
 PHP can encrypt data.
Why PHP?
4

 PHP runs on various platforms (Windows, Linux,


Unix, Mac OS X, etc.)
 PHP is compatible with almost all servers used today
(Apache, IIS, etc.)
 PHP supports a wide range of databases.
 PHP is an Interpreted language, hence it doesn't
need a compiler to be executed.
Intro to PHP
5

• What is PHP?
 Language developed by Rasmus Lerdorf from the Apache
Group
 Its primary use is for server-side scripting
• Ex: To process HTML forms
• Ex: To perform a DB query and pass on results
• Ex: To dynamically generate HTML
 PHP scripts are often embedded within HTML documents
• The server processes the HTML document, executing the PHP
segments and substituting the output within the HTML document
Intro to PHP
6

 The modified document is then sent to the client


 As mentioned previously, the client never sees the PHP
code
• The only reason the client even knows PHP is involved is due to
the file extension → .php
 But even this is not required if the server is configured
correctly
> The server can be configured to run PHP scripts even if the file
does not have a .php extension
> By default XAMPP will only execute PHP if the .php extension is
provided
> Can change this with an .htaccess file
> See what happens if you have PHP code without the .php
extension
Intro to PHP
7

 PHP is a HUGE language


 It is a fully functional language

 It has an incredible amount of built-in features

 Form processing
 Output / generate various types of data (not just text)
 Database access
 Allows for various DBs and DB formats

 Object-oriented features
 Somewhat of a loose hybrid of C++ and Java

 Huge function / class library


Intro to PHP
8

 We will look at only a small part of PHP


 There are also many tools that are already pre-written in / for
PHP
 If you are building a substantial project you may want to use some
of these
 Ex: http://github.com/pear

 Also see http://www.php.net/sites.php

 There are also content management systems written in PHP


 Ex: http://drupal.org/

 Ex: http://wordpress.org/

 However, we may not be covering them here


 We will focus on the straight PHP language
Intro. to PHP
9

 PHP Program Structure


 Or really lack thereof

 PHP, as with many scripting languages, does not have


nearly the same structural requirements as a language
like Java
 A script can be just a few lines of code or a very large,
structured program with classes and objects
 The complexity depends on the task at hand
 However, there are some guidelines for incorporating
PHP scripts into HTML files
Requirements for PHP
10

1. PHP Parser: To execute PHP scripts, PHP installation is


required.
2. Web Server: Because PHP is mostly used to develop
websites, hence most of its implementations comes
bundled with Apache Web Server, which is required to
host the application developed in PHP over HTTP.
3. Database: Any database management system, PHP
comes with a native support for MySQL.
 Now, you can install all the 3 services separately yourself,
or you can simply download and install software which
automatically installs all the above services.[USB
Webserver]
PHP Syntax
11

 A 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".

 <?php

// PHP code goes here


?>

 Adding php code inside the PHP tags is known

as Escaping to php.
• Consider the following PHP file
12

<!DOCTYPE html>
HTML 5 Document
<html>
Root HTML Tag
<head>
<title>Simple PHP Example</title> Document Head
</head>
<body>
D <?php echo "<p><h1>Output</h1>";
echo "<h2>Output</h2>";
O echo "<h3>Output</h3></p>";
C ?> PHP Code
<script language="PHP">
echo "\n<b>More PHP Output</b>\n";
B echo "New line in source but not rendered";
O echo "<br/>";
D echo "New line rendered but not in source";
</script>
Y
</body>
</html>
PHP Syntax Rules
13
PHP Hello World

Above is the PHP source code.


PHP Hello World

It renders as HTML that looks like this:


PHP Hello World

This program is extremely simple and you really did


not need to use PHP to create a page like this. All it
does is display: Hello World using the PHP echo()
statement.

Think of this as a normal HTML file which happens


to have a set of special tags available to you that do a
lot of interesting things.
PHP Comments

In PHP, we use // to make


a single-line comment or /*
and */ to make a large
comment block.
Data Types in PHP
18
Declaring PHP Variables
19

 PHP variable names are case-sensitive!


 $tiger is not same as $Tiger
 <?php Not start with a
$variableName = value; numeric value. It
can either start with
?> an alphabet or an
 $str = "I am a string"; underscore sign _
($_var)
 $int = 4;
 $float = 3.14;
 $bol = true; //false
 $var = null;
PHP Conditional Statements
20
PHP Loop
21

Executed at least one


time (b=11)
PHP Loop
22
Thank you
23

You might also like