You are on page 1of 29

04.

Server-side Programming
Server vs Client

○ Server :
a software which process request from a client
○ Web Server :
a software which process request via HTTP from client and
returns a web page (Apache, Lighttpd, Nginx, Oracle, etc.)
○ Client :
an interface/software from end-user (Web Browser, mobile
apps, software, games, etc.)
Server-side programming vs
Client-side programming

3
Server-side Programming

○ In web programming, there are several server-side


programming language
□ PHP
□ Node.js
□ GoLang
□ Java
□ Python

4
Server Application

5
Server Application

○ Apache : for handling user request


○ MySQL/MariaDB : for storing/reading data
○ PHP : for parsing PHP script

○ Complete package : WAMP, LAMP, MAMP, XAMPP


PHP

7
PHP

○ PHP’s purpose is to give website ability to parse a


page dynamically
○ PHP script is written directly on HTML script
<?php
//This is a php script
?>

8
Standard Output

○ PHP standard output is translated into html

echo "Hello World";


print "Hello World"; print("Hello World");
print_r("Hello World");
var_dump("Hello World");

9
Variable identifier

○ In PHP, variable identifier written with dollar sign

$x = 1;
$y = "Hello World";
$z = true;

10
Variable identifier

○ You can print variable directly using echo

$PI = 3.14;
echo "PI is $PI";
//Pi is 3.14

11
Variable identifier

○ Or joint them using dot

$PI = 3.14;
echo "PI is ".$PI;

12
Array

○ Two ways of writing array in PHP


$array1 = array(1, 2, 3, 4);
$array2 = [1, 2, 3, 4];

echo $array1[1];
print_r($array1);
var_dump($array1);
13
Array

○ Adding new value to array

$array1 = array(1, 2, 3, 4);


array_push($array1, 5);
$array1[] = 6;

14
N-Dimensional Array

$array3 = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9, 10]
];
$array3[] = [11, 12, 13, 14];
echo $array3[1][2];

15
Looping and Switch

16
For Loop

for($i = 0; $i < count($array1); $i++){


echo $array[$i];
}

foreach($array1 as $a){
echo $a;
}
17
While do, do while

$i = 0; $i = 0;
while($i < count($array1)){ do{
echo $array1[$i]; echo $array1[$i];
echo "<br>"; echo "<br>";
$i++; $i++;
} }
while($i < count($array1))

18
If then else

if($i != 0){ if($i < 5){


echo "true"; echo "true";
}else{ }else if(5 <= $i and $i < 10) {
echo "false"; echo "false";
} }else{
echo "unknown";
}

19
Function

20
Function

function familyName($fname) {
echo "$fname Refsnes.<br>";
}

familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
Variable Scope

22
Variable Scope

○ Global variable :
□ variable that declared outside a function (scope)
○ Local variable :
□ variable that declared in a function (scope)
○ Superglobals variable :
□ Variable that always exists in all scope
Superglobals

○ $_GET ○ $_FILES
○ $_POST ○ $_ENV
○ $_COOKIE ○ $_SERVER
○ $_SESSION ○ $_REQUEST

All superglobals variables are associative array

24
HTML Forms

25
Form Element

○ To create form, we can use <form> element


<form action="url" method="post | get">
.
form elements
.
</form>

26
Form Element

○ <label> ○ <fieldset>
○ <input> ○ <legend>
○ <select> ○ <datalist>
○ <option> ○ <output>
○ <textarea> ○ <optgroup>
○ <button>

27
Label

○ Label are useful for small screen (smartphone)


○ Match the label with the input id

<label for="fname">First name:</label>

28
Input

○ Input are used to contain user input


<label for="user">Username:</label><br>
<input type="text" id="user"><br>
<label for="pass">Password:</label><br>
<input type="password" id="pass"><br>
<input type="button" value="submit">

29

You might also like