0% found this document useful (0 votes)
357 views21 pages

IP Chapter 4 PHP Part 1

Server-side scripting allows dynamic web pages by running scripts on the web server. PHP is a popular server-side scripting language that is embedded into HTML and executed by the web server to produce dynamic web page output. PHP files can contain HTML, text, and PHP scripts. The PHP scripts connect to databases and perform actions based on user input to generate customized web page content. Server-side scripting provides functionality like form handling, file processing, database access, and dynamic content generation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
357 views21 pages

IP Chapter 4 PHP Part 1

Server-side scripting allows dynamic web pages by running scripts on the web server. PHP is a popular server-side scripting language that is embedded into HTML and executed by the web server to produce dynamic web page output. PHP files can contain HTML, text, and PHP scripts. The PHP scripts connect to databases and perform actions based on user input to generate customized web page content. Server-side scripting provides functionality like form handling, file processing, database access, and dynamic content generation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Lecture 6

CHAPTER 4
SERVER-SIDE SCRIPTING BASICS
Overview of Web Scripting
oWeb page is a document, typically written in HTML that is almost
always accessible via HTTP.
oIt can be static or dynamic. Static web page means that what is
displayed does not change until the underlying HTML or XML is
changed.
oWhen the content that is displayed changes in response to actions
taken by the user, then the web page is said to be dynamic. This
kind of web page is developed using scripting languages.
o Generally, code written in a scripting language does not have to
be compiled – unlike, for example, code written in C++ or Visual
Basic. Instead, it is interpreted at the time of execution. Web
browsers can interpret certain scripting languages.

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 2


Cont..
oScripts can be written to run either server-side or client-side.
oA script must be interpreted at the time it is requested from the
web server. Each scripting language has its own script interpreter
– called a script engine.
oA client-side script is executed on the client, by the browser.
Client-scripting is often used to validate data entered on a form
by the user, before the form is submitted to the server.
oA server-side script is executed on the server, and generally
produces HTML which is then output HTML to the client.

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 3


Introduction to server-side scripting
oServer-side scripting is a web server technology in which a user's request is
fulfilled by running a script directly on the web server to generate dynamic
web pages.
oIt is usually used to provide interactive web sites that interface to databases
or other data stores.
oIn contrast, server-side scripts, written in languages such as PHP, ASP.NET, 
Java,  ColdFusion,  Perl, Ruby,  Go, Python, and server-side JavaScript, are
executed by the web server when the user requests a document.
oThey produce output in a format understandable by web browsers (usually
HTML), which is then sent to the user's computer.
oThe user cannot see the script's source code (unless the author publishes
the code separately), and may not even be aware that a script was executed.
oDocuments produced by server-side scripts may, in turn, contain client-side
scripts.

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 4


Cont.
oServer-side Web scripting is mostly about connecting Web sites to back end
servers, such as databases. This enables two-way communication:
oClient to server: Customer-enter information as request.
oServer to client: Web pages can be assembled from back end-server to give
output.
A server side script can:-
o Dynamically edit, change or add any content to a Web page to make it more
useful for individual users
o Respond to user queries or data submitted from HTML forms
o Access any data or databases and return the result to a browser
o Provide security since server side codes cannot be viewed from a browser

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 5


Cont.
In server side script, since the scripts are executed on the server, the browser
that displays the file does not need to support scripting at all.
The following are server-side scripting languages:
PHP (*.php)
Active Server Pages (ASP)
Java via Java Server Pages (*.jsp)
JavaScript using Server-side JavaScript (*.ssjs) etc.

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 6


PHP
What is PHP?
oPHP is one of the most prevalent Web programming languages for creating
dynamic Web content.
oIts similarity to C’s syntax and open-source nature make PHP relatively easy to
learn.
oPHP is a scripting language, created in 1994 by Rasmus Lerdorf from the Apache
Group that is designed for producing dynamic Web content.
oPHP originally stood for “Personal Home Page,” but as its functionality
expanded, it became “PHP: Hypertext Processor” because it takes PHP code as
input and produces HTML as output.
oPHP is especially suited for web development and can be embedded into HTML.
oPHP is an open source software and free to download and use.

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 7


PHP
o PHP supports many databases (MySQL, Informix, Oracle, Sybase, PostgreSQL,
Generic ODBC, Microsoft SQL Server etc.).
o PHP files can contain text, HTML tags and scripts. Like other files written in
server side language, PHP files are returned to the browser as plain HTML and
it has a file extension of ".php", ".php3", or ".phtml".
o The server processes the html document, executing the PHP segments and
substituting the output within the html document
o PHP scripts are executed on the server.
o PHP is used for form handling, file processing, and database access.
o An embedded scripting language, meaning that it can exist within HTML code.
o Cross-platform, meaning it can be used on Linux, Windows, Macintosh, etc.,
making PHP very portable.
o easy to learn and runs efficiently on the server side

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 8


Cont.
Why PHP?
oScalability: Web-based 3-tier architecture can scale out
oPHP runs on different platforms (Windows, Linux, UNIX, Mac OS X, etc.)
oPHP is compatible with almost all servers used today (Apache, IIS, etc.)
oPHP has support for a wide range of databases
oPHP is free. Download it from the official PHP resource: www.php.net
oPHP is easy to learn and runs efficiently on the server side

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 9


Cont.
How do webserver works?
oClient specifies document at a specific web address that is desired (specified by
a URL)
o Ex: http://www.dtu.edu.et

oIf the document is html or text, the server simply forwards it back to the client
o It is then rendered in the client's browser

oHowever, if it has embedded PHP, the server first executes the PHP, replacing
the PHP code with its output in the document
oThe modified document is then sent to the client

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 10


Cont.
What you need to get started
oHopefully you have established a basic idea of what server-side scripting is,
and when you should use it. Next you need some basic requisites to be able to
write and execute scripts. Basically you need:
o Install WAMP server. WAMP server refers to a software stack for the
Microsoft Windows operating system, created by Romain Bourdon and
consisting of the Apache web server, OpenSSL for SSL (Secure Sockets Layer)
support, MySQL database and PHP programming language. WAMP sever has a
root directory called WWW. So all the files of a website must be stored under
this directory (C:\wamp\www\) to be executed by the server.
oYou need some sort of text editor such as Notepad, Notepad++, etc. to write
the scripts.
oYou also need a web browser to display the web content. The web browser
can be Internet Explorer, Mozilla Firefox, Opera, and Google Chrome etc.

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 11


PHP Basic Syntax
The PHP parsing engine needs a way to differentiate PHP code from other
elements in the page.
The mechanism for doing so is known as ‘escaping to PHP.’ A PHP scripting
block always starts with <?php and ends with ?>.
The PHP interpreter considers anything outside of these delimiters as plaintext
to be sent to the client.
PHP tags
The most universally effective PHP tag style is: <? PHP write a code here ?
>
Short or short-open tags look like this: <? Write a code here ?>
HTML script tags look like this:
<script language="PHP">...</script>

09/07/2021 HTML PREPARED BY INSTRUCTOR GIZATIE D. 12


Cont.
NB:
On servers with shorthand support enabled you can start a scripting block with
<? and end with ?>.
For maximum compatibility, it is recommended to use the standard form (<?
php…?>) rather than the shorthand form<?....?>
A statement in PHP is any expression that is followed by a semicolon (;)

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 13


PHP Output Statement
oThere are two basic statements to output text with PHP: echo and print.
oecho has no return value whereas print has a return value.
oThe returned value represents whether the print statement is succeeded or not. If the
print statement succeeds, the statement returns 1 otherwise 0.
oecho can take multiple parameters (although such usage is rare) but print can only
take one argument.
oecho is marginally faster than print.
oThe echo or print statement can be used with or without parentheses: echo or echo().
oThe general format of the echo statement is as follows:
oecho outputitem1,outputitem2,outputitem3, . . .;
oecho (output statement);
oThe general format of the print statement is as follows:
oprint outputstatement;
oprint(outputstatement);
09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 14
Cont.
It is possible to embed HTML tags in echo or print statements. The browser will
parse and interpret them like any tag included in HTML page and display the
page accordingly.
Table1.1 Using echo/print statements

echo/print statement PHP output web page display


echo “Hello World”; HelloWorld! HelloWorld!
echo 123; 123 123
echo “Hello\nWorld!”; Hello Hello World!
World!
echo “Hello<br>World!”; Hello<br>World Hello
World!
echo hello; Error// a string should error// a string
be should be
enclosed quote enclosed quote

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 15


Cont.
In HTML, a new line is not displayed as a new line; it is just interpreted as a
single space.
Note: The file must have exa.php extension. If the file has exa.html extension,
the PHP code will not be executed.

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 16


Working with Variables
A variable is a special container that can be defined to hold a value such
as number, string, object, array, or a Boolean.

Here are the most important things to know about variables in PP.

The main way to store information in the middle of a PHP program is by using
a variable.
 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 used before they are assigned have default values.

 PHP does a good job of automatically converting types from one to another when
necessary.

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 17


Cont.
Variable Naming Rules
When creating PHP variables, you must follow these four rules:
Variable names must start with a letter of the alphabet or the _ (underscore)
character.
Variable names can contain only the characters: a-z, A-Z, 0-9, and _
(underscore).
Variable names may not contain spaces. If a variable must comprise more
than one word it should be separated with the _ (underscore) character.
(e.g., $user_name).
Variable names are case-sensitive. The variable $High_Score is not the same
as the variable $high_score.

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 18


Data types
The correct way of declaring a variable in PHP is:
$Variable_Name=value;
PHP has a total of eight data types which we use to construct our variables:
integers, doubles, Booleans, null, strings, arrays, objects and resources. The
first five are simple types, and the next two (arrays and objects) are compound -
the compound types can package up other arbitrary values of arbitrary type,
whereas the simple types cannot.
<? php
$variable = "name";
$literally = 'My $variable will not print!\\n';
print($literally);
$literally = "My $variable will print!\\n";
print($literally);
?>

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 19


Cont.
PHP provides a large number of predefined variables to all scripts. The variables represent
everything from external variables to built-in environment variables, last error messages to last
retrieved headers.
$GLOBALS — References all variables available in global scope
$_SERVER — Server and execution environment information
$_GET — HTTP GET variables
$_POST — HTTP POST variables
$_FILES — HTTP File Upload variables
$_REQUEST — HTTP Request variables, and can replace $_POST, $_GET and $_COOKIE variables

$_SESSION — Session variables


$_COOKIE — HTTP Cookies

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D. 20


y ou
n k
T ha

09/07/2021 PHP PREPARED BY INSTRUCTOR GIZATIE D.


21

You might also like