Chapter One
Chapter One
- Dynamic Content
Server Side vs Client Side
Programming Languages
Client side scripts
• Are executed by the client, particularly by the
browser
• They are used for appearance and behavior of a
web page
• It has little or no access to the underlying operating
system
• Some features may not work or properly rendered
on specific browser(s)
Cont…
• What is server side scripting language ?
– a server-side scripting language is a script that is executed
on the server as apposed to client-side scripting languages
like JavaScript
– It requires server-side scripting engine
– It has full access to the server operating system
– there are variety of them like PHP, Python, Ruby, C#,
NodeJS, etc….
When to use server-side scripting
• When developing Dynamic web pages
• Authentication, authorization and session
tracking.
• Template-driven page generation. Including
repeated content like header/footers and
navigation menus around the “content area”
of a web page.
Cont.….
• Personalization and customization of content based
on authentication and authorization. This also
includes the serving of content based on the content
of the page (e.g. YouTube, Amazon, Facebook) or the
browsing behavior of the user.
• Handling POST form input
• Communication with other programs, libraries and
APIs – e.g. sending out e-mail
PHP
• It is server-side scripting language
• It is not visible to visitors
• It should be saved with .php extension
• It is denoted in a page with opening and
closing tags as follows
<?php
?>
• Every statement ends with a semicolon
Cont.…
• Example
<?php
// First line of code goes here;
// Second line of code goes here;
// Third line of code goes here;
?>
• Commenting
– Single line (//) or multiple line (/* -----*/)
Cont.….
• Creating the first program.
<html>
<head>
</head>
<body>
<?Php
echo “ hello there”;
?>
</body>
</html>
Cont.….
• PHP script can be emended into HTML page.
• Pitfalls:-
– Double quotes
• Use \” escaping character or single quote.
– Remember that you still have to follow PHP rules,
even though you’re coding in HTML.
• echo “<font size=\”2\”>”;
– Don’t try to cram too much HTML into your PHP
sections.
Variables and Constants
• It is case-sensitive.
• Variable declaration rules are applied.
– A variable starts with the $ sign, followed by the name of
the variable
– 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)
Cont.…..
• A value in a variable is displayed using echo or print
statement.
• PHP also supports static variables. E.g. static $x = 0
• Variables have global and local scopes.
• define (“FAVMOVIE”, “The Life of Brian”);
e.g. echo “My favorite movie is “;
echo FAVMOVIE;
• Variables are denoted with a dollar sign ($).
• $num = 5;
• It is loosely typed language.
PHP data types
• PHP supports the following data types:
– String
– Integer - It is a number between -2,147,483,648 and
+2,147,483,647.
– Float (floating point numbers - also called double)
– Boolean
– Array
– Object
– NULL
• Reading Assignment : - string methods.
Cont.….
• Built in math functions.
– Rand([min],[max]);
– Ceil(number) Round numbers up to the nearest
integer;
– Floor(number) Round numbers down to the
nearest integer;
– Max(arg1, arg2);
– min(arg1,arg2);
Passing variables between pages
• $_GET
• $_POST
• $_REQUEST
• $_FILES
• $_SESSION
Control Structure
• Control structures determine the flow of code
within an application.
• Conditional statement
if (expression) {
statement
}
Cont.…..
if (expression) {
statement
}
else{
statement
}
Cont.….
switch($variable) {
case “expression 1":
statement(s)
break;
case “expression 2":
statement(s)
break;
default:
statement(s)
}
Looping
• While
while (expression) {
statements
}
• For
for(expression1;expression2;expressio
n3) {
statements
}
Break and Continue
• Encountering a break statement will
immediately end execution of a do...while, for,
foreach, switch, or while block.