Anda di halaman 1dari 37

PHP basic

PHP: Hypertext
Processor
Bagus Made Sabda N., S.Si., M.Eng.

STIKOM Bali 2017


Pengantar (1)

PHP adalah bahasa pemrograman script serverside yang


didesain untuk pengembangan web, tetapi juga bisa
digunakan sebagai bahasa pemrograman umum
PHP pertama kali dikembangkan pada tahun 1995 oleh
Rasmus Lerdorf
PHP dirilis dalam lisensi PHP License, yang bersifat
gratis atau tidak dikenakan biaya.

STIKOM Bali 2017


Pengantar (2)

Awalnya PHP adalah singkatan dari Personal Home Page


Menjadi dasar dari program CMS (Content Management
System) populer seperti Joomla, Drupal,dan WordPress
Disebut juga sebagai Scripting Language yang
merupakan Bahasa pemrograman yang tidak
memerlukan lingkungan khusus dan biasanya disisipkan
pada Bahasa pemrograman

STIKOM Bali 2017


Sejarah

PHP/FI : Personal Home Page/Forms Interpreter 2 (1996)


PHP: Hypertext Preprocessor 3 (1997)
PHP: Hypertext Preprocessor 4 (2000)
PHP: Hypertext Preprocessor 5 (2004)
PHP: Hypertext Preprocessor 6 (2005) sudah ditutup
PHP: Hypertext Preprocessor 7 (2014)

STIKOM Bali 2017


Instalasi Web Server Apache, PHP
dan MySQL dengan XAMPP

membutuhkan 3 buah program, yakni:


1. Aplikasi PHP itu sendiri
2. Aplikasi web server
3. Aplikasi web browser
Web server adalah aplikasi tempat PHP berjalan.
Saat ini web server yang sering digunakan adalah
Apache Web Server yang bersifat Open Source

STIKOM Bali 2017


Menjalankan Web Server

Apache Melalui XAMPP


START > All Program > XAMPP > XAMPP Control Panel
Control Panel

STIKOM Bali 2017


Cara menjalankan File PHP

File php yang akan dijalankan harus berada di dalam


folder D:\xampp\htdocs
Untuk menjalankan di dalam browser, anda tinggal
mengganti alamat D:\xampp\htdocs menjadi localhost
Setiap file PHP harus ditulis menggunakan akhiran.php
Penamaan file PHP, harus tanpa spasi dan sebaiknya
menggunakan huruf kecil. Dapat
menggunakan underscore

STIKOM Bali 2017


PHP Syntax
<?php
// PHP code goes here
?>

PHP Comments
<!DOCTYPE html>
<html>
<body>
<?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
</body>
</html>

STIKOM Bali 2017


Example Script PHP

<html>
<head>
<title>Example</title>
</head>
<body>

<?php
echo ("Anda berada di stikom bali");
?>

</body>
</html>

STIKOM Bali 2017


Script PHP

all keywords (e.g. if, else, while, echo, etc.), classes,


functions, and user-defined functions are NOT case-
sensitive
all variable names are case-sensitive

STIKOM Bali 2017


Example script PHP: variable
& keywords

<!DOCTYPE html> <!DOCTYPE html>


<html> <html>
<body> <body>

<?php <?php
ECHO "Hello World!<br>"; $color = "red";
echo "Hello World!<br>"; echo "My car is " . $color . "<br>";
EcHo "Hello World!<br>"; echo "My house is " . $COLOR . "<br>";
?> echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html> </body>
</html>

STIKOM Bali 2017


Variables : Rules for PHP

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, and _ )
Variable names are
<?php
case-sensitive ($age and $AGE are
two different variables)
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>

STIKOM Bali 2017


PHP is a loosely Typed Language: PHP
automatically converts the variable to
the correct data type, depending on its
value
<?php
$x
= PHP
5; // global scope
Variables Scope: Local, Global
<?php &
Static function myTest() {
function myTest() {
static $x = 0;
// using x inside this function will
echo $x;
generate an error
$x++;
echo "<p>Variable x inside function is:
}
$x</p>";
}
myTest();
myTest();
myTest();
?>
?>

STIKOM Bali 2017


$GLOBALS[index]

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.
<?php
$x = 5;
$y = 10;

function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] +
$GLOBALS['y'];
}

myTest();
echo $y; // outputs 15
?>

STIKOM Bali 2017


echo and print Statements

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.

STIKOM Bali 2017


Data Types

String
Integer
Float (floating point numbers - also called double)
Boolean
Array
Object
NULL
Resource

STIKOM Bali 2017


Strings

Get The Length of a String


strlen() : function returns the length of a string.

<?php
echo strlen("Hello world!");?>

Count The Number of Words in a String


str_word_count()
<?php :
echo str_word_count("Hello world!");
?>

STIKOM Bali 2017


Strings (2)

Reverse a String
strrev() : function reverses a string

<?php
echo strrev("Hello world!");
?>

Replace Text Within a String


str_replace() : replaces some characters with some other
characters<?php
in a string
echo str_replace("world", Wolly", "Hello
world!");
?>

STIKOM Bali 2017


Constants

Syntax : define(name, value, case-insensitive)


name: Specifies the name of the constant
value: Specifies the value of the constant
case-insensitive: Specifies whether the constant name
should be case-insensitive. Default is false
<?php
<?php
define("GREETING", "Welcome to
define("GREETING", "Welcome to
W3Schools.com!");
W3Schools.com!", true);
echo GREETING;
echo greeting;
?>
?>

STIKOM Bali 2017


Constants (2)

Constants are Global


Constants are automatically global and can be used across
the entire script.
<?php
define("GREETING", "Welcome to
W3Schools.com!");

function myTest() {
echo GREETING;
}

myTest();
?>

STIKOM Bali 2017


Operators

Operators are used to perform operations on variables and


values. PHP divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators

STIKOM Bali 2017


PHP Arithmetic Operators

STIKOM Bali 2017


PHP Assignment Operators

STIKOM Bali 2017


PHP Comparison Operators

STIKOM Bali 2017


PHP Increment / Decrement
Operators

STIKOM Bali 2017


PHP Logical Operators

STIKOM Bali 2017


PHP String Operators

STIKOM Bali 2017


PHP Array Operators

STIKOM Bali 2017


Latihan

Buatlah sebuah program PHP yang berisi dengan


penjumlahan, pengurangan, perkalian dan pembagian

STIKOM Bali 2017


PHP Conditional Statements

Very often you write code, you want to perform


different actions for different conditions. You can use
conditional statements in your code to do this.
In PHP we have the following conditional statements:
if statement - executes some code if one condition is true
if...else statement - executes some code if a condition is
true and another code if that condition is false
if...else if....else statement - executes different codes
for more than two conditions
switch statement - selects one of many blocks of code to
be executed

STIKOM Bali 2017


PHP - The if Statement

The if statement executes some code if one condition is


true.
Syntax:
if (condition) {
code to be executed if condition is true;
}

STIKOM Bali 2017


PHP - The if...else Statement

The if....else statement executes some code if a


condition is true and another code if that condition is
false.
Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}

STIKOM Bali 2017


PHP - The if...else if....else
Statement

The if....else if...else statement executes different


codes for more than two conditions.
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}

STIKOM Bali 2017


Latihan

Buatlah program menggunakan ifelse ifelse


statement yang berisi kolom input untuk memasukkan
nilai:
Jika nilai yang diinput kurang dari 50 maka dapat
menampilkan tulisan Nilai anda masih kurang dari
standar
Jika Nilai yang diinput lebih besar dari 50 hingga 60
menampilkan nilai anda masih kurang
Nilai diatas 60 sampai 80 menampilkan tulisan nilai
anda bagus
Nilai diatas 80 hingga 100 menampilkan tulisan nilai
anda memuaskan
Jika nilai yang diinput lebih dari 100 menampilkan
tulisan input yang dimasukkan salah

STIKOM Bali 2017


The PHP switch Statement

Use the switch statement to select one of many blocks


of code to be executed.
Syntax switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}

STIKOM Bali 2017


The PHP switch
Statement (2)

This is how it works:


First we have a single expression n (most often a
variable), that is evaluated once.
The value of the expression is then compared with the
values for each case in the structure.
If there is a match, the block of code associated with
that case is executed.
Use break to prevent the code from running into the
next case automatically.
The default statement is used if no match is found.

STIKOM Bali 2017


The PHP switch Statement:
Example
<?php
$favcolor = "red";

switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>

STIKOM Bali 2017

Anda mungkin juga menyukai