AppDividend
Latest Code Tutorials

PHP Functions: How to Use Functions In PHP

A function is a block of statements used repeatedly in a program. A function will not execute immediately when a page loads. Instead, 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 that takes one more input in a parameter, 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.

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 built-in library functions created for different areas, 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 series () function that 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, 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 parameters as you’re 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. We have provided both the strings or numbers or whatever type of arguments you want to pass at the time of the call.

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 given 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 is 10.

So, when you call the setPrice() function, you do not necessarily need to pass the argument. However, 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 them 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 the 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

That’s it for this tutorial.

Recommended Posts

PHP chunk_split()

PHP array sum()

PHP count()

How To Check Variable Is NULL in PHP

PHP array_walk()

Leave A Reply

Your email address will not be published.

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