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

Lecture15-Intro To PHP

Uploaded by

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

Lecture15-Intro To PHP

Uploaded by

noor ulain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

1

BIO400
Lecture 15

INTRODUCTION TO
PHP

Ms. Tamsila Parveen


2

ORIGINS AND USES OF PHP


• Origins
– Rasmus Lerdorf – 1995
– Developed to allow him to track visitors to his Web site

• PHP is an open-source product


• PHP is an acronym for Personal Home Page, or
PHP: Hypertext Preprocessor
• PHP is used for form handling, file processing,
and database access
3

OVERVIEW OF PHP
• PHP is a server-side scripting language, like ASP
• PHP supports many databases (MySQL, Informix, Oracle,
Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
• PHP is an open source software (OSS)
• PHP runs on different platforms (Windows, Linux, Unix, etc.)
• PHP is compatible with almost all servers used today
(Apache, IIS, etc.)
• PHP is FREE to download from the official PHP resource:
www.php.net
What is a PHP File?

– PHP files may contain text, HTML tags and


scripts

– PHP files are returned to the browser as


plain HTML

– PHP files have a file extension of ".php",


".php3", or ".phtml"
What Can PHP Do?
• PHP can generate dynamic page content
• Allow users to upload files
• Create/read files on the server (for example, the
files that your users upload)
• Have a "member's area" (i.e. via a login page)
• Have a shopping cart
How PHP works ?
• PHP is a server-side language that means the code
written in PHP resides on a host computer called a server.
The server sends Web pages to the requesting visitors
(you, the client, with your web browser)
PHP Client-Server
Client Server

1 URL 2

HTML 3 Script request

1 Visitor goes to a web site written in PHP HTML


PHP

2 Server reads the PHP and then processes it


according to its scripted directions.

3 PHP code tells the server to send the


appropriate data – HTML code. Treats it as a
standard HTML.
PHP Installation
• You need to install web server before you
install PHP. There are many different web
servers available to choose from, so you
need to choose which one you prefer.
Two of the more popular web servers are:
• Apache
• Internet Information Services (IIS)
PHP Syntax
The PHP syntax is based on C, Java, and Perl,
Creating a PHP file is similar to creating an HTML
file. In fact, most PHP files are a mixture of PHP
code and HTML.

To create a PHP file, simply do the following:


1. Create a new file in your favorite editor
2. Type some PHP code
3. Save the file with a .php extension
Basic Code Syntax
3 different forms declaring php
<html> <?
<head> PHP Code In Here
<title>PHP Syntax Example</title> ?>
</head> <?php
<body> PHP Code In Here
<?php php?>
echo "PHP is easy!";
?> <script language="php">
PHP Code In Here
</body> </script>
</html>
Comments in PHP
<html>
PHP support three kinds of comment <body>
tags :
<?php
1. //
//This is a comment
This is a one line comment
/*
2. #
This is
This is a Unix shell-style comment.
It's also a one line comment a comment
3. /* ..... */ block
Use this multi line comment if you */
need to. ?>
</body>
</html>
How to Run PHP File
Variables
•Variables are used for storing values, like text strings,
numbers or arrays.

•When a variable is declared, it can be used over and over


again in your script.

•All variables in PHP start with a $ sign symbol.

•The correct way of declaring a variable in PHP:

$var_name = value;
String Variables in PHP
• In PHP, variable names must start with a
dollar sign ($).

• String variables are used for values that


contains characters.

<?php
$txt="Hello World";
echo $txt;
?>
PHP Variables Examples
• <?php
$txt = "Hello world!"; <?php
$x = 5; $txt = “Fruits of all
flavors";
$y = 10.5; echo "I like $txt!";
?> ?>
<?php
$variable1 = 2;
$variable2 = 9;
$variable3 = $variable1 + $variable2;
echo $variable3;
?>
Rules for PHP variables:
• A variable starts with the $ sign, followed by the name
of the variable
• A variable name must start with a letter or the
underscore character
• A variable name must not contain spaces.
• A variable name can only contain alpha-numeric
characters and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive ($age and $AGE are
two different variables)
PHP Variables-Concatenation
<?php
$variable1 = 2;
$variable2 = 9;
$variable3 = $variable1 + $variable2;
echo $variable1 . " + " . $variable2 . " = "
. $variable3;
?>
17

PRIMITIVES, OPERATIONS, EXPRESSIONS

• PHP has many predefined variables, including the


environment variables of the host operating system
– You can get a list of the predefined variables by calling
phpinfo() in a script

• There are eight primitive types:


– Four scalar types: Boolean, integer, double, and string
– Two compound types: array and object
– Two special types: resource and NULL
18

PRIMITIVES, OPERATIONS, EXPRESSIONS

• String Operations and Functions


• Functions:
•strlen, strcmp, strpos, substr, as in
C
•chop – remove whitespace from the right end
•trim – remove whitespace from both ends
•ltrim – remove whitespace from the left end
•strtolower, strtoupper
19

PRIMITIVES, OPERATIONS, EXPRESSIONS

• The type of a variable can be determined


with gettype or is_type
– gettype($total) - it may return data type
– is_integer($total) – a predicate function
20

OUTPUTS
• Output from a PHP script is HTML that is sent to the
browser
• HTML is sent to the browser through standard output
• There are three ways to produce output: echo, print, and
printf
– echo and print take a string, but will coerce other values to strings
echo “Test“,”None”; # More than one parameter acceptable
echo("first <br />", $sum) # More than one,
print "Welcome to my site!"; # Only one
printf – same like C
• PHP code is placed in the body of an HTML document
21

OUTPUTS
• An Example:
<html>
<head><title> Trivial php example </title>
</head>
<body>
<?php
print "Welcome to my Web site!";
?>
</body>
</html>
echo and print Difference

You might also like