Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Chapter 2-Server Side Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Chapter 2

Server-side programming
What is server side PL?
❑ It is a technique used in web development which involves
employing scripts on a web server which produce a response
for each user's request to the website.
❑ It provides an interface to the user and is used to limit access
to proprietary data and help keep control of the script source
code.
❑ Server-side processing is used to interact with permanent
storage like databases or files.
❑ Examples: Java, JavaScript using Server-side JavaScript
(SSJS), Perl, PHP
D/ce b/n server-side and client-side PL
Client-side programming Server-side programming

web browser running on the user’s local web server software (e.g. Apache) that runs
machine. on server hardware.
processing takes place on the end users user's request is fulfilled by running a script
computer. directly on the web server
interface to user and links to server-side interface to databases or other data stores on
language the server

Access data from server-side Access data directly from database sources

It can be dynamic/static content It is always dynamic content

User directly interacts with. User interacts via client-side requests

E.g:- HTML E.g:- PHP


PHP
❑ It is an acronym for "PHP Hypertext Preprocessor"

❑ It is used for creating dynamic and interactive websites.

❑ PHP files can contain text, HTML, CSS, JavaScript, and


PHP code

❑ PHP code are executed on the server, and the result is


returned to the browser as plain HTML

❑ PHP files have extension ".php"


…cont. PHP
❑ It can create, open, read, write, delete, and close files on the
server
❑ It can collect data from form, and it can send and receive
cookies
❑ It can add, delete, modify data in your database and supports a
wide range of databases
❑ It can restrict users to access some pages on your website and
it can encrypt data
❑ It runs on various platforms (Windows, Linux, UNIX, Mac
OS X, etc.)
syntax
<?php PHP code! ?>
Or you can place PHP code within
<?php and ?> tags, normally within the context of
some HTML.
Example
<html><body><h1>This is HTML.</h1>
<?php PHP code! ?>
<p>More HTML</p>
</body></html>
.
❑ A PHP script can be placed anywhere in the document.

❑ PHP statements are terminated by semicolon (;).

❑ In PHP there are two output printing values:

1. echo: - can output one or more strings

❑ echo or echo().

❑ It is faster compared to print as echo does not return any


value.

2. print: - can only output one string, and returns always 1

❑ print or print().
Using echo and print
Example: using echo
<?php
echo "<h2>PHP is fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This", " string", " was", " made", " with multiple
parameters.";
?>
Example: using print
<?php
print "<h2>PHP is fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
Comments

PHP supports three ways of commenting:

1. // This is a single line comment

2. # This is also a single line comment

3. /* This is a multiple lines comment block that

spans over more than one line */


PHP Case Sensitivity
❑ In PHP, all user-defined functions, classes, and keywords
(e.g. if, else, while, echo, etc.) are NOT case-sensitive.

❑ However; in PHP, all variables are case-sensitive.

❑ In 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):
example:-
Example
<!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
PHP variables:
❑ 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, _ )
❑ Variable names are case sensitive ($y and $Y are two different
variables)
❑ PHP has no command for declaring a variable.
❑ A variable is created the moment you first assign a value to it:
<?php
$txt="Hello world!";
$x=5;
$y=10.5;
?>
php variable properties
❑ PHP automatically converts the variable to the
correct data type, depending on its value.

❑ In other languages such as C, C++, and Java, the


programmer must declare the name and type of
the variable before using it.
PHP Variables Scope
In PHP, variables can be declared anywhere in the
script.
The scope of a variable is the part of the script where
the variable can be referenced/ used.
PHP has three different variable scopes:
1. local
2. Global
3. static
Local and Global Scope

❑ A variable declared outside a function has a


GLOBAL SCOPE and can only be accessed
outside a function.
❑ A variable declared within a function has a
LOCAL SCOPE and can only be accessed within
that function.
The following example tests variables with local and
global scope:
.
<?php
$x=5; // global scope
function myTest() {
$y=10; // local scope
echo "<p>Test variables inside the function:</p>";
echo "Variable x is: $x";
echo "<br>";
echo "Variable y is: $y"; }
myTest();
echo "<p>Test variables outside the function:</p>";
echo "Variable x is: $x";
echo "<br>";
echo "Variable y is: $y";
?>
❑ In the example above there are two variables $x and $y
and a function myTest().
❑ $x is a global variable since it is declared outside the
function and $y is a local variable since it is created inside
the function.
❑ When we output the values of the two variables inside the
myTest() function, it prints the value of $y as it is the
locally declared, but cannot print the value of $x since it
is created outside the function.
❑ Then, when we output the values of the two variables
outside the myTest() function, it prints the value of $x, but
cannot print the value of $y since it is a local variable and
it is created inside the myTest() function.
❑ And you can make variable within function global by
using global keyword.
PHP the static Keyword
❑ Normally, when a function is completed or executed, all of
its variables are deleted.
❑ However, sometimes we want a local variable NOT to be
deleted. We need it for a further job. To do this, use the
static keyword when you first declare the variable:
❑ Example:- <?php
function myTest() {
static $x=0;
echo $x;
$x++; }
myTest();
myTest();
myTest();
?>
Manipulate Strings
❑ A string is a sequence of characters, like "Hello
world!".
❑ A string can be any text inside quotes. You can use
single or double quotes:
Example:- <?php
$x = "Hello world!";
echo $x;
echo "<br>";
$x = 'Hello world!';
echo $x;
?>
Concatenating Strings

❑ It refers to the appending of one item onto another.

❑ The period (.) is the operator for performing this


action, and it’s used like so:

$s1 = 'Hello, ';

$s2 = 'world!';

$greeting = $s1 . $s2;


❑ Because of the way PHP deals with variables, the same
effect could be accomplished using $greeting = "$s1$s2";
Adjusting String Case
❑ A handful of PHP functions are used to change the case of
a string’s letters:

. ucfirst() capitalizes the first letter of the string.

. ucwords() capitalizes the first letter of words in a string.

. strtoupper() makes an entire string uppercase.

. strtolower() makes an entire string lowercase.


Operators in PHP
1. Arithmetic Operators
Assume variable A holds 10 and variable B holds 20 then:
Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A –B will give -10
* Multiply both operands A * B will give 200
/ Divide numerator by de-numerator B / A will give 2
% Modulus operator and remainder of after an integer division
B% A will give 0
++ Increment operator, increases integer value by one A++ will
give 11
-- Decrement operator, decrease integer value by one A - - will
give 9
2. Comparison Operators

❑ Assume variable A holds 10 and variable B holds 20


then Operator Description
(A == B) is not true.
(A! = B) is true.
(A > B) is not true.
(A < B) is true.
(A >= B) is not true.
(A <= B) is true.
3. Logical Operators
❑ Assume variable A holds 10 and variable B holds 20
then:-

(A and B) is true.

(A or B) is true.

(A && B) is true( If both the operands are nonzero then


condition) becomes true.)

(A || B) is true.

!!(A && B) is false.


4. Assignment Operators

• C = A + B will assign value of A + B into C

• C += A is equivalent to C = C + A

• C -= A is equivalent to C = C - A

• C *= A is equivalent to C = C * A

• C /= A is equivalent to C = C / A

• C %= A is equivalent to C = C % A
5. Conditional Operator

❑ This first evaluates an expression for a true or false value


and then execute one of the two given statements
depending upon the result of the evaluation.

The conditional operator has this syntax:

?: Conditional Expression If condition is true? Then value


x: otherwise value y
Set a PHP Constant
❑ To set a constant, use the define() function - it takes
three parameters:

❑ The first parameter defines the name of the constant,

❑ The second parameter defines the value of the


constant, and the optional third parameter specifies
whether the constant name should be case-insensitive.
Default is false.
.
❑ The example below creates a case-sensitive
constant, with the value of "Welcome to Alliance
college!":
Example
<?php
define("GREETING", "Welcome to Alliance
college!");
echo GREETING;
?>
.
The example below creates a case-insensitive constant,
with the value of "Welcome to
Alliance college!":
Example
<?php
define("GREETING", "Welcome to Alliance college!",
true);
echo greeting;
?>
Working with PHP
➢ To start using PHP, you can:

❑ Install a web server on your own PC

❑ Install PHP

❑ Install a database, such as MySQL

❑ Or you can have and install service that comprises


all things together such as WAMP, XAMPP.
..

You might also like