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

10. PHP Function Definition and Example

Detri Amelia Chandra by Detri Amelia Chandra
15/01/2020
in PHP, Programming
0
PHP Function Definition and Example

PHP Function Definition and Example

0
SHARES
221
VIEWS
Share on FacebookShare on Twitter

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.

Recommended Post

Relasi Tabel Database Menggunakan Operasi Join dan Model Klasik

14. PHP Forms – Validation With Required Fields

14. PHP Forms – Validation With Required Fields

  • 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
Previous Post

9. PHP Loop (While, For, Foreach) Definition and Example

Next Post

11. PHP Array Example, Complete Editions

Related Posts

Relasi Tabel Database Menggunakan Operasi Join dan Model Klasik

Relasi Tabel Database Menggunakan Operasi Join dan Model Klasik

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

14. PHP Forms – Validation With Required Fields

15/02/2020
PHP Forms - Validation With Required Fields

14. PHP Forms – Validation With Required Fields

15/02/2020
PHP Form Handling

13. New Performing PHP Form Handling

13/02/2020
Learn PHP Global Variables - Superglobals With Example

12. Learn PHP Global Variables – Superglobals With Example

20/01/2020
PHP Array Example, Complete Editions

11. PHP Array Example, Complete Editions

17/01/2020
Next Post
PHP Array Example, Complete Editions

11. PHP Array Example, Complete Editions

Tinggalkan Balasan Batalkan balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

Top Stories

Relasi Tabel Database Menggunakan Operasi Join dan Model Klasik

Relasi Tabel Database Menggunakan Operasi Join dan Model Klasik

30/04/2020
PEMROGRAMAN WEB DAN PERANGKAT BERGERAK – PERPUSTAKAAN PART-1

Pemrograman Web dan Perangkat Bergerak – Perpustakaan Part-1

16/04/2020
PEMROGRAMAN WEB DAN PERANGKAT BERGERAK – PERPUSTAKAAN PART-1

Pemrograman Web dan Perangkat Bergerak – Perpustakaan Part-1

16/04/2020

News & More

Categories

  • Counseling
  • Consultations
  • Relationship
  • Friendship
  • Singlehood

About Us

We bring you the best Premium WordPress Themes that perfect for news, magazine, personal blog, etc. Check our landing page for details.

Connect on Social

© 2019 JNews - Premium WordPress news & magazine theme by Jegtheme.

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

© 2021 Detri Amelia Chandra - IT Tips and Programming Tutorial.