Modul Pemrograman Web

Materi PHP

PHP (Hypertext Preprocessor) adalah bahasa pemrograman server-side yang digunakan untuk mengembangkan aplikasi web dinamis. Berikut adalah beberapa konsep dasar dalam PHP:

Contoh Konsep Dasar PHP

1. Variabel

$x = 5;
$y = "Hello";
$z = true;

2. Penggunaan Variabel dalam Teks

$name = "John";
echo "Hello, $name!";

3. Struktur Kontrol

if ($x > 0) {
  echo "Positif";
} else {
  echo "Negatif";
}

4. Perulangan

for ($i = 0; $i < 5; $i++) {
  echo $i;
}

5. Fungsi

function greet($name) {
  return "Hello, " . $name . "!";
}
$greeting = greet("World");

6. Array

$numbers = array(1, 2, 3, 4, 5);
$fruits = ["Apple", "Banana", "Orange"];

7. Pengolahan Form

<form action="submit.php" method="post">
  <label for="name">Nama:</label>
  <input type="text" id="name" name="name"><br>
  <input type="submit" value="Submit">
</form>

8. Koneksi Database

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

9. Penggunaan Session

session_start();
$_SESSION['user'] = "John";
echo "Welcome, " . $_SESSION['user'];