You are on page 1of 42

CHAPTER 10

Server side scripting basics


Outline
2

 Introduction to server-side scripting


 Server-side scripting languages
 Use Basic Syntax
 Send Data to the Web Browser
 Write Comments
 Utilize Variables
 Manipulate Strings
 Manipulate Numbers
 Work with constants
Introduction to server-side scripting
3

 Web programming involves the following tools


 Static HTML
 Client-side scripting
 Server-side scripting
 Static HTML
 Is written purely in HTML language (text based)
 Advantages
 Any browser can display it
 Any device can display it
 Easy to learn and produce automatically
 Web developers can make changes easily
…cont’d…
4

 Disadvantages
 It makes control of design and layout difficult
 It doesn’t scale up to large number of pages
 It is not very interactive
 It can’t cope with rapidly changing content or presentation

 In response to these disadvantages the client side


scripts are introduced, then server side scripts
…cont’d…
5

 Client-side scripts
Tools/Languages Use

CSS Formatting pages

JavaScript /VBScript Event handling

Java Applets To get standalone applications

 They largely depend on the browser(type).


 They create the eye-catching part of the web
…cont’d…
6

 Server-side scripts
 Normally invisible to the user
 Is mostly all about connecting websites to the back-end
servers
 Creates 2-way communication
 Client to server
 Server to client
…cont’d…
7

 Server side scripts are good for:


 Community forums
 Email
 Support systems (customer, technical)
 Advertising
 Surveys ,tests etc
 Informational sites
 Games
 Any other application that needs backend server
…cont’d…
8
Server-side scripting languages
9

 Many languages may be used to create these


scripts.
 They include but are not limited to the examples
below.
 PHP
 JSP
 ASP.NET
 Python
Use Basic Syntax (Php)
10

 PHP is an HTML-embedded scripting language,


meaning that you can intermingle PHP and HTML
code within the same file.
 So to begin programming with PHP, start with a
simple Web page.
 To add PHP code to a page, place it within PHP
tags:
…cont’d…
11

 Along with placing PHP code within PHP tags,


your PHP files must have a proper extension.
 The extension tells the server to treat the script in a
special way, namely, as a PHP page.
 Most Web servers use .html for standard HTML
pages and .php for PHP files.
 If your HTML file contains even a single line of php
code, try to save it with .php file extension
…cont’d…
12

 To make a basic PHP script:


 Create a new document in your editor or IDE
 Create basic HTML
 Before the closing body tag, insert the PHP tags:
 Save the file as first.php
 Place the file in the proper directory of your web server
(C:/xampp/htdocs/Your-file )
 Run your first.php file using a web-browser
 Type ‘http://localhost/first.php’ or
 ‘http://127.0.0.1/first.php’ in browsers address bar
…cont’d…
13

 If your first.php file didn’t run, check the


following:
 Start the web server control panel
 Use URL format to run the file as follows in your
browser
…cont’d…
14

• If you get file not found!/object not found! error, check


 The file name and extension for correctness
 The location of the file
Example
15

 The starting HTML file with output


Sending data to web browser
16

 To create dynamic Web sites with PHP, you must


know how to send data to the Web browser.
 PHP has a number of built-in functions for this
purpose, the most common being echo and print.

Both are the same!


…cont’d…
17

 ECHO and echo can be equally used.


Writing Comments
18

 Use the following syntax


 ‘ #’ or ‘// ‘for single line comment
 ‘ /* */’ for multiline comment
 Example:
Utilize Variables
19

 Variables are containers used to temporarily store values.


 These values can be numbers, text, or much more
complex data.
 PHP supports eight types of variables.
 These include four scalar (single-valued) types—Boolean
(TRUE or FALSE), integer, floating point (decimals), and
strings (characters);
 Two non-scalar (multi-valued)—arrays and objects; plus
 Resources (which you’ll see when interacting with databases)
and NULL (which is a special type that has no value).
…cont’d…
20

 Rules of naming and using a variable:


 A variable’s name must start with a dollar sign ($),
 The variable’s name can contain a combination of letters,
numbers, and the underscore,
 The first character after the dollar sign must be either a letter or
an underscore
 Variable names in PHP are case-sensitive!
 Variables can be assigned values using the equals sign (=),
also called the assignment operator.
 To display the value of a variable, you can print the variable
without quotation marks:
…cont’d…
21

print $some_var;
 Or variables can be printed within double quotation
marks:
print "Hello, $name";
 You cannot print variables within single quotation
marks:
print 'Hello, $name'; // Won't work!
Example(code)
22

Shows the
usage
of variables
Example (Output)
23
…cont’d…
24
Manipulate Strings
25

 string is merely a quoted chunk of characters: letters,


numbers, spaces, punctuation, and so forth.
 These are all strings:
 ‘fikir’
 “In watermelon sugar”
 ‘100’
 ‘March 2, 2021’
 To make a string variable, assign a string value to a valid
variable name:
 $first_name = ‘fikir';
 $today = March 2, 2021';
…cont’d…
26

 When creating strings, you can use either single or


double quotation marks to encapsulate the
characters, just as you would when printing text.
 $var = "Define \"platitude\", please.";
 var = 'Define "platitude", please.';
 To print out the value of a string, use either echo or
print:
 echo $first_name;
 echo "Hello, $first_name";
…cont’d…
27

 Concatenating strings
 is like addition for strings, whereby characters are added to
the end of the string.
 It is performed using the concatenation operator, which is
the period (.):
 Examples
 $city= 'Seattle';
 $state = 'Washington';
 $address = $city . $state;

 The $address variable now has the value


SeattleWashington
…cont’d…
28

 String functions
Name Java Equivalent
strlen length
strpos indexOf
substr substring
strtolower, strtoupper toLowerCase, toUpperCase
trim trim
explode, implode split, join
strcmp compareTo
String Functions Example
29
…cont’d…
30

 Output of the previous slide program


Interpreted Strings
31

$age = 16;
print "You are " . $age . " years old.\n";
print "You are $age years old.\n"; # You are 16 years old.
PHP

 strings inside " " are interpreted


 variables that appear inside them will have their values
inserted into the string
 strings inside ' ' are not interpreted:
print ' You are $age years old.\n '; # You are $age years
old. \n PHP
Interpreted Strings (cont.)
32

print "Today is your $ageth birthday.\n"; # $ageth not


found
print "Today is your {$age}th birthday.\n";
PHP

 if necessary to avoid ambiguity, can enclose


variable in {}
Manipulate Numbers
33

 Valid number-type variables in PHP can be anything like


 8
 3.14
 10980843985
 -4.2398508
 4.4e2
 Along with the standard arithmetic operators you can use on
numbers
 There are dozens of number functions in PHP, but the most
common are:
 round -- rounds a number to the nearest number
 number_format -- used to format a number as common usage format
…cont’d…
34

 Examples
 $n = 3.14;
 $n = round ($n); // 3
 It can also round to a specified number of decimal places:
 $n = 3.142857;
 $n = round ($n, 3); // 3.143

 $n = 20943;
 $n = number_format ($n); // 20,943
 This function can also set a specified number of decimal points:
 $n = 20943;
 $n = number_format ($n, 2); //
 20,943.00
Example(code)
35
Example(output)
36
Work with constants
37

 Constants, like variables, are used to temporarily store a


value, but otherwise, constants and variables differ in many
ways.
 For starters, to create a constant, you use the define( )
function instead of the assignment operator (=):
 define ('NAME', value);
 unlike variables, a constant’s value cannot be changed.
 A constant can only be assigned a scalar value, like a string or
a number:
 define ('USERNAME', 'troutocity');
 define ('PI', 3.14);
…cont’d…
38

 To access a constant’s value, like when you want to print it,


you cannot put the constant within quotation marks:
 echo "Hello, USERNAME"; // Won't work!
 Instead, either print the constant by itself:
 echo 'Hello, ';
 echo USERNAME;
 or use the concatenation operator:
 echo 'Hello, ' . USERNAME;
 PHP runs with several predefined constants
 PHP_VERSION (the version of PHP running) and PHP_OS
(the operating system of the server).
Example(code)
39
Example(output)
40
Review exercise
41

 What does a page’s encoding refer to?


 What impact does the encoding have on the page?
 What PHP functions, or language constructs, can you use
to send data to the Web browser?
 How does using single versus double quotation marks
differ in creating or printing strings?
 What does it mean to escape a character in a string?
 Search the PHP manual for the $_SERVER variable to
see what other information it contains.
 How are constants defined and used?
42 The End
Dig, dig and dig more!

You might also like