PHP. Fungsi Show Meningkatkan Artikel Simpan Artikel Seperti Artikel
Meningkatkan Artikel Simpan Artikel Fungsi adalah blok kode yang ditulis dalam program untuk melakukan beberapa tugas tertentu. Kita dapat menghubungkan fungsi dalam program dengan karyawan di kantor secara nyata untuk pemahaman yang lebih baik tentang cara kerja fungsi. Misalkan bos ingin karyawannya menghitung anggaran tahunan. Jadi bagaimana proses ini akan selesai? . Fungsi bekerja dengan cara yang sama. Mereka mengambil informasi sebagai parameter, mengeksekusi blok pernyataan atau melakukan operasi pada parameter ini dan mengembalikan hasilnya.
Mengapa kita harus menggunakan fungsi?
Membuat Fungsi Saat membuat fungsi yang ditentukan pengguna, kami perlu mengingat beberapa hal.
Sintaksis. function function_name(){ executable code; } Contoh. PHP
This is Geeks for Geeks_0 This is Geeks for Geeks1 This is Geeks for Geeks2 This is Geeks for Geeks3 This is Geeks for Geeks4 This is Geeks for Geeks_5
This is Geeks for Geeks_6 This is Geeks for Geeks_7
This is Geeks for Geeks_8 Keluaran. This is Geeks for Geeks_
Parameter atau Argumen Fungsi Informasi atau variabel, di dalam tanda kurung fungsi, disebut parameter. Ini digunakan untuk menyimpan nilai yang dapat dieksekusi selama runtime. Pengguna bebas mengambil parameter sebanyak yang dia mau, dipisahkan dengan operator koma(,). Parameter ini digunakan untuk menerima input selama runtime. Saat meneruskan nilai seperti saat pemanggilan fungsi, mereka disebut argumen. Argumen adalah nilai yang diteruskan ke fungsi dan parameter digunakan untuk menampung argumen tersebut. Dalam istilah umum, baik parameter maupun argumen memiliki arti yang sama. Kita perlu mengingat bahwa untuk setiap parameter, kita harus memberikan argumen yang sesuai. function function_name($first_parameter, $second_parameter) { executable code; } Contoh. PHP
function function_name($first_parameter, $second_parameter) { executable code; }_0
function function_name($first_parameter, $second_parameter) { executable code; }2 function function_name($first_parameter, $second_parameter) { executable code; }3 function function_name($first_parameter, $second_parameter) { executable code; }4 function function_name($first_parameter, $second_parameter) { executable code; }5 function function_name($first_parameter, $second_parameter) { executable code; }4 function function_name($first_parameter, $second_parameter) { executable code; }7 function function_name($first_parameter, $second_parameter) { executable code; }8 This is Geeks for Geeks_0 This is Geeks for Geeks1 The product is 301 The product is 302 function function_name($first_parameter, $second_parameter) { executable code; }3 The product is 304 function function_name($first_parameter, $second_parameter) { executable code; }5 The product is 304 function function_name($first_parameter, $second_parameter) { executable code; }7 This is Geeks for Geeks4 This is Geeks for Geeks1 This is Geeks for Geeks2 Ram is 15 years old Adam is 12 years old1 This is Geeks for Geeks4 This is Geeks for Geeks_5
This is Geeks for Geeks_6 Ram is 15 years old Adam is 12 years old_5 Ram is 15 years old Adam is 12 years old_6
This is Geeks for Geeks_8 Keluaran. The product is 30
Menetapkan Nilai Default untuk parameter Fungsi PHP memungkinkan kita untuk menetapkan nilai argumen default untuk parameter fungsi. Jika kita tidak meneruskan argumen apa pun untuk parameter dengan nilai default, maka PHP akan menggunakan nilai set default untuk parameter ini dalam pemanggilan fungsi. PHP
Ram is 15 years old Adam is 12 years old_9
The product is 301 The product is 302 function function_name($first_parameter, $second_parameter) { executable code; }4 The product is 304 The product is 305 This is Geeks for Geeks_0 This is Geeks for Geeks1 This is Geeks for Geeks2 The product is 309 This is Geeks for Geeks4 This is Geeks for Geeks_5
This is Geeks for Geeks_6 The product is 301 The original value is still 10 The original value changes to 204 The original value is still 10 The original value changes to 205
The original value is still 10 The original value changes to 206 The original value is still 10 The original value changes to 207 The product is 301 The original value is still 10 The original value changes to 209 <?php 0
This is Geeks for Geeks_8 Keluaran. Ram is 15 years old Adam is 12 years old Pada contoh di atas, parameter $num memiliki nilai default 12, jika kita tidak memberikan nilai apa pun untuk parameter ini dalam pemanggilan fungsi, maka nilai default 12 ini akan dipertimbangkan. Juga parameter $str tidak memiliki nilai default , jadi wajib Mengembalikan Nilai dari Fungsi Fungsi juga dapat mengembalikan nilai ke bagian program dari mana ia dipanggil. Kata kunci kembali digunakan untuk mengembalikan nilai kembali ke bagian program, dari mana ia dipanggil. Nilai yang dikembalikan mungkin dari jenis apa pun termasuk array dan objek. Pernyataan return juga menandai akhir dari fungsi dan menghentikan eksekusi setelah itu dan mengembalikan nilainya. PHP
function function_name($first_parameter, $second_parameter) { executable code; }_0
function function_name($first_parameter, $second_parameter) { executable code; }2 function function_name($first_parameter, $second_parameter) { executable code; }3 function function_name($first_parameter, $second_parameter) { executable code; }4 function function_name($first_parameter, $second_parameter) { executable code; }5 function function_name($first_parameter, $second_parameter) { executable code; }4 function function_name($first_parameter, $second_parameter) { executable code; }7 function function_name($first_parameter, $second_parameter) { executable code; }8 This is Geeks for Geeks_0 This is Geeks for Geeks1 The product is 301 The product is 302 function function_name($first_parameter, $second_parameter) { executable code; }3 The product is 304 function function_name($first_parameter, $second_parameter) { executable code; }5 The product is 304 function function_name($first_parameter, $second_parameter) { executable code; }7 This is Geeks for Geeks4 This is Geeks for Geeks_1 This is Geeks for Geeks1 funcGeek() 4 The product is 301 This is Geeks for Geeks4 funcGeek() 7This is Geeks for Geeks_5
This is Geeks for Geeks_00 This is Geeks for Geeks01 This is Geeks for Geeks2 This is Geeks for Geeks03 This is Geeks for Geeks4
This is Geeks for Geeks_8 Keluaran. The product is 30
Parameter meneruskan ke Fungsi PHP memungkinkan kita dua cara di mana argumen dapat diteruskan ke suatu fungsi.
Contoh. PHP
This is Geeks for Geeks_07
This is Geeks for Geeks09 The product is 304 This is Geeks for Geeks11 This is Geeks for Geeks1 The product is 304 This is Geeks for Geeks14 This is Geeks for Geeks1 funcGeek() 4 The product is 304 This is Geeks for Geeks4 This is Geeks for Geeks_5
This is Geeks for Geeks_20
This is Geeks for Geeks22 The product is 304 This is Geeks for Geeks11 This is Geeks for Geeks1 The product is 304 This is Geeks for Geeks27 This is Geeks for Geeks1 funcGeek() 4 The product is 304 This is Geeks for Geeks4 This is Geeks for Geeks_5
This is Geeks for Geeks_33 This is Geeks for Geeks34
This is Geeks for Geeks_09 This is Geeks for Geeks33 <?php 0This is Geeks for Geeks2 This is Geeks for Geeks39 This is Geeks for Geeks4
This is Geeks for Geeks_41 This is Geeks for Geeks33 <?php 0This is Geeks for Geeks2 This is Geeks for Geeks45 This is Geeks for Geeks4
This is Geeks for Geeks_8 Keluaran. The original value is still 10 The original value changes to 20 Artikel ini disumbangkan oleh Chinmoy Lenka. Jika Anda menyukai GeeksforGeeks dan ingin berkontribusi, Anda juga dapat menulis artikel menggunakan tulis. geeksforgeeks. org atau kirimkan artikel Anda ke review-team@geeksforgeeks. org. Lihat artikel Anda muncul di halaman utama GeeksforGeeks dan bantu Geeks lainnya Catatan Pribadi Saya arrow_drop_up Menyimpan Silakan Login untuk berkomentar.Bagaimana cara mendefinisikan fungsi dalam PHP?Untuk mendeklarasikan tipe pengembalian fungsi, tambahkan tanda titik dua (. ) dan tipe tepat sebelum kurung kurawal ( { ) pembuka saat mendeklarasikan fungsi .
Bagaimana cara mendefinisikan dan memanggil fungsi di PHP?Ada dua metode untuk melakukan ini. Salah satunya secara langsung memanggil fungsi dengan nama variabel menggunakan braket dan parameter dan yang lainnya adalah dengan menggunakan fungsi call_user_func() tetapi dalam kedua metode nama variabel harus digunakan.
Berapa banyak cara fungsi didefinisikan dalam PHP?Berapa Banyak Fungsi di PHP? . over 700 functions you can use for various tasks. |