0% found this document useful (0 votes)
20 views12 pages

Chapter One

After studying this course you will have a greater understanding of the following: – New features of HTML 5 and their uses – Geolocation for identifying geo-position of users – The < video > tag – Canvas and SVG for drawing on the Web – Use of app cache and database – Use of Web workers

Uploaded by

btekulu2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views12 pages

Chapter One

After studying this course you will have a greater understanding of the following: – New features of HTML 5 and their uses – Geolocation for identifying geo-position of users – The < video > tag – Canvas and SVG for drawing on the Web – Use of app cache and database – Use of Web workers

Uploaded by

btekulu2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Chapter 1

Server-Side Scripting Basics in PHP


1. Introduction to Server-Side Script

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.

In 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. They produce output in a format understandable by
web browsers (usually HTML), which is then sent to the user's computer. The 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. Documents produced by server-side scripts may, in turn,
contain client-side scripts.

Server-side Web scripting is mostly about connecting Web sites to back end servers, such as
databases. This enables two-way communication:

 Client to server: Customer-entered information as request.


 Server to client: Web pages can be assembled from back end-server to give output.

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.

A server script can do: -

 Dynamically edit, change or add any content to a Web page


 Respond to user queries or data submitted from HTML forms
 Access any data or databases and return the result to a browser
 Customize a Web page to make it more useful for individual users
 Provide security since server code cannot be viewed from a browser

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.

1.1. Introduction to PHP

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.

1.1.1. What is PHP?

PHP is a widely-used, open source scripting language

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 files have extension ".php"

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

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.

1.2. PHP Syntax

[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

components need to be installed on computer system.

 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

There are three ways to start PHP environment

The most commonly and effective PHP syntax

<?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:

<script language=‖php‖>.. . </script>

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>

<h1>My first PHP page</h1>

<?php

echo "Hello World!";

?>

</body>

</html>

PHP statements end with a semicolon (;).


In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined
functions are not case-sensitive. In the example below, all three echo statements below are
equal and legal: ECHO is the same as echo:

<!DOCTYPE html>

<html>

<body>

<?php

ECHO "Hello World!<br>";

echo "Hello World!<br>";

EcHo "Hello World!<br>";

?>

[Date] 5
</body>

</html>

Note: However; all variable names are case-sensitive!


Look at the example below; only the first statement will display the value of the $color variable!
This is because $color, $COLOR, and $coLOR are treated as three different variables:

<!DOCTYPE html>

<html>

<body>

<?php
$color = "red";

echo "My car is " . $color . "<br>";

echo "My house is " . $COLOR . "<br>";

echo "My boat is " . $coLOR . "<br>";

?>

</body>

</html>

1.3. PHP Comments


Comments in PHP
A comment in PHP code is a line that is not executed as a part of the program. Its only purpose
is to be read by someone who is looking at the code.
Comments can be used to:
 Let others understand your code
 Remind yourself of what you did - Most programmers have experienced coming back to
their own work a year or two later and having to re-figure out what they did.
 Comments can remind you of what you were thinking when you wrote the code
 Leave out some parts of your code
PHP supports several ways of commenting:
 Single Line Comments
 Multiple Line Comments
<?php

[Date] 6
// This is a single-line comment
/* This is a multi-line comment */
?>

1.4. PHP Variables


A variable is a special container that can be defined to hold a value such as number, string,
object, array, or a Boolean. The main way to store information in the middle of a PHP program
is by using a variable.

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

 A variable name must start with a letter or the underscore character


 A variable name cannot start with a number
 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)
 Remember that PHP variable names are case-sensitive!
Creating (Declaring) PHP Variables

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

1.6. PHP Operators


Operators are used to perform operations on variables and values. PHP divides the operators in
the following groups:

 Arithmetic operators
 Assignment operators
 Increment/Decrement operators
 Logical operators
 String operators
 Conditional assignment operators

PHP Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common arithmetical
operations, such as addition, subtraction, multiplication, and, division etc.

Operator Name Example Result


+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and $y
* Multiplication $x * $y Product of $x and $y
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x divided by $y
Example:
<?php
$x = 10;
$y = 6;
echo $x+,-,*,/, % $y;
?>

[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.

Assignment Same as... Description


x += y x=x+y Addition
x -= y x=x-y Subtraction
x *= y x=x*y Multiplication
x /= y x=x/y Division
x %= y x=x%y Modulus
Example:
1. <?php $x = 20; $x += 100; echo $x; ?> Output: 120
2. <?php $x = 50; $x -= 30; echo $x; ?> Output: 20
3. <?php $x = 5; $x *= 6; echo $x; ?> Output: 30
4. <?php $x = 10; $x /= 5; echo $x; ?> Output: 2
5. <?php $x = 15; $x %= 4; echo $x; ?> Output: 3

PHP Increment / Decrement Operators


The PHP increment operators are used to increment a variable's value. The PHP decrement
operators are used to decrement a variable's value.
Operator Same as... Description
++$x Pre-increment Increments $x by one, then returns $x
$x++ Post-increment Returns $x, then increments $x by one
--$x Pre-decrement Decrements $x by one, then returns $x
$x-- Post-decrement Returns $x, then decrements $x by one
Example:
1. <?php $x = 10; echo ++$x; ?> Output: 11
2. <?php $x = 10; echo $x++; ?> Output: 9
3. <?php $x = 10; echo --$x; ?> Output: 9
4. <?php $x = 10; echo $x--; ?> Output: 10

[Date] 10
PHP Logical Operators

The PHP logical operators are used to combine conditional statements.

Operator Name Example Result

and And $x and $y True if both $x and $y are true

or Or $x or $y True if either $x or $y is true

xor Xor $x xor $y True if either $x or $y is true, but not


both

&& And $x && $y True if both $x and $y are true

|| Or $x || $y True if either $x or $y is true

! Not !$x True if $x is not true

Example:

1. <?php $x = 100; $y = 50; if ($x == 100 and $y == 50) { echo "Hello world!"; } ?>

Output: 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

You might also like