Skip to main content

PHP Functions


Create a User Defined Function in PHP

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

Syntax

function functionName() {
    code to be executed;
}


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 ():

Example

<?php
function writeMsg() {
    echo "Hello world!";
}

writeMsg(); // call the function?>




PHP Function Arguments

Information can be passed to functions through arguments. An argument is just like a variable.
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
The following example has a function with one argument ($fname). When the familyName() function is called, we also pass along a name (e.g. Jani), and the name is used inside the function, which outputs several different first names, but an equal last name:

Example

<?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):

Example

<?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 example above, notice that we did not have to tell PHP which data type the variable is.
PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.
In PHP 7, type declarations were added. This gives us an option to specify the expected data type when declaring a function, and by adding the strict declaration, it will throw a "Fatal Error" if the data type mismatch.
In the following example we try to send both a number and a string to the function without using strict:

Example

<?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:



Example

<?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:

Example

<?php declare(strict_types=1); // strict requirementfunction setHeight(int $minheight = 50) {
    echo "The height is : $minheight <br>";
}

setHeight(350);
setHeight(); // will use the default value of 50setHeight(135);
setHeight(80);
?>


PHP Functions - Returning values

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



Example

<?php declare(strict_types=1); // strict requirementfunction sum(int $x, int $y) {
    $z = $x + $y;
    return $z;
}

echo "5 + 10 = " . sum(510) . "<br>";
echo "7 + 13 = " . sum(713) . "<br>";
echo "2 + 4 = " . sum(24);
?>




PHP Return Type Declarations

PHP 7 also supports Type Declarations for the return statement. Like with the type declaration for function arguments, by enabling the strict requirement, it will throw a "Fatal Error" on a type mismatch.
To declare a type for the function return, add a colon ( : ) and the type right before the opening curly ( { )bracket when declaring the function.
In the following example we specify the return type for the function:



Example

<?php declare(strict_types=1); // strict requirementfunction addNumbers(float $a, float $b) : float {
    return $a + $b;
}
echo addNumbers(1.25.2);
?>


Example

<?php declare(strict_types=1); // strict requirementfunction addNumbers(float $a, float $b) : int {
    return (int)($a + $b);
}
echo addNumbers(1.25.2);
?>





















Comments

Popular

PHP Constants

PHP Constants A constant is an identifier (name) for a simple value. The value cannot be changed during the script. A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note:  Unlike variables, constants are automatically global across the entire script. Create a PHP Constant To create a constant, use the  define()  function. Syntax define( name ,  value ,  case-insensitive ) Parameters: name : Specifies the name of the constant value : Specifies the value of the constant case-insensitive : Specifies whether the constant name should be case-insensitive. Default is false Example Create a constant with a  case-sensitive  name: <?php define( "GREETING" ,  "Welcome to W3Schools.com!" ); echo  GREETING; ?> Example Create a constant with a  case-insensitive  name: <?php define( "GREETING" ,  "Welcome to W3Schools.com!" , true); echo  greeting; ?> PHP Constant Arrays In PHP7, ...

Terms & Conditions B Super-Chat

Terms & Conditions By downloading or using the app, these terms will automatically apply to you – you should make sure therefore that you read them carefully before using the app. You’re not allowed to copy, or modify the app, any part of the app, or our trademarks in any way. You’re not allowed to attempt to extract the source code of the app, and you also shouldn’t try to translate the app into other languages, or make derivative versions. The app itself, and all the trade marks, copyright, database rights and other intellectual property rights related to it, still belong to Al Sheikh Aminul Islam. Al Sheikh Aminul Islam is committed to ensuring that the app is as useful and efficient as possible. For that reason, we reserve the right to make changes to the app or to charge for its services, at any time and for any reason. We will never charge you for the app or its services without making it very clear to you exactly what you’re paying for. The B Super Chat app stores and proces...

Earning App

App Details User App details: 1.Sign Up - name, number, password,refer (Phone number verification) 2.Login - Number and Password 4.Multiple Account not supported 5. Dashboard - Point, Number, Today Claim, Invalid Acitvity 6.Quiz cliam reward after 20 quiz. 7. Click on ad after a amount of time.(as you want) 8.Payment History (own) 9.Payment History (All user) 10.Your social links (Telegram, Facebook etc.) 11.Push Notification 12.Automatically Back 13.Automatically back by tracking  VPN Servise . Motet things will be added if you want. Admin App details 1.See all user 2.Edit Users data(Name, Number, point, inavild activity count etc.) 3.Block or Unblock 4.Payment Requests 5.Send Push Notifications 6.Change Admin password. Our app supported only this company ads - 1.Admob Order Now Our contact Number : +8801533471179 Imo/Telegram/What'sApp +8801533471179 Our email - fedatoapps@gmail.com USER APP PIC<{[<>]}>   ...