Chapter One
Chapter One
Server-side scripting language, which means that the scripts are, executed on the server, the
computer where the Web site is located. Server-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. It is usually used to provide interactive web sites that interface to databases
or other data stores. This is different from client-side scripting where scripts are run by the
viewing web browser.
The primary advantage to server-side scripting is the ability to highly customize the response
based on the user's requirements, access rights, or queries into data stores. From security point of
view, server-side scripts are never visible to the browser as these scripts are executes on the
server and emit HTML corresponding to user's input to the page.
Server-side Web scripting is mostly about connecting Web sites to back end servers, such as
databases. This enables two-way communication:
Server-side scripting is about "programming" the behavior of the server while client-side
scripting is about "programming" the behavior of the browser. Normally, when a browser
requests an HTML file, the server returns the file. However, if the file contains a server-side
[Date] 1
script, the script is executed on the server before the file is returned to the browser as plain
HTML.
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 followings are server-side scripts:
PHP (*.php)
Active Server Pages (ASP)
ANSI C scripts • Java via JavaServer Pages (*.jsp)
JavaScript using Server-side JavaScript (*.ssjs)
Lasso (*.lasso) etc
The main focus here is PHP, which is a server-side scripting language, which can be embedded
in HTML or used as a standalone binary, and could be run with open source software like
WAMP server.
PHP can dynamically create the HTML code that generates the Web page.
Web page visitors see the output from scripts, but not the scripts themselves.
PHP stands for PHP: Hypertext Preprocessor. It is a server-side scripting language, which can
be embedded in HTML. Over the past few years, PHP and server-side Java have gained
momentum, while ASP has lost mindshare.
PHP is a server-side scripting language, which means that the scripts are executed on the server,
the computer where the Web site is located. This is different than JavaScript, another popular
[Date] 2
language for dynamic Web sites. JavaScript is executed by the browser, on the user ‘s
computer. Thus, JavaScript is a client-side language.
Because PHP scripts execute on the server, PHP can dynamically create the HTML code that
generates the Web page, which allows individual users to see customized Web pages. Web page
visitors see the output from scripts, but not the scripts themselves.
PHP is particularly strong in its ability to interact with databases. PHP handles connecting to the
database and communicating with it, so we don ‘t need to know the technical details for
connecting to a database or for exchanging messages with it, it is enough telling PHP the name
of the database and where it is, and PHP handles the details. It connects to the database, passes
our instructions to the database, and returns the database response to us.
PHP files can contain text, HTML, CSS, JavaScript, and PHP code
PHP code is executed on the server, and the result is returned to the browser as plain HTML
PHP can create, open, read, write, delete, and close files on the server
With PHP you are not limited to output HTML. You can output images or PDF files. You can
also output any text, such as XHTML and XML.
[Date] 3
Syntax is a way to representation of PHP script, which gives the primary idea to specify the code
format. A PHP script is executed on the server, and the plain HTML result is sent back to the
browser. To work with server script, for example with PHP, we need to install a web server,
programming itself, and database server. For PHP, we install PHP as a programming. There are
many web servers to choose for PHP uses; the most commonly used web server is called Apache
which we can download from the internet freely. Similarly, for database, there are many options
to use; the most popular database server for web pages is MySQL which again can be
downloaded freely from internet.
PHP is also available freely on the internet. In order to develop and run PHP Web pages three
vital
Web Server - PHP will work with virtually all Web Server software, including
Microsoft's
Internet Information Server (IIS) but then most often used is freely available Apache
Server.
Database - PHP will work with virtually all database software, including Oracle and
Sybase but most commonly used is freely available MySQL database.
PHP Parser - In order to process PHP script instructions a parser must be installed to
generate HTML output that can be sent to the Web Browser.
The official PHP website (PHP.net) has installation instructions for PHP:
http://php.net/manual/en/install.php
<?php
echo "Welcome to the world of php";
?>
Short or short-open tags look like this:
<?
echo "welcome to the world of php";
?>
[Date] 4
HTML script tags: HTML script tags look like this:
A PHP script can be placed anywhere in the document. A PHP script starts with <?php and
ends with ?>: A PHP file normally contains HTML tags, and some PHP scripting code.
Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP
function "echo" to output the text "Hello World!" on a web page:
<!DOCTYPE html>
<html>
<body>
<?php
?>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<?php
?>
[Date] 5
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<?php
$color = "red";
?>
</body>
</html>
[Date] 6
// This is a single-line comment
/* This is a multi-line comment */
?>
Here are the most important things to know about variables in PHP.
In PHP, a variable starts with the $ sign, followed by the name of the variable:
<?php
$x = 5;
$y = "John";
echo $x;
echo "<br>";
echo $y;
?>
In the example above, the variable $x will hold the value 5, and the variable $y will hold the
value "John".
PHP has a total of eight data types which we use to construct our variables:
1. Integers: are whole numbers, without a decimal point, like 4195.
2. Doubles: are floating-point numbers, like 3.14159 or 49.1.
3. Booleans: have only two possible values either true or false.
[Date] 7
4. Strings: are sequences of characters, like 'PHP supports string operations.'
5. Arrays: are named and indexed collections of other values.
6. Objects: are instances of programmer-defined classes, which can package up both other
kinds of values and functions that are specific to the class.
7. Resources: are special variables that hold references to resources external to PHP (such
as database connections).
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.
1.5. PHP Strings
PHP Strings are sequences of characters, like “Hello world!". Following are valid examples of
string
<?php
echo "Hello";
print "Hello";
?>
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.
Note There is a big difference between double quotes and single quotes in PHP. Double quotes
process special characters, single quotes does not. E.g. when there is a variable in the string, it
returns the value of the variable:
<?php <?php
$x = "John"; $x = "John";
echo "Hello $x"; echo 'Hello $x';
?> ?>
Output: Hello John Output: Hello $x
The PHP strlen() function returns the length of a string.
[Date] 8
<?php
echo strlen("Hello world!");
?>
Output=12
Arithmetic operators
Assignment operators
Increment/Decrement operators
Logical operators
String operators
Conditional assignment operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical
operations, such as addition, subtraction, multiplication, and, division etc.
[Date] 9
Output: 16, 4, 60,1.6666666666667,4
PHP Assignment Operators
The PHP assignment operators are used with numeric values to write a value to a variable. The
basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the
assignment expression on the right.
[Date] 10
PHP Logical Operators
Example:
1. <?php $x = 100; $y = 50; if ($x == 100 and $y == 50) { echo "Hello world!"; } ?>
2. <?php $x = 100; $y = 50; if ($x == 100 or $y == 80) { echo "Hello world!"; } ?>
Output: Hello world!
3. <?php $x = 100; $y = 50; if ($x == 100 xor $y == 80) { echo "Hello world!"; } ?>
Output: Hello world!
4. <?php $x = 100; $y = 50; if ($x == 100 && $y == 50) { echo "Hello world!"; } ?>
Output: Hello world!
5. <?php $x = 100; $y = 50; if ($x == 100 || $y == 80) { echo "Hello world!"; } ?>
Output: Hello world!
[Date] 11
6. <?php $x = 100; if (!($x == 90)) { echo "Hello world!"; } ?>
Output: Hello world!
[Date] 12