Dasar PHP - Memulai dengan PHP
PHP (Hypertext Preprocessor) adalah bahasa pemrograman server-side yang powerful untuk pengembangan web. Mari kita mulai dengan dasar-dasar PHP.
Apa itu PHP?
PHP adalah bahasa pemrograman open-source yang dirancang khusus untuk pengembangan web. PHP dapat di-embed ke dalam HTML dan berjalan di server.
Keunggulan PHP
- Easy to Learn: Syntax yang sederhana dan mudah dipahami
- Cross-platform: Berjalan di berbagai sistem operasi
- Large Community: Komunitas yang besar dan aktif
- Rich Ecosystem: Banyak framework dan library
- Database Support: Dukungan berbagai database
- Web-focused: Dirancang khusus untuk web development
Setup Development Environment
System Requirements
- PHP >= 8.0
- Web server (Apache/Nginx)
- Text editor atau IDE
- Database (optional)
Instalasi PHP
1
2
3
4
5
6
7
8
9
|
# Ubuntu/Debian
sudo apt update
sudo apt install php php-cli php-fpm
# macOS dengan Homebrew
brew install php
# Windows dengan XAMPP/WAMP
# Download dari https://www.apachefriends.org/
|
Syntax Dasar PHP
1
2
3
4
5
|
<?php
// PHP code here
?>
<?= "Hello World" ?> // Echo shorthand
|
2. Variables
1
2
3
4
5
6
7
8
9
|
<?php
$name = "John Doe";
$age = 25;
$isStudent = true;
$price = 19.99;
echo $name; // Output: John Doe
var_dump($age); // Output: int(25)
?>
|
3. Data Types
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?php
// String
$string = "Hello World";
// Integer
$integer = 42;
// Float
$float = 3.14;
// Boolean
$boolean = true;
// Array
$array = [1, 2, 3, 4, 5];
// Object
$object = new stdClass();
// Null
$null = null;
?>
|
4. Strings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
$name = "PHP";
// String concatenation
echo "Hello " . $name; // Hello PHP
// String interpolation
echo "Hello $name"; // Hello PHP
// String functions
echo strlen($name); // 3
echo strtoupper($name); // PHP
echo strtolower($name); // php
?>
|
5. Arrays
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?php
// Indexed array
$fruits = ["Apple", "Banana", "Orange"];
// Associative array
$person = [
"name" => "John",
"age" => 25,
"city" => "New York"
];
// Accessing array elements
echo $fruits[0]; // Apple
echo $person["name"]; // John
// Array functions
echo count($fruits); // 3
array_push($fruits, "Mango");
?>
|
Control Structures
1. Conditional Statements
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
$age = 18;
if ($age >= 18) {
echo "You are an adult";
} elseif ($age >= 13) {
echo "You are a teenager";
} else {
echo "You are a child";
}
// Ternary operator
$status = ($age >= 18) ? "adult" : "minor";
?>
|
2. Loops
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?php
// For loop
for ($i = 0; $i < 5; $i++) {
echo $i . " ";
}
// While loop
$i = 0;
while ($i < 5) {
echo $i . " ";
$i++;
}
// Foreach loop
$fruits = ["Apple", "Banana", "Orange"];
foreach ($fruits as $fruit) {
echo $fruit . " ";
}
// Foreach with keys
foreach ($fruits as $index => $fruit) {
echo "$index: $fruit ";
}
?>
|
Functions
1. Function Declaration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php
function greet($name) {
return "Hello, $name!";
}
echo greet("John"); // Hello, John!
// Function with default parameter
function greetWithTitle($name, $title = "Mr.") {
return "Hello, $title $name!";
}
echo greetWithTitle("John"); // Hello, Mr. John!
echo greetWithTitle("Jane", "Ms."); // Hello, Ms. Jane!
?>
|
2. Built-in Functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
// String functions
$text = "Hello World";
echo strlen($text); // 11
echo strpos($text, "World"); // 6
echo substr($text, 0, 5); // Hello
// Array functions
$numbers = [1, 2, 3, 4, 5];
echo count($numbers); // 5
echo max($numbers); // 5
echo min($numbers); // 1
echo array_sum($numbers); // 15
?>
|
1
2
3
4
5
|
<form method="POST" action="process.php">
<input type="text" name="username" placeholder="Username">
<input type="email" name="email" placeholder="Email">
<button type="submit">Submit</button>
</form>
|
1
2
3
4
5
6
7
8
9
10
|
<?php
// process.php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$email = $_POST["email"];
echo "Username: " . htmlspecialchars($username) . "<br>";
echo "Email: " . htmlspecialchars($email) . "<br>";
}
?>
|
Next Steps
Di modul selanjutnya, kita akan mempelajari:
- Object-Oriented Programming (OOP)
- File handling
- Database integration
- Error handling
- Security best practices
Mari lanjutkan ke modul berikutnya!