Detri Amelia Chandra
  • HOME
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
    • URL Redirect Tool
    • URL Download Encryptor
No Result
View All Result
Detriamelia.com
  • HOME
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
    • URL Redirect Tool
    • URL Download Encryptor
No Result
View All Result
Detriamelia.com
No Result
View All Result

10. PHP Function Definition and Example

15/01/2020
in PHP, Programming
PHP Function Definition and Example

PHP Function Definition and Example

In this discussion, we will discuss about PHP Function. We really need PHP Function in certain circumstances when coding. The genuine intensity of PHP originates from its capacities. PHP has in excess of 1000 inherent capacities, and moreover you can make your own custom capacities.
Other than the inherent PHP capacities, it is conceivable to make your own capacities.

  • A capacity is a square of explanations that can be utilized over and again in a program.
  • A capacity won’t execute consequently when a page loads.
  • A capacity will be executed by a call to the capacity.

Create a User Defined Function in PHP

A user-defined function declaration starts with the word function:

Note: A function name must start with a letter or an underscore. Function names are NOT case-sensitive.

Tip: Give the function a name that reflects what the function does!
In the example below, we create a function named “writeMsg()”. The opening curly brace ( { ) indicates the beginning of the function code, and the closing curly brace ( } ) indicates the end of the function. The function outputs “Hello world!”. To call the function, just write its name followed by brackets ():

1
2
3
4
5
6
<?php
function writeMsg() {
    echo "Hello world!";
}
writeMsg(); // call the function
?>

PHP Function Arguments

Data can be gone to capacities through contentions. A contention is much the same as a variable.
Contentions are determined after the capacity name, inside the enclosures. You can include the same number of contentions as you need, simply separate them with a comma.
The accompanying model has a capacity with one contention ($fname). At the point when the familyName() work is called, we likewise go along a name (for example Jani), and the name is utilized inside the capacity, which yields a few distinctive first names, yet an equivalent last name. See the example below:

1
2
3
4
5
6
7
8
9
10
<?php
function familyName($fname) {
    echo "$fname Refsnes.<br>";
}
familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
?>

The following example has a function with two arguments ($fname and $year):

1
2
3
4
5
6
7
8
<?php
function familyName($fname, $year) {
    echo "$fname Refsnes. Born in $year <br>";
}
familyName("Hege", "1975");
familyName("Stale", "1978");
familyName("Kai Jim", "1983");
?>

PHP is a Loosely Typed Language

In the model above, notice that we didn’t need to disclose to PHP which information type the variable is.
PHP naturally relates an information type to the variable, contingent upon its worth. Since the information types are not set in an exacting sense, you can do things like adding a string to a whole number without causing a mistake.
In PHP 7, type assertions were included. This gives us an alternative to indicate the normal information type while pronouncing a capacity, and by including the severe announcement, it will toss a “Lethal Error” if the information type confuse.
In the accompanying model we attempt to send both a number and a string to the capacity without utilizing severe. See the example below:

1
2
3
4
5
6
7
<?php
function addNumbers(int $a, int $b) {
    return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is NOT enabled "5 days" is changed to int(5), and it will return 10
?>

To specify strict we need to set declare(strict_types=1);. This must be on the very first line of the PHP file.
In the following example we try to send both a number and a string to the function, but here we have added the strict declaration:

1
2
3
4
5
6
7
<?php declare(strict_types=1); // strict requirement
function addNumbers(int $a, int $b) {
    return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is enabled and "5 days" is not an integer, an error will be thrown
?>

PHP Default Argument Value

The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument:

1
2
3
4
5
6
7
8
9
<?php declare(strict_types=1); // strict requirement
function setHeight(int $minheight = 50) {
    echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>

PHP Functions – Returning values

To let a function return a value, use the return statement:

1
2
3
4
5
6
7
8
9
<?php declare(strict_types=1); // strict requirement
function sum(int $x, int $y) {
    $z = $x + $y;
    return $z;
}
echo "5 + 10 = " . sum(5, 10) . "<br>";
echo "7 + 13 = " . sum(7, 13) . "<br>";
echo "2 + 4 = " . sum(2, 4);
?>

PHP Return Type Declaration

PHP 7 additionally bolsters Type Declarations for the arrival proclamation. Like with the sort assertion for work contentions, by empowering the exacting prerequisite, it will toss a “Lethal Error” on a kind confound.
To proclaim a sort for the capacity return, include a colon ( : ) and the sort directly before the opening wavy ( { )section while announcing the capacity.
In the accompanying model we indicate the arrival type for the capacity:

1
2
3
4
5
6
<?php declare(strict_types=1); // strict requirement
function addNumbers(float $a, float $b) : float {
    return $a + $b;
}
echo addNumbers(1.2, 5.2);
?>

You can specify a different return type, than the argument types, but make sure the return is the correct type:

1
2
3
4
5
6
<?php declare(strict_types=1); // strict requirement
function addNumbers(float $a, float $b) : int {
    return (int)($a + $b);
}
echo addNumbers(1.2, 5.2);
?>

 

Tags: PHP
ShareTweetShare

Related Posts

Cara Mudah Memahami Bahasa Pemrograman Python
Python

Belajar Dasar-dasar Python untuk Pemula: Cara Mudah Memahami Bahasa Pemrograman Python

Python adalah bahasa pemrograman tingkat tinggi yang dapat digunakan untuk membuat berbagai macam aplikasi. Python diciptakan oleh Guido van Rossum...

06/03/2023
Relasi Tabel Database Menggunakan Operasi Join dan Model Klasik
Belajar PHP

Relasi Tabel Database Menggunakan Operasi Join dan Model Klasik

MySQL adalah database relational yang sangat populer di dunia programming hari ini. Dengan memiliki fitur yang bisa dikaitkan antar tabel,...

30/04/2020
PHP Forms - Validation With Required Fields
PHP

14. PHP Forms – Validation With Required Fields

Hi everyone, this time we will discuss about PHP Validation using the Required Field. Not many people know, it turns...

15/02/2020
PHP Form Handling
PHP

13. New Performing PHP Form Handling

Detriamelia.Com - Running form functions in PHP is not difficult. By using the $ _POST and $ _GET methods it...

13/02/2020
Next Post
PHP Array Example, Complete Editions

11. PHP Array Example, Complete Editions

Learn PHP Global Variables - Superglobals With Example

12. Learn PHP Global Variables - Superglobals With Example

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended

Understanding PHP Condition If Else, Elseif Statements

7. Understanding PHP Condition If Else, Elseif Statements

15/01/2020
Detailed Explanation of PHP Data Types

6. Detailed Explanation of PHP Data Types

15/01/2020
Easy to Identify Identity Card Number (NIK) Using PHP

Easy to Identify Identity Card Number (NIK) Using PHP

27/03/2019
install-apache-mysql-and-php-in-ubuntu-18-04-lts-part-2

3. How to Install Apache, MySQL and PHP in Ubuntu 18.04 LTS Part 2

11/11/2019

Instagram Feed

  • Sekolah sambil bermain...
  • Produk mahasiswa semester 5 Pendidikan Teknologi Informasi STKIP Rokania..
Kran dispenser otomatis (sensor)
#arduino
#PTI Ok
#STKIPRokania
#AyoKuliahDiRokania
  • Salah satu produk mahasiswa Pendidikan Teknologi Informasi STKIP Rokania...Pemanfaatan Internet Of Thing pada Lampu dan CCTV otomatis dengan Telegram Bot..

#StkipRokania
#PendidikanTeknologiInformasi
  • "Golden Time" yang tidak bisa diputar dan diulang tetapi tersimpan di inner child nya dan diingat selamanya..
  • Selamat atas M.Kom nya yah...semoga ada Reski untuk studi selanjutnya ya...
  • HOME
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
Detriamelia.Com

© 2022 Detri Amelia Chandra - IT Tips and Programming Tutorials.

No Result
View All Result
  • HOME
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
    • URL Redirect Tool
    • URL Download Encryptor