CHAPTER 5 SERVER SIDE SCRIPTING -...
Transcript of CHAPTER 5 SERVER SIDE SCRIPTING -...
http://fsktm2.uthm.edu.my/azizulzamri
CHAPTER 5
SERVER SIDE SCRIPTING
PART 1 OF 3
Azizulzamri bin Muhamed Amin
[Principles | Operation & Expression | Output]
SERVER SIDE SCRIPTING
The scripts can be written in any of a number of server-side scripting
languages available (Example : PHP, ASP.NET).
Server-side scripting differs from client-side scripting which are run client-
side in the web browser.
Server side code is used to retrieve and generate content for the dynamic
pages (Example : to retrieve the content from database).
SERVER SIDE SCRIPTING
1) The user requests a web page from the web server.
2) The web server executes the code (no PHP code ever reaches the user) in
the web page and generates an HTML content for that page.
Note :
the code is called "server side code/script" because it is executed by the
web server.
the page containing the code is called dynamic page
3) Information it outputs (the html content) is sent back to the browser so that
it can be displayed to the user.
SERVER SIDE SCRIPTING
Note:
By the time the data get's to the user's browser, there is no PHP code left, only
the HTML code remains.
That's why if attempt to run your PHP documents on a computer with no web
server will only display the code instead of its output.
User requests a web page from the web server
Web server executes the code and generate
HTML content
Output sent back to the
client browser
Ex : Hello welcome to my
shop.com
HOW SERVER SIDE SCRIPTS WORK?
Ex :
http://www. myshop.com
Ex : <?php
echo (“Hello welcome to
my shop.com!”);
?>
Differences of Scripting Lang.
SERVER SIDE SCRIPTING CLIENT SIDE SCRIPTING
PLATFORM Web server Web browser
EXECUTION Before page load After page load
SOURCE CODE Unseen Visible
NEED FOR SERVER Yes No
BASIC SYNTAX
PHP stands for Hypertext Preprocessor and is a server-side language.
The script is run on your web server, not on the user's browser
PHP is relatively new (compared to languages such as Java) but is quickly
becoming one of the most popular scripting languages on the internet.
PHP scripts are always enclosed in between two PHP tags.
This tags tells the web server to parse the information between the tags as
PHP.
BASIC SYNTAX
Following are different style for writing PHP script:
1) Everything between the <?php and ?> is read as PHP code.
2) The statement can also be phrased as simply <? if desired.
Anything outside of these PHP tags is read as HTML, hence we can easily
switch between PHP and HTML as needed.
BASIC SYNTAX
The three different forms are as follows:
Style 1 <?php
PHP Code In Here
?>
Style 2 <?
PHP Code In Here
?>
Style 3 <script language="php">
PHP Code In Here
</script>
BASIC SYNTAX
There are two ways to use HTML on PHP page.
1) The first way is to put the HTML outside of PHP tags.
<?php -and- ?> are put it in the middle HTML.
2) The second way to use HTML with PHP is by using PRINT or ECHO.
This method you include the HTML inside of the PHP tags.
Each code line in PHP must end with a semicolon. The semicolon is a
separator and is used to distinguish one set of instructions from another.
BASIC SYNTAX – Style 1
<html>
<body>
<?php
//your php code here
?>
<b>Here is some more HTML</b>
<?php
//more php code
?>
</body>
</html>
Example :
<html>
<body>
<?php
echo("Hello World!“);
?>
<h1>My first PHP page</h1>
<?php
echo(“Good Morning!“);
?>
</body>
</html>
BASIC SYNTAX – Style 2
Example :
<?php
echo("<html>“);
echo("<title>Style 2</title>“);
echo("<body>“);
echo("<b>My Example</b>“);
print("<i>Hello!</i>“);
echo("<body>“);
echo("</html>“);
?>
This is a nice
quick method if
you only have a
line or so to do.
echo / echo()
The echo() function outputs one or more strings.
Note: The echo() function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo(), using parentheses will generate a parse error.
Tip: The echo() function is slightly faster than print().
Tip: The echo() function also has a shortcut syntax. Prior to PHP 5.4.0, this syntax only works with the short_open_tag configuration setting enabled.
Example
In PHP there are two basic ways to get output: echo and print.
echo(strings)
The differences are small: echo has no return value while print has a return value of 1 so it can
be used in expressions. echo can take multiple parameters (although such usage is rare) while
print can take one argument. echo is marginally faster than print.
The echo statement can be used with or without parentheses: echo or echo().
Parameter Description
strings Required. One or more strings to
be sent to the output
<!DOCTYPE html>
<html>
<body>
<?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.";
?>
</body>
</html>
Print / print()
<!DOCTYPE html> <html> <body> <?php print "<h2>PHP is Fun!</h2>"; print "Hello world!<br>"; print "I'm about to learn PHP!"; ?> </body> </html>
The print statement can be used with or without parentheses: print or print().
Index , form , print data
1. Index.php
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html> 2. form
<form action="action.php" method="post"> <p>Your name:
<input type="text" name="name" /></p> <p>Your age:
<input type="text" name="age" /></p> <p><input
type="submit" /></p> </form>
3. Print data from form
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
.
BASIC SYNTAX - COMMENT
A comment is the portion of a program that exists only for the human
reader and stripped out before displaying the programs result.
All text that appears between the start of the comment and the end will be
ignored.
There are two commenting formats in PHP:
1) Single-line comments:
generally used for single line comments (short explanations of code).
2) Multi-lines comments:
generally used to provide more detailed explanations (go over one
line) when necessary.
BASIC SYNTAX - COMMENT
A single line comment is written as follows:
// Your comment can go in here
Everything after the // will be ignored when the script is executed.
Single line comment can also be placed on the end of a statement.
Example:
print("Hello $name“); // Welcome to the user
BASIC SYNTAX - COMMENT
The multiline style of commenting is the same as in C.
A multiline comment is written as follows:
/*
The following piece of code will take the input
the user gave and will check that it is valid before
adding it to the database
*/
Anything between the /* and the */ will be ignored.
It is important to always close this type of comment as not doing so could
make the script not working.
BASIC SYNTAX - COMMENT
Example :
<?php
echo("Hello World!“); // example of single line comment
/*
This echo statement will print out
a hello world message
*/
echo("Hello World!“);
?>
BASIC SYNTAX - variable
As with other programming languages, PHP allows you to define variables.
All variable begin with a $ sign.
In PHP there are several variable types, but the most common is called a
string.
Variables can hold text and numbers.
BASIC SYNTAX - variable
To assign some text to a string you would use the following code:
$welcome_text = "Hello and welcome to my website.";
Variables are case sensetive so $Welcome_Text is not the same as $welcome_text
When assigning numbers to variables you do not need to include the quotes so:
$user_id = 987;
would be allowed.
BASIC SYNTAX - variable
Rules for PHP variables:
A variable starts with the $ sign, followed by the name of the variable
A variable name must begin with a letter or the underscore character
A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
A variable name should not contain spaces
Variable names are case sensitive ($y and $Y are two different
variables)
OPERATION AND EXPRESSION
Expressions are used to perform operations and give an answer to a single
value.
Expressions consist of two parts : operators and operands.
Operands can be:
1) Variables
2) Numbers
3) Strings
4) Boolean values
5) other expressions.
OPERATION AND EXPRESSION
a = 3 + 4
b = (3 + 4) / 2
expression (3+4) is used as an
operand along with b and 2
Example 1: Example 2:
operands operands operators operators
OPERATION AND EXPRESSION
Operators are used perform operations on operands (variables and
values).
Following are major categories of operator supported by PHP.
1) Arithmetic Operators
2) Comparison Operators
3) Logical Operators
4) Assignment Operators
OPERATION AND EXPRESSION
Arithmetic operators apply mathematical functions to the operands.
Operator Name Description Example Result
+ Addition Sum of x and y
Example : x + y 2 + 2 4
- Subtraction Subtract of x and y
Example : x - y 5 - 2 3
* Multiplication Multiplication of x and y
Example : x * y 5 * 2 10
/ Division Division of x and y
Example : x / y 15 / 5 3
% Modulus Remainder of x divided by y
Example : x % y 5 % 2
10 % 8
10 % 2
1
2
0
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT
OPERATION AND EXPRESSION
<?php
$a = 42;
$b = 20;
$c = $a + $b;
echo("Addition Operation Result: $c“);
$c = $a - $b;
echo("Subtraction Operation Result: $c“);
$c = $a * $b;
echo("Multiplication Operation Result: $c“);
?>
Output :
Addition Operation Result: 62
Subtraction Operation Result: 22
Multiplication Operation Result: 840
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT
OPERATION AND EXPRESSION
Comparison operator compare one operand to another operand.
Comparison operators are used inside conditional statements and evaluate
to either true or false.
They give the ability to compare whether elements are equal, identical, less
than or greater than one another (with some other variations).
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT
OPERATION AND EXPRESSION
Operator Name Description Example Result
== Equal True if x is equal to y
Example : x == y 5==8
FALSE
!=
<>
!==
Not equal True if x is not equal to y or
they are not of same type
Example : x != y or x <> y or x !== y
5!=8
5<>8
5!=="5"
TRUE
> Greater than True if x is greater than y
Example : x > y 5>8
FALSE
< Less than True if x is less than y
Example : x < y 5<8
TRUE
>= Greater than
or equal to
True if x is greater than or
equal to y
Example : x >= y
5>=8 FALSE
<= Less than or
equal to
True if x is less than or
equal to y
Example : x <= y
5<=8 TRUE
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT
OPERATION AND EXPRESSION
<?php
$a = 42;
$b = 20;
if($a==$b)
{
echo("TEST1 : a is equal to b“);
}
else
{
echo("TEST1 : a is not equal to b“);
}
?> Output :
TEST1 : a is not equal to b
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT
OPERATION AND EXPRESSION
Logical operators compare elements using comparisons AND, OR, and the
LIKE.
Operator Name Description Example Result
&& AND True if both x and y are
true
Example : x && y
x=6 ,y=3
(x < 10 && y > 1)
TRUE
|| OR True if either or both x
and y are true
Example : x || y
x=6 , y=3
(x==5 || y==5)
FALSE
! NOT True if x is not true
Example : !x x=6, y=3
!(x==y)
TRUE
XOR XOR True if either x or y is
true, but not both
Example : x xor y
x=6 ,y=3
(x==6 xor y==3)
FALSE
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT
OPERATION AND EXPRESSION
<?php
$a = 42;
$b = 0;
if($a<10 && $b>1)
{
echo("TEST1 : Both a and b are true“);
}
else
{
echo("TEST1 : Both or either a or b is false);
}
?> Output :
TEST1 : Both or either a or b is false
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT
OPERATION AND EXPRESSION
The assignment operator is used to assign values to variables in PHP.
Such an assignment of value is done with the "=", or equal character.
Operator Name 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
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT
OPERATION AND EXPRESSION
Assignment Details Description Example Result
x = y x = y The left operand gets set to
the value of the expression
on the right
x=6, y=3
x = y
x=3
y=3
x += y x = x + y Addition x=6, y=3
x += y
x=9
y=3
x -= y x = x - y Subtraction x=6, y=3
x -= y
x=3
y=3
x *= y x = x * y Multiplication x=6, y=3
x *= y
x=18
y=3
x /= y x = x / y Division x=6, y=3
x /= y
x=2
y=3
x %= y x = x % y Modulus x=6, y=3
x %= y
x=0
y=3
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT
OPERATION AND EXPRESSION
<?php
$a = 42;
$b = 20;
/* Assignment operator */
$c = $a + $b;
echo("Addition Operation Result: $c“);
/* c value was 42 + 20 = 62 */
$c += $a;
echo("Add AND Assignment Operation Result: $c“);
?> Output :
Addition Operation Result: 62
Add AND Assignment Operation Result: 104
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT
OUTPUT
There are 4 different ways of outputting text to the browser:
1) echo("Text here");
2) echo "Text here";
3) print("Text here");
4) print "Text here";
All of these do the same thing where the information to be printed is
contained between the quotation mark.
However, aapparently in very large programs that are simply outputting
text, the ECHO statement will run slightly faster.
OUTPUT
Example:
<?php
echo("Hello World");
echo “Hello World";
print("Hello World");
print "Hello World";
?>
Echo and print commands can prints either a string variable or quotes.
OUTPUT – outputting string
Echo and print uses quotes to define the beginning and end of the string.
Hence, to use a quotation mark inside echo or print, the escape character; a
backslash (\) must be used:
The backslash will tell PHP that the quotations are to be used within the
string and NOT to be used to end echo's string.
OUTPUT – outputting string
Example :
<?php
// This won't work because of the quotes around specialH5!
echo "<h5 class="specialH5">Hello World!</h5>";
// OK because we escaped the quotes!
echo "<h5 class=\"specialH5\">Hello World!</h5>";
// OK because use of apostrophe ‘ instead
echo "<h5 class='specialH5'>Hello World!</h5>";
?>
OUTPUT – outputting variable
To output variable values, no quotations are required, even if the variable
hold a string.
Example :
<?php
$my_string = "Hello, my name is: ";
$my_number = 4;
$my_letter = a;
echo $my_string;
echo $my_number;
echo $my_letter;
?>
Output :
Hello, my name is: 4a
OUTPUT – outputting variable & text
Outputting variables can also be done by placing the variable inside of
double-quoted strings (e.g. "string here and a $variable").
Putting a variable inside the quotes (" ") tells PHP to take the string value of
that variable and use it in the string.
Example:
<?php
$my_string = "Hello, my name is: ";
echo "$my_string Bob";
?>
Output :
Hello, my name is: Bob