Apa dua metode untuk memasukkan file dalam php?

Ringkasan. dalam tutorial ini, Anda akan belajar cara memasukkan kode dari file menggunakan konstruksi PHP

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
3

Pengantar PHP termasuk konstruk

Konstruk

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
3 memungkinkan Anda memuat kode dari file lain ke dalam file. Inilah sintaks dari konstruksi

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
3

include 'path_to_file';

Code language: PHP (php)

Dalam sintaks ini, Anda menempatkan jalur ke file setelah kata kunci

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
3. Misalnya, untuk memuat kode dari file

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
_7 ke dalam file

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
8, Anda dapat menggunakan pernyataan

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
3 berikut

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)

Jika PHP tidak dapat menemukan file

Warning: include(functions.php): failed to open stream: No such file or directory in .. on line 4 Warning: include(): Failed opening 'functions.php' for inclusion (include_path='\xampp\php\PEAR') in .. on line 4

Code language: PHP (php)
_0 di direktori

Warning: include(functions.php): failed to open stream: No such file or directory in .. on line 4 Warning: include(): Failed opening 'functions.php' for inclusion (include_path='\xampp\php\PEAR') in .. on line 4

Code language: PHP (php)
1, itu akan mengeluarkan peringatan. Sebagai contoh

Warning: include(functions.php): failed to open stream: No such file or directory in .. on line 4 Warning: include(): Failed opening 'functions.php' for inclusion (include_path='\xampp\php\PEAR') in .. on line 4

Code language: PHP (php)
_

Saat memuat file

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
_7, PHP pertama-tama mencari file

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
7 di direktori yang ditentukan oleh

Warning: include(functions.php): failed to open stream: No such file or directory in .. on line 4 Warning: include(): Failed opening 'functions.php' for inclusion (include_path='\xampp\php\PEAR') in .. on line 4

Code language: PHP (php)
4. Dalam contoh ini, ini adalah

Warning: include(functions.php): failed to open stream: No such file or directory in .. on line 4 Warning: include(): Failed opening 'functions.php' for inclusion (include_path='\xampp\php\PEAR') in .. on line 4

Code language: PHP (php)
5. Jika PHP dapat menemukan file

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
_7 di sana, PHP akan memuat kode dari file tersebut

Jika tidak, PHP mencari file

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
_7 di direktori skrip panggilan dan direktori kerja saat ini. Jika PHP dapat menemukan file

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
_7 di sana, itu memuat kode. Jika tidak, itu mengeluarkan peringatan jika file tidak ada

Ketika PHP memuat file

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
_7, itu benar-benar mengeksekusi kode di dalam file

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
7. Misalnya, jika Anda menempatkan kode berikut di file

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
7

<?php // functions.php function get_copyright() { return 'Copyright &copy; ' . date('Y') . ' by phptutorial.net. All Rights Reserved!'; } echo get_copyright();

Code language: HTML, XML (xml)

dan sertakan

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
_7 dalam file

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
8, Anda akan melihat output berikut saat Anda menjalankan file

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
8

Copyright © 2021 by phptutorial.net. All Rights Reserved!

Code language: CSS (css)

Ini menunjukkan bahwa konstruksi

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
_3 memang membuat PHP mengeksekusi kode dalam file

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
7

PHP termasuk contoh

Dalam praktiknya, Anda akan sering menggunakan konstruksi

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
3 ke elemen halaman dari desain situs umum. Misalnya, semua halaman di situs Anda mungkin memiliki header dan footer yang sama

Untuk menghindari pengulangan elemen ini di beberapa halaman, Anda dapat menempatkan kode header dan footer di file terpisah seperti

<?php // functions.php function get_copyright() { return 'Copyright &copy; ' . date('Y') . ' by phptutorial.net. All Rights Reserved!'; } echo get_copyright();

Code language: HTML, XML (xml)
8 dan

<?php // functions.php function get_copyright() { return 'Copyright &copy; ' . date('Y') . ' by phptutorial.net. All Rights Reserved!'; } echo get_copyright();

Code language: HTML, XML (xml)
9 dan menyertakannya di halaman

Biasanya, Anda menempatkan file template seperti

<?php // functions.php function get_copyright() { return 'Copyright &copy; ' . date('Y') . ' by phptutorial.net. All Rights Reserved!'; } echo get_copyright();

Code language: HTML, XML (xml)
8 dan

<?php // functions.php function get_copyright() { return 'Copyright &copy; ' . date('Y') . ' by phptutorial.net. All Rights Reserved!'; } echo get_copyright();

Code language: HTML, XML (xml)
9 di direktori terpisah. Sesuai kesepakatan, nama direktori

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
_3 adalah

Copyright © 2021 by phptutorial.net. All Rights Reserved!

Code language: CSS (css)
3

. ├── index.php ├── functions.php ├── inc │ ├── footer.php │ └── header.php └── public ├── css │ └── style.css └── js └── app.js

Code language: CSS (css)

File

<?php // functions.php function get_copyright() { return 'Copyright &copy; ' . date('Y') . ' by phptutorial.net. All Rights Reserved!'; } echo get_copyright();

Code language: HTML, XML (xml)
_8 berisi kode header halaman. Itu memiliki tautan ke file

Copyright © 2021 by phptutorial.net. All Rights Reserved!

Code language: CSS (css)
_5 yang terletak di direktori

Copyright © 2021 by phptutorial.net. All Rights Reserved!

Code language: CSS (css)
6

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="public/css/style.css"> <title>PHP include Example</title> </head> <body>

Code language: HTML, XML (xml)

File

<?php // functions.php function get_copyright() { return 'Copyright &copy; ' . date('Y') . ' by phptutorial.net. All Rights Reserved!'; } echo get_copyright();

Code language: HTML, XML (xml)
_9 berisi kode yang terkait dengan footer halaman

<script src="js/app.js"></script> </body> </html>

Code language: HTML, XML (xml)

Dalam file

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
_8, Anda dapat menyertakan file

<?php // functions.php function get_copyright() { return 'Copyright &copy; ' . date('Y') . ' by phptutorial.net. All Rights Reserved!'; } echo get_copyright();

Code language: HTML, XML (xml)
8 dan

<?php // functions.php function get_copyright() { return 'Copyright &copy; ' . date('Y') . ' by phptutorial.net. All Rights Reserved!'; } echo get_copyright();

Code language: HTML, XML (xml)
9 seperti ini

<?php include 'inc/header.php'; ?> <h1>PHP include</h1> <p>This shows how the PHP include construct works.</p> <?php include 'inc/footer.php'; ?>

Code language: HTML, XML (xml)

Jika Anda menjalankan file

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
8 dan melihat kode sumber halaman, Anda juga akan melihat kode dari file

<?php // functions.php function get_copyright() { return 'Copyright &copy; ' . date('Y') . ' by phptutorial.net. All Rights Reserved!'; } echo get_copyright();

Code language: HTML, XML (xml)
8 dan

<?php // functions.php function get_copyright() { return 'Copyright &copy; ' . date('Y') . ' by phptutorial.net. All Rights Reserved!'; } echo get_copyright();

Code language: HTML, XML (xml)
9

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="public/css/style.css" /> <title>PHP include Example</title> </head> <body> <h1>PHP include</h1> <p>This shows how the PHP include construct works.</p> <script src="public/js/app.js"></script> </body> </html>

Code language: HTML, XML (xml)

PHP termasuk & cakupan variabel

Saat Anda menyertakan file, semua variabel yang ditentukan dalam file tersebut mewarisi cakupan variabel dari baris tempat penyertaan terjadi

1) Termasuk di luar contoh fungsi

Misalnya, berikut ini mendefinisikan variabel

. ├── index.php ├── functions.php ├── inc │ ├── footer.php │ └── header.php └── public ├── css │ └── style.css └── js └── app.js

Code language: CSS (css)
_4 dan

. ├── index.php ├── functions.php ├── inc │ ├── footer.php │ └── header.php └── public ├── css │ └── style.css └── js └── app.js

Code language: CSS (css)
5 dalam

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
7

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
0

Saat Anda menyertakan

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
7 dalam file

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
8, variabel

. ├── index.php ├── functions.php ├── inc │ ├── footer.php │ └── header.php └── public ├── css │ └── style.css └── js └── app.js

Code language: CSS (css)
4 dan

. ├── index.php ├── functions.php ├── inc │ ├── footer.php │ └── header.php └── public ├── css │ └── style.css └── js └── app.js

Code language: CSS (css)
5 menjadi variabel global dalam file

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
8. Dan Anda dapat menggunakannya sebagai berikut

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
_1

2) Termasuk dalam contoh fungsi

Namun, jika Anda menyertakan file dalam suatu fungsi, variabel dari file yang disertakan bersifat lokal untuk fungsi tersebut. Lihat contoh berikut

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
_2

Dalam contoh ini, kami menyertakan

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
_7 di dalam fungsi

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="public/css/style.css"> <title>PHP include Example</title> </head> <body>

Code language: HTML, XML (xml)
3. Oleh karena itu, variabel

. ├── index.php ├── functions.php ├── inc │ ├── footer.php │ └── header.php └── public ├── css │ └── style.css └── js └── app.js

Code language: CSS (css)
4 dan

. ├── index.php ├── functions.php ├── inc │ ├── footer.php │ └── header.php └── public ├── css │ └── style.css └── js └── app.js

Code language: CSS (css)
5 dari

<?php // index.php file include 'functions.php';

Code language: HTML, XML (xml)
7 bersifat lokal ke

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="public/css/style.css"> <title>PHP include Example</title> </head> <body>

Code language: HTML, XML (xml)
7

Penting untuk diperhatikan bahwa semua fungsi, kelas, antarmuka, dan sifat yang ditentukan dalam file yang disertakan akan memiliki cakupan global

Bagaimana cara memasukkan dua file dalam PHP?

Pernyataan include() digunakan untuk menyertakan file php di file lain. Dengan cara ini Anda dapat menulis sepotong kode dalam file php dan dapat menggunakannya untuk banyak file melalui pernyataan include().

Apa perbedaan mode file dalam PHP?

Mode file adalah elemen yang digunakan untuk melakukan berbagai tugas. Suka – Buat file, Buka, Baca, Hapus, Tambahkan, tutup. Berikut deskripsi semua mode

Apa cara yang benar untuk memasukkan file teks PHP dalam kode PHP?

PHP include digunakan untuk menyertakan file berdasarkan jalur yang diberikan. .
termasuk 'nama file';
sertakan ('nama file');

Bisakah kita memasukkan dua kali file dalam PHP?

Jika file dimasukkan dua kali, PHP akan memunculkan kesalahan fatal karena fungsinya sudah dideklarasikan . Direkomendasikan untuk menggunakan include_once alih-alih memeriksa apakah file sudah disertakan dan secara kondisional kembali ke dalam file yang disertakan.