PHP Dasar
PHP Dasar
1994 : Pada awalnya PHP merupakan kependekan dari Personal Home Page
dibuat oleh Rasmus Lerdorf
1995: Rasmus Lerdorf merilis versi 2dengan nama PHP/FI (Form Interpreter)
wujudnya berupa sekumpulan skrip yang digunakan untuk mengolah data
formulir dari web
1997: Sebuah perusahaan bernama Zend merilis ulang PHP dengan nama PHP
3.0 dan singkatan nya berubah menjadi PHP : Hypertext Preprocessing
1999 : Zend merilis interpreter PHP baru dan rilis tersebut dikenal dengan PHP
4.0
2004 : Zend merilis PHP 5.0
Sekarang : Versi terbaru dan stabil saat ini adalah 7.2
Kelebihan PHP
Install XAMPP
Install Visual Code
Sintaks Dasar PHP
<?php
// Your script is here
?>
File PHP berekstensi .php
Setiap statement (pernyataan program) diakhiri ; (titik koma)
Komentar
<?hp
// satu baris komentar
# ini juga satu baris komentar
/*
banyak baris komentar
*/
$myVar = 5;
Valid variable names
• Diawali dengan huruf atau garis bawah ( $nama, $_alamat)
• Dapat mengandung huruf, angka atau garis bawah
• Karakter pertama nama variable tidak boleh menggunakan angka
penamaan yang salah : $1hati
• Tidak boleh menggunakan kata kunci (keyword) PHP (contoh:. “class”)
• Tidak boleh menggunakan spasi, tanda ( “.” , “,” , “[“, “]” )
ECHO DAN PRINT
echo dan print memiliki fungsi yang sama, mencetak output data ke layar display user
Perbedaannya:
Variable dapat menyimpan data dengan tipe yang berbeda seperti di bawah ini :
String (Teks, Ex : $nama = “ M Herizki Hamdani”;)
Integer (Angka bilangan bulat, Ex : $nomor = 12; )
Float (Angka dengan decimal, Ex : $nilai = 7.5;)
Boolean (True or False, Ex : $isActive = true;)
Array (Banyak nilai dalam satu variable, Ex : $cars
= array("Volvo","BMW","Toyota");
Object (Ex : $tanggal = new Date(); )
Null (Tidak punya nilai, Ex : $hasil = null; )
Resource ($a=fopen(‘tes.txt’); )
Keyword dalam PHP
Fungsi String
$kota = ‘Depok’;
$propinsi = ‘ Jawa Barat ‘;
$alamat = $kota.‘ , ‘.$propinsi ; // dicetak : Depok , Jawa Barat
Operator Aritmatika
x=y x=y The left operand gets set to the value of the
expression on the right
x += y x=x+y Addition
x -= y x=x-y Subtraction
x *= y x=x*y Multiplication
x /= y x=x/y Division
x %= y x=x%y Modulus
Operator Perbandingan
Operator Name Example Result
== Equal $x == $y Returns true if $x is equal to $y
=== Identical $x === $y Returns true if $x is equal to $y, and they are
of the same type
!= Not equal $x != $y Returns true if $x is not equal to $y
<> Not equal $x <> $y Returns true if $x is not equal to $y
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are
not of the same type
> Greater than $x > $y Returns true if $x is greater than $y
< Less than $x < $y Returns true if $x is less than $y
>= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y
<= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
Global Variable - $_POST
Berguna untuk mengumpulkan data yang telah di submit pada form html yang menggunakan method “post”
Contoh :
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
Global Variable - $_GET
Berguna untuk mengumpulkan data yang telah di submit pada form html yang menggunakan
method “get”
Contoh :
<html>
<body>
</body>
</html>
Di halaman test_get.php
<html>
<body>
<?php
echo “Nama " . $_GET[nama'] . " umur " . $_GET[‘umur'];
?>
</body>
</html>