PHP Unit-I
PHP Unit-I
System
System
http://en.wikipedia.org/wiki/History_of_programming_languages
Client/Server on the WWW
● Standard web sites operate on a request/response basis
● A user requests a resource E.g. HTML document
● Server responds by delivering the document to the client
● The client processes the document and displays it to
user
● Ordinarily the client/response model merely serves a text
file over the HTTP protocol to the user agent application,
which then renders the output appropriately.
● The protocol is inherently stateless (meaning that each
request/response is dealt with individually).
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)
● User/Client authentication is needed.
● Most server side programming script is embedded within
markup
● although does not have to be, sometimes better not to.
Request to a Static Site
Request to a Dynamic Site
● The server must respond dynamically if it needs to provide
different client-side code depending on the situation
● Date and time
● Specifics of the user’s request
● Database contents – forms and authentication
PHP - What is it / does it do?
● PHP is a recursive acronym for “PHP: Hypertext
Preprocessor”
● Server-side scripting language that may be embedded into
HTML
● hence well suited for web development
● Programming language that is interpreted and executed on the server
● Execution is done before delivering content to the client
● Ultimate goal is to get PHP files to generate client-side code
● must end up with HTML, CSS, JavaScript, other client-side code!
● Executes entirely on the server, requiring no specific features
from the client
● It is a widely-used open source general-purpose scripting
language used to create web applications in combination with
a web server, such as Apache
PHP - What is it / does it do?
● Contains a vast library of functionality that programmers
can harness.
● PHP can also be used to create command-line scripts
akin to Perl or shell scripts
● but such use is much less common than PHP’s use as a web
language.
● Strictly speaking, PHP has nothing to do with layout,
events, on-the-fly Document Object Model (DOM)
manipulation, or really anything about the look and feel
of a web page.
● In fact, most of what PHP does is invisible to the end
user.
● Someone looking at a PHP page will not necessarily be able to
tell that it was not written purely in Hypertext Markup Language
(HTML), because the result of PHP is usually HTML.
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
Client
side
HTML
page
How PHP generates HTML/JS Web
pages
Client
Browser
1 4 PHP
3
module
Apache
2
Fast feature
Cost Many extensions
development
<html>
<head>
<title>A simple html demo</title>
</head>
<body>
<h1>HTML & php embedding</h1>
<p>A simple phpdemo</p>;
</body>
</html>
Understanding PHP
● This example illustrates the concept of server-side scripting in
a nutshell.
● The PHP has been interpreted and executed on the web
server, as distinct from Java Script and other client-side
technologies interpreted and executed within a web browser
on a user’s machine.
● The code that you now have in this file consists of four types
of text:
● HTML
● PHP tags
● PHP statements
● White space
● Most of the lines in the example are just plain HTML.
Comments in PHP
● A comment is the portion of a program
that exists only for the human reader.
● The very first thing that a program
executor does with program code is to
strip out the comments, so they cannot
have any effect on what the program
does.
● Comments are invaluable in helping the
next person who reads your code figure
out what you were thinking when you
wrote it, even when that person is
yourself a week from now.
● In PHP, we use // to make a single-line
comment or /* and */ to make a large
comment block.
Jumping in and out of PHP mode
● At any given moment in a PHP script, you are either in PHP
mode or you’re out of it in HTML.
● There’s no middle ground. Anything within the PHP tags is
PHP; everything outside is plain HTML, as far as the server is
concerned.
● <?php $id = 1; ?>
● <FORM METHOD=”POST” ACTION=”registration.php”“>
<P>First name:<INPUT TYPE=”TEXT” NAME=”firstname” SIZE=”20”>
<P>Last name:<INPUT TYPE=”TEXT” NAME=”lastname” SIZE=”20”>
<P>Rank:<INPUT TYPE=”TEXT” NAME=”rank” SIZE=”10”>
<INPUT TYPE=”HIDDEN” NAME=”serial number” VALUE=” <?php
echo $id; ?> ”>
<INPUT TYPE=”submit”SUBMIT” VALUE=”INPUT”“> </FORM>
PHP Language
Basics
Unit-I
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:
● // or # single line comments
● /* Multiple line block comment */
● Precedence
● Enforced using parentheses
● E.g. $sum = 5 + 3 * 6; // would equal 23
● $sum = (5 + 3) * 6; // would equal 48
PHP Variables
● The main way to store information in the middle of a PHP
program is by using a variable
● a way to name and hang on to any value that you want to use later.
● All variables in PHP are denoted with a leading dollar sign ($).
● The value of a variable is the value of its most recent
assignment.
● Variables are assigned with the = operator, with the variable on
the left-hand side and the expression to be evaluated on the
right.
● Variables can, but do not need, to be declared before
assignment.
● Variables have no intrinsic type other than the type of their
current value.
PHP Variables
● Variables used before they are assigned have default values.
● After the initial $, variable names must be composed of letters
(uppercase or lowercase), digits (0–9), and underscore
characters (_).
● Furthermore, the first character after the $ may not be a number.
● Variable names are case-sensitive, i.e. $author and $Author are
different
● A variable name should not contain spaces. If a variable name is
more than one word, it should be separated with an underscore
($my_string) or with capitalization ($myString)
$x = 2; $x = 2;
$y = x + 5; y = $x + 5;
print $y; print $x;
= Assign $x = $y $x = $y
+= Add and assign $x += $y $x = $x + $y
-= Subtract and assign $x -= $y $x = $x - $y
*= Multiply and assign $x *= $y $x = $x * $y
/= Divide and assign quotient $x /= $y $x = $x / $y
%= Divide and assign modulus $x %= $y $x = $x % $y
PHP Comparison Operators
Operator Name Example Result
== Equal $x == $y True if $x is equal to $y
=== Identical $x === $y True if $x is equal to $y, and they are of the
same type
!= Not equal $x != $y True if $x is not equal to $y
<> Not equal $x <> $y True if $x is not equal to $y
!== Not identical $x !== $y True if $x is not equal to $y, or they are not
of the same type
< Less than $x < $y True if $x is less than $y
> Greater than $x > $y True if $x is greater than $y
>= Greater than or $x >= $y True if $x is greater than or equal to $y
equal to
<= Less than or equal $x <= $y True if $x is less than or equal to $y
to
Incrementing and
Decrementing Operators