Server Side Internet
Programming
Introduction to PHP, Setup, and Basics
Server-Side Programming (Backend)
- Opposite of client side programming (frontend)
- Handles all logics, calculations, and data processing
- Does not show anything to user, just process data
- It helps us in
- Data Validation
- Processing Efficiency
- Business Process Handling
HTTP Request
Client could ask for data from the server by calling certain protocol, but
in the web, the most used protocol is HTTP (HyperText Transfer
Protocol). When you access a certain website, the browser will send
HTTP Request which will be processed by the server.
The request includes:
- URL that defines specific part of the web
- HTTP Method that specifies the activity
- Other additional information such as cookies, URL parameters, etc
HTTP Methods
GET : to get a resource
POST : create a new resource
HEAD : get metadata
PUT : update existing resource
DELETE : delete specified resource
Other methods such as PATCH, CONNECT, etc
Let’s try
1. Open PUIS (puis.president.ac.id) or any website
2. Right click and click “Inspect” or “Inspect Element”
3. Go to “Network” tab, click on the first item found
4. Look at the right side of the bar
Static Sites (only Frontend/Client Side)
Dynamic Sites (supported by backend)
Need your opinion here
What do you think about these applications? Which one is static which
one is dynamic?
1. tokopedia.com
2. puis.president.ac.id
3. president.ac.id
4. berkshirehathaway.com
PHP: Hypertext Preprocessor
- Created by Rasmus Lerdorf in 1995
- Previously called “Personal Home Page”, now “PHP: Hypertext
Preprocessor”
- Used for server-side programming to handle server logic, e.g. uploading
data, saving data, process data, and so on
- PHP is still relevant, despite of the rise of other languages
- Have 3 major versions: PHP 5, PHP 7, and PHP 8
- File extension of PHP is “.php”
W3Techs reports that, as of October 2022, "PHP is used by 74.4% of all the
websites whose server-side programming language we know”
Installing Tools
To start working with this course, we should install the following
applications:
1. PHP
2. MySQL
3. Apache Web Server
Or alternatively, we could install all the softwares from the bundle
Laragon
And also install “composer”
Download
1. Laragon from https://laragon.org/
2. Composer from https://getcomposer.org
Laragon
Composer
How to run the PHP file?
When we use Laragon, there will be a directory located in C://
(depending on where you install it) called → C:/laragon/www
The one above is where we put our PHP code in order to be executed.
To see the result, you could access “localhost” on your web browser.
Now, run your Laragon, turn on Apache, and let’s code in PHP
Hello World in PHP
<?php Notice we use
// this is a simple program <?php ?> tags
echo("Hello World<br/>");
print('PHP Basics');
?>
PHP: Comments
<?php
/**
* this is a multiline comment
*/
// this is a one line comment
# this is a one line comment (new)
?>
PHP: Data Types and Variables
- PHP is similar to JavaScript in terms of data type
- Data types in PHP:
1. Integer
2. Float (double)
3. String
4. Boolean
5. Array
6. Object
7. NULL
PHP: Data Types and Variables
In PHP, a variable does not need to have a data type (unlike C, C++, or Java)
Rules:
- Must start with dollar ($) sign, followed by the name
- May start with letter or underscore after $, not a number
- Must contain only alphanumeric characters and underscore (a-z, A-Z, 0-9,
_)
- Variables are case sensitive ($age and $AGE are different)
PHP: Data Types and Variables
<?php
$name = "John";
$age = 20;
$length = 10.5;
$width = 5.2;
$area = $length * $width;
Notice we could add string by
echo($name . " is " . $age); using “.” (dot)
// OR
echo("$name is $age"); Or put the variable inside string,
this only works using double
?> quote
PHP: Constants
- Constant is variable that cannot be changed
- Does not need $ sign
- Two ways:
- const keyword
- define() function
PHP: Constants
<?php
// method 1: using const
const PI = 3.14;
PI = 22 / 7; // error!
echo(PI);
// method 2: using define
define("PI", 3.14);
echo(PI);
?>
PHP: Operators and Operands (Arithmetic)
PHP: Operators and Operands (Assignment)
PHP: Operators and Operands (Comparison)
Example of using spaceship operator
<?php
echo((1 <=> 2) . "<br/>");
echo((1 <=> 1) . "<br/>");
echo((1 <=> 0) . "<br/>");
?>
What can you conclude?
PHP: Operators and Operands (Increment and Decrement)
PHP: Operators and Operands (Logical)
PHP: Operators and Operands (String and Conditional)
PHP: Output
- Use echo or print function
- Both functions may omit the parentheses
<?php
$x = 10;
$y = 5;
echo("Hello World<br/>");
echo $x * $y . "<br/>";
print("PHP Basics<br/>");
print "PHP Basics<br/>";
?>
PHP: Strings
- Could be single quoted ('John') or double quoted ("John")
- String is an “array” of characters, so we could use array approach in a
string
- To connect one string to another, use (.) dot operator
- example: echo($name . " is " . $age)
- We call this “string concatenation”
- To add special characters such as tab, newline,
quotation, use “escape characters”
PHP: Introduction to Form
A simple introduction to send data from HTML to PHP server to process. We
use 2 different HTTP Methods here: GET and POST
1. GET will show the parameter data in the URL (BE CAREFUL)
2. POST will hide the parameter data from being displayed
(RECOMMENDED)
Follow the steps:
3. Create a file called as form.html in a directory
4. Create another file called as welcome.php (act as the server) in the
same directory
File: form.html
<!DOCTYPE html>
<html lang="en">
<body>
<form action="welcome.php" method="get">
<input type="text" name="username" placeholder="Username"/><br/>
<input type="email" name="email" placeholder="Email"/><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
File: welcome.php
<?php
echo("Welcome, " . $_GET["username"] . "<br/>");
echo("Your email is " . $_GET["email"] . "<br/>");
?>
Everything inside $_GET is the “name” of the
form input!
Notice the URL!
After you click submit, notice the URL and see what happen
Now change the method to post in form.html
And also change the $_GET into $_POST in the welcome.php
File: form.html
<!DOCTYPE html>
<html lang="en">
<body>
<form action="welcome.php" method="post">
<input type="text" name="username" placeholder="Username"/><br/>
<input type="email" name="email" placeholder="Password"/><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
File: welcome.php
<?php
echo("Welcome, " . $_POST["username"] . "<br/>");
echo("Your email is <b>" . $_POST["email"] . "</b><br/>");
?>
Notice here we could use HTML tag inside the PHP
String!
When to use GET or POST?
It depends on the situation.
Use GET if:
- Wants to provide a public data (not sensitive)
- Wants to let user bookmark the URL
- Wants to limit the URL (max 2000 characters)
Use POST if:
- Wants to provide a private data (sensitive)
- Wants to send unlimited data to the server
- Wants to send complex data like file and image
Exercise
Create a simple application
1. Accepts a number
2. Send number to server (PHP)
3. Show the square value of the number
E.g.
References
https://www.w3schools.com/php/php_operators.asp
https://www.php.net/