02 - Pemrograman Web - Modul 2 PHP - 20210302
02 - Pemrograman Web - Modul 2 PHP - 20210302
Modul ke-2
M. Udin Harun Al Rasyid, S.Kom, Ph.D
http://udinharun.lecturer.pens.ac.id/
udinharun@pens.ac.id
http://udinharun.lecturer.pens.ac.id/ 1
http://udinharun.lecturer.pens.ac.id 2
Rencana Pembelajaran Semester
Pekan Pembahasan
2 Modul 02 - PHP: install, syntax, comments, variables, Echo/print, data types, strings
4 Modul 04 - PHP: Form, Form handling, Form validation, Form required, Form URL/E-mail,
Form Complete
5 - Modul 05 - PHP: date and time, include, file handling, file open/read, file upload, cookies,
sessions, filters, filter advanced, PHP JSON.
- Presentasi Proposal Final Project Kuliah
6 Modul 06 - PHP: OOP, classes/Objects, constructor, destructor, access modifiers,
inheritance, constants, abstract classes, traits, static method, static properties.
7 Modul 07 - MySQL database: Connect, Create DB, Create table, Insert data, Get last ID,
Insert multiple, Prepared, Select data, Where, Order by, Delete data, Update data, Limit data.
http://udinharun.lecturer.pens.ac.id 3
Rencana Pembelajaran Semester
Pekan Pembahasan
9 Modul 08 - PHP XML Parser: SimpleXML Parser, SimpleXML – Get,
XML Expat, XML DOM
10 AJAX PHP: AJAX database, AJAX XML, AJAX Live search, AJAX Poll
11 Login: Login using social media, Login using google account, Login
using FB account
12 Node JS: intro, modules, HTTP module, file system, URL module, NPM,
Events, upload files, email
13 Node JS MySQL: Get started, Create Db, Create table, Insert into,
Select from, Where, Order by, Delete, Drop table, Update, Limit, Join
14 Node JS MongoDB
15 Demo Final Project
16 - Ujian Akhir Semester (UAS)
- Demo Final Project
http://udinharun.lecturer.pens.ac.id 4
Capaian Pembelajaran
http://udinharun.lecturer.pens.ac.id/ 5
Pokok Bahasan
PHP intro
PHP install
PHP syntax
PHP comments
PHP variables
PHP Echo/print
PHP data types
PHP strings
http://udinharun.lecturer.pens.ac.id/ 6
PHP Introduction
http://udinharun.lecturer.pens.ac.id/ 7
What You Should Already Know
Before you continue you should have a basic
understanding of the following:
HTML
CSS
JavaScript
http://udinharun.lecturer.pens.ac.id/ 8
What is PHP?
PHP is an acronym for "PHP: Hypertext
Preprocessor"
PHP is a widely-used, open source scripting
language
PHP scripts are executed on the server
PHP is free to download and use
http://udinharun.lecturer.pens.ac.id/ 9
What is a PHP File?
PHP files can contain text, HTML, CSS, JavaScript, and PHP code
PHP code is executed on the server, and the result is returned to the
browser as plain HTML
http://udinharun.lecturer.pens.ac.id/ 10
What Can PHP Do?
PHP can generate dynamic page content
PHP can create, open, read, write, delete, and close files on the
server
PHP can collect form data
With PHP you are not limited to output HTML. You can output
images, PDF files, and even Flash movies. You can also output any
text, such as XHTML and XML.
http://udinharun.lecturer.pens.ac.id/ 11
Why PHP?
PHP runs on various platforms (Windows, Linux, Unix, Mac OS X,
etc.)
PHP is compatible with almost all servers used today (Apache, IIS,
etc.)
http://udinharun.lecturer.pens.ac.id/ 12
What's new in PHP 7
PHP 7 is much faster than the previous popular stable release (PHP
5.6)
http://udinharun.lecturer.pens.ac.id/ 13
PHP Installation
What Do I Need?
To start using PHP, you can:
Find a web host with PHP and MySQL support
Install a web server on your own PC, and then install PHP and
MySQL
http://udinharun.lecturer.pens.ac.id/ 14
Use a Web Host With PHP Support:
If your server has activated support for PHP you do not need to do
anything.
Just create some .php files, place them in your web directory, and
the server will automatically parse them for you.
http://udinharun.lecturer.pens.ac.id/ 15
Set Up PHP on Your Own PC
However, if your server does not support PHP, you must:
install a web server
install PHP
http://udinharun.lecturer.pens.ac.id/ 16
Percobaan 01:
<!DOCTYPE html>
<html>
<body>
<?php
echo “Teknik Informatika PENS";
?>
</body>
</html>
http://udinharun.lecturer.pens.ac.id/ 17
http://udinharun.lecturer.pens.ac.id/ 18
PHP Syntax
A PHP script is executed on the server, and the plain HTML result is
sent back to the browser.
PHP script can be placed anywhere in the document.
A PHP script starts with <?php and ends with ?>:
The default file extension for PHP files is ".php".
http://udinharun.lecturer.pens.ac.id/ 19
A PHP file normally contains HTML tags, and some PHP scripting
code.
Below, we have an example of a simple PHP file, with a PHP script
that uses a built-in PHP function "echo" to output the text "Hello
World!" on a web page.
Note: PHP statements end with a semicolon (;).
http://udinharun.lecturer.pens.ac.id/ 20
Percobaan 02:
http://udinharun.lecturer.pens.ac.id/ 21
PHP Case Sensitivity
In PHP, NO keywords (e.g. if, else, while, echo, etc.), classes,
functions, and user-defined functions are case-sensitive.
In the example below, all three echo statements below are equal
and legal.
Note: However; all variable names are case-sensitive!
http://udinharun.lecturer.pens.ac.id/ 22
Percobaan 03:
http://udinharun.lecturer.pens.ac.id/ 23
Look at 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:
http://udinharun.lecturer.pens.ac.id/ 24
Percobaan 04:
http://udinharun.lecturer.pens.ac.id/ 25
PHP Comments
http://udinharun.lecturer.pens.ac.id/ 26
Percobaan 05: Syntax for single-line comments
http://udinharun.lecturer.pens.ac.id/ 27
Percobaan 06: Syntax for multiple-line comments:
http://udinharun.lecturer.pens.ac.id/ 28
Percobaan 07: Using comments to leave out parts of the
code
http://udinharun.lecturer.pens.ac.id/ 29
PHP Variables
Note: When you assign a text value to a variable, put quotes around
the value. Think of variables as containers for storing data.
Note: Unlike other programming languages, PHP has no command
for declaring a variable. It is created the moment you first assign a
value to it.
http://udinharun.lecturer.pens.ac.id/ 30
Percobaan 08:
http://udinharun.lecturer.pens.ac.id/ 31
PHP Variables
A variable can have a short name (like x and y) or a more
descriptive name (age, carname, total_volume).
Rules for PHP variables:
http://udinharun.lecturer.pens.ac.id/ 32
Output Variables
The PHP echo statement is often used to output data to the screen.
The following example will show how to output text and a variable:
http://udinharun.lecturer.pens.ac.id/ 33
Percobaan 09:
http://udinharun.lecturer.pens.ac.id/ 34
Percobaan 10:
The following example will produce the same output as the example above:
http://udinharun.lecturer.pens.ac.id/ 35
Percobaan 11: the sum of two variables.
http://udinharun.lecturer.pens.ac.id/ 36
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:
local
global
static
http://udinharun.lecturer.pens.ac.id/ 37
Global and Local Scope
http://udinharun.lecturer.pens.ac.id/ 38
Percobaan 12: Variable with global scope
http://udinharun.lecturer.pens.ac.id/ 39
http://udinharun.lecturer.pens.ac.id/ 40
A variable declared within a function has a LOCAL SCOPE and can
only be accessed within that function.
You can have local variables with the same name in different
functions, because local variables are only recognized by the
function in which they are declared.
http://udinharun.lecturer.pens.ac.id/ 41
Percobaan 13:
http://udinharun.lecturer.pens.ac.id/ 42
http://udinharun.lecturer.pens.ac.id/ 43
PHP The global Keyword
The global keyword is used to access a global variable from within a
function.
To do this, use the global keyword before the variables (inside the
function):
http://udinharun.lecturer.pens.ac.id/ 44
Percobaan 14:
http://udinharun.lecturer.pens.ac.id/ 45
PHP also stores all global variables in an array called
$GLOBALS[index].
The index holds the name of the variable.
This array is also accessible from within functions and can be used
to update global variables directly.
http://udinharun.lecturer.pens.ac.id/ 46
Percobaan 15
http://udinharun.lecturer.pens.ac.id/ 47
PHP The static Keyword:
To do this, use the static keyword when you first declare the
variable:
Then, each time the function is called, that variable will still have the
information it contained from the last time the function was called.
http://udinharun.lecturer.pens.ac.id/ 48
Percobaan 16
http://udinharun.lecturer.pens.ac.id/ 49
PHP echo and print Statements
With PHP, there are two basic ways to get output: echo and print.
echo and print are more or less the same. They are both used to
output data to the screen.
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.
http://udinharun.lecturer.pens.ac.id/ 50
The PHP echo Statement
Display Text
The following example shows how to output text with the echo
command (notice that the text can contain HTML markup):
http://udinharun.lecturer.pens.ac.id/ 51
Percobaan 17:
http://udinharun.lecturer.pens.ac.id/ 52
Display Variables:
The following example shows how to output text and variables with
the echo statement:
http://udinharun.lecturer.pens.ac.id/ 53
Percobaan 18:
http://udinharun.lecturer.pens.ac.id/ 54
The PHP print Statement
Display Text
The following example shows how to output text with the print
command (notice that the text can contain HTML markup):
http://udinharun.lecturer.pens.ac.id/ 55
Percobaan 19:
http://udinharun.lecturer.pens.ac.id/ 56
Display Variables
The following example shows how to output text and variables with
the print statement:
http://udinharun.lecturer.pens.ac.id/ 57
Percobaan 20:
http://udinharun.lecturer.pens.ac.id/ 58
PHP Data Types
Variables can store data of different types, and different data types can
do different things.
PHP supports the following data types:
String
Integer
Boolean
Array
Object
NULL
http://udinharun.lecturer.pens.ac.id/ 59
PHP String
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:
http://udinharun.lecturer.pens.ac.id/ 60
Percobaan 21:
http://udinharun.lecturer.pens.ac.id/ 61
PHP Integer
An integer data type is a non-decimal number between -
2,147,483,648 and 2,147,483,647.
Rules for integers:
http://udinharun.lecturer.pens.ac.id/ 62
Percobaan 22:
http://udinharun.lecturer.pens.ac.id/ 63
PHP Float
A float (floating point number) is a number with a decimal point or a
number in exponential form.
In the following example $x is a float. The PHP var_dump() function
returns the data type and value:
http://udinharun.lecturer.pens.ac.id/ 64
Percobaan 23:
http://udinharun.lecturer.pens.ac.id/ 65
PHP Boolean
http://udinharun.lecturer.pens.ac.id/ 66
PHP Array
http://udinharun.lecturer.pens.ac.id/ 67
Percobaan 24:
http://udinharun.lecturer.pens.ac.id/ 68
PHP Object
First we must declare a class of object. For this, we use the class
keyword. A class is a structure that can contain properties and
methods:
http://udinharun.lecturer.pens.ac.id/ 69
Percobaan 25:
http://udinharun.lecturer.pens.ac.id/ 70
PHP NULL Value
Null is a special data type which can have only one value: NULL.
http://udinharun.lecturer.pens.ac.id/ 71
Percobaan 26:
http://udinharun.lecturer.pens.ac.id/ 72
PHP Strings
http://udinharun.lecturer.pens.ac.id/ 73
Percobaan 27:
http://udinharun.lecturer.pens.ac.id/ 74
str_word_count() - Count Words in a String
http://udinharun.lecturer.pens.ac.id/ 75
Percobaan 28:
http://udinharun.lecturer.pens.ac.id/ 76
strrev() - Reverse a String
http://udinharun.lecturer.pens.ac.id/ 77
Percobaan 29:
http://udinharun.lecturer.pens.ac.id/ 78
strpos() - Search For a Text Within a String
http://udinharun.lecturer.pens.ac.id/ 79
Percobaan 30:
http://udinharun.lecturer.pens.ac.id/ 80
str_replace() - Replace Text Within a String
http://udinharun.lecturer.pens.ac.id/ 81
Percobaan 31:
http://udinharun.lecturer.pens.ac.id/ 82
Tugas Praktikum
Kerjakan Semua Percobaan PHP modul ke-2 sejumlah 31 Percobaan.
http://udinharun.lecturer.pens.ac.id/ 83
NEXT
PHP Programming
PHP: numbers, constants, operators, if..else..elseif, switch, loops,
functions, arrays, superglobals.
http://udinharun.lecturer.pens.ac.id/ 84
References
https://www.w3schools.com
https://books.goalkicker.com/PHPBook/
https://hackr.io/blog/best-php-books-for-beginners-and-advanced-
programmers
http://udinharun.lecturer.pens.ac.id/ 85
IKLAN STRATEGI BELAJAR DAN
SUKSES MENJADI MAHASISWA
http://udinharun.lecturer.pens.ac.id 86
Strategi Belajar Cek jaringan, kuota,
PC/Laptop/smartphone
Online dan
Membaca dan melihat konten materi dari
Komunikasi yang dosen sebelum pembelajaran dimulai.
Efektif Hadir tepat waktu sesuai jam kuliah.
Ijin ke dosen jika datang terlambat kuliah
atau ada kendala.
Catat hal-hal yang tidak dimengerti dan
tanyakan saat kuliah online sinkron.
Proaktif dalam forum diskusi secara
langsung maupun tidak langsung.
Kerjakan semua tugas tepat waktu.
Jangan sampai tidak mengerjakan tugas.
Pastikan tugas sudah sukses diunggah
secara online.
Berkomunikasi dengan dosen sesuai etika. Perhatikan
Strategi Belajar pemakaian kosakata yang baik, waktu, tempat, alat
komunikasi.
Online dan Diskusi dengan teman/kakak kelas berkaitan dengan
Komunikasi yang tugas dan materi.
Buat forum diskusi online dengan teman seangkatan.
Efektif Ikuti forum komunitas IT di kampus maupun luar kampus.
Ikuti forum BEM/HIMA/UKKI
Aktif dalam kompetisi lomba mahasiswa.
Menambah pengetahuan dari luar kampus secara online:
edX
Coursera
udemy
Khan Academy
MIT Opencourseware
W3schools
Youtube
etc
Siapkan mentalmu dahulu
Atur jadwalmu dengan sebaik mungkin
Lengkapi alat-alat yang menunjang
perkuliahan
Pasang target IPK
Belajar dengan fokus dan kerja keras
Review materi kuliah
Dekat dengan dosen
Tips Sukses Asah skill
Menjadi Mahasiswa Jalin jaringan yang luas
Berorganisasi
Berdoa kepada Allah SWT dan
meminta doa restu dari kedua orangtua