> For the complete documentation index, see [llms.txt](https://learn.devlabss.my.id/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learn.devlabss.my.id/php/bab-1-persiapan-awal/1.3-menulis-kode-pertama.md).

# 1.3 Menulis Kode Pertama

Setelah alat siap, sekarang saatnya membuat file PHP pertama.

Bagian ini penting karena Anda mulai melihat hubungan antara file, server lokal, dan hasil di browser.

### Tujuan belajar

* membuat file PHP pertama
* menjalankannya di localhost
* memahami output dasar dengan `echo`

### Langkah membuat file pertama

Ikuti langkah berikut:

{% stepper %}
{% step %}

### Buka folder proyek

Buka folder server lokal Anda. Jika memakai Laragon, biasanya proyek disimpan di folder `www`.
{% endstep %}

{% step %}

### Buat file baru

Buat file baru dengan nama `halo.php`.
{% endstep %}

{% step %}

### Tulis kode pertama

Isi file dengan kode berikut:

```php
<?php
echo "Halo, dunia PHP!";
?>
```

{% endstep %}

{% step %}

### Simpan dan jalankan

Simpan file, lalu buka browser dan akses file tersebut melalui localhost.
{% endstep %}
{% endstepper %}

Contoh alamat yang sering dipakai:

```
http://localhost/halo.php
```

### Penjelasan kode

Tag `<?php` menandai awal kode PHP.

Perintah `echo` menampilkan teks ke layar.

Tanda titik koma `;` menandai akhir satu perintah.

Baris `?>` menandai akhir blok PHP pada contoh ini.

### Kenapa file harus dibuka lewat localhost

Jika Anda hanya klik dua kali file PHP, browser tidak akan memproses PHP sebagai program server.

File perlu dibaca dulu oleh server lokal. Setelah itu, hasilnya baru dikirim ke browser.

### Hasil yang seharusnya tampil

Jika semua benar, browser akan menampilkan:

```
Halo, dunia PHP!
```

Hasil ini terlihat sederhana, tetapi sangat penting. Itu berarti alur PHP Anda sudah berjalan.

### Jika file tidak tampil

Periksa hal berikut:

* server lokal sudah menyala
* nama file benar, yaitu `halo.php`
* file disimpan di folder server yang tepat
* alamat yang dibuka sesuai nama file

### Kesalahan umum

* file disimpan sebagai `halo.html`
* lupa menyalakan Laragon
* salah menulis alamat file di browser
* file dibuat di folder yang bukan folder proyek

### Latihan singkat

Ubah isi `echo` menjadi kalimat berikut:

```
Saya siswa RPL dan sedang belajar PHP.
```

Lalu jalankan lagi file tersebut.

Setelah itu, coba ubah sekali lagi menjadi kalimat buatan Anda sendiri.

### Poin evaluasi bab 1.3.

Pastikan Anda sudah memahami poin berikut:

* Saya berhasil membuat file PHP pertama.
* Saya berhasil menjalankan file PHP lewat localhost.
* Saya paham fungsi `echo`.
* Saya tahu bahwa file PHP harus dibuka lewat server lokal.

Jika semua sudah jelas, lanjut ke [1.4 Sintaks & Komentar](/php/bab-1-persiapan-awal/1.4-sintaks-and-komentar.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.devlabss.my.id/php/bab-1-persiapan-awal/1.3-menulis-kode-pertama.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
