AppDividend
Latest Code Tutorials

PHP Functions Tutorial With Example | Functions In PHP

0

PHP Functions Tutorial With Example | Functions In PHP is today’s topic. A function is a block of statements that can be used repeatedly in a program. A function will not execute immediately when a page loads. A call to the function will execute a function. The real power of PHP comes from its functions; it has more than 1000 built-in functions.

PHP Functions

PHP functions are similar to other programming languages. The function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value.

You already have seen many functions like push() and explode() etc. They are built-in functions, but PHP gives you the option to create your functions as well.

There are two parts which should be clear to you:

  1. Creating a User Defined Function in PHP
  2. Calling a User Defined Function in PHP

You hardly need to create your PHP function because there are already more than 1000 of built-in library functions created for different area and you need to call them according to your requirement.

#Creating a User Defined Function in PHP

You can create a function its name should start with keyword function, and all the PHP code should be put inside { and } braces.

function functionName() {
    code to be executed;
}

A function name can start with a letter or underscore, not a number. Function names are NOT case-sensitive. See the following code.

<?php

function series() {
    return 'Stranger Things';
}

We have created a function called series() which returns the Stranger Things string.

We have created a function, but not called the function.

#Calling a User Defined Function in PHP

Okay, we can call the function using the following syntax.

functionName();

If we continue the above example, then we can write the following code to call the PHP function.

<?php

function series() {
    return 'Stranger Things';
}

echo series();

We are printing the returned string from the series() function. See the output.

➜  pro php app.php
Stranger Things                                                                 
➜  pro

#PHP Functions with Parameters

PHP gives you the option to pass your parameters inside a function.

You can pass as many as parameters your like. These parameters work like variables inside your function.

See the following code.

<?php

function series($mike, $eleven) {
   echo "Both {$mike} and {$eleven} are cute!";
}

series('Finn', 'Millie');

We have passed two arguments which are $mike and $eleven. At the time calling, we have provided both the strings or numbers or whatever type of arguments you want to pass.

See the output.

➜  pro php app.php
Both Finn and Millie are cute!                                                   
➜  pro

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 data type expected when declaring a function, and by enabling the strict requirement, it will throw a “Fatal Error” on a type mismatch.

See the following example.

<?php

function series($jane, $eleven) {
   echo $jane + $eleven;
}

series('Millie Bobby Brown', 11);

See the output.

➜  pro php app.php
PHP Warning:  A non-numeric value encountered in /Users/krunal/Desktop/code/php/pro/app.php on line 4

Warning: A non-numeric value encountered in /Users/krunal/Desktop/code/php/pro/app.php on line 4
11                                                                              
➜  pro

My current PHP version is the following.

➜  pro php -v
PHP 7.2.14 (cli) (built: Jan 12 2019 05:21:04) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.14, Copyright (c) 1999-2018, by Zend Technologies
➜  pro

#PHP Default Argument Value

We can pass the default argument if the actual argument is not passed to the function.

See the following code.

<?php

function setPrice(int $florencebymills = 10) {
  echo "The min price is : $florencebymills"."\n";
}

setPrice();

We have defined the default price as $florencebymills variable, which has value 10.

So, when you call the setPrice() function, you do not need to pass the argument necessarily. You will still get the default value, which is 10. See the output.

➜  pro php app.php
The min price is : 10
➜  pro

But, you can also override the default value by providing the actual value like the following.

<?php

function setPrice(int $florencebymills = 10) {
  echo "The min price is : $florencebymills"."\n";
}

setPrice(14);

We have passed the 14 as an actual parameter. So, it will override the formal parameter.

See the output.

➜  pro php app.php
The min price is : 14
➜  pro

#Dynamic Function Calls

It is possible to assign function names as strings to variables and then treat these variables precisely as you would the function name itself.

See the following code.

<?php

function lyrics() {
  echo "KGB was dogging me!";
}

$sim = "lyrics";
$sim();

In the above code, we have defined the lyrics() function.

Then we have defined one variable whose value is the same as function’s name, and then we treat that variable as a function and call the function and see the output.

➜  pro php app.php
KGB was dogging me!                                                             
➜  pro

Finally, PHP Functions Tutorial With Example | Functions In PHP is over.

#Recommended Posts

  1. PHP chunk_split() Function Example Tutorial
  2. PHP Array Sum Example | PHP array_sum() Function Tutorial
  3. PHP Array Count Example | PHP count() Function Tutorial
  4. How To Check Variable Is NULL in PHP | PHP is_null() Tutorial Example
  5. PHP array_walk Example | PHP array_walk() Function Tutorial

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.