Skip to main content

PHP Functions

 PHP Functions

A function is a section of code that, when run, accomplishes a specified purpose. The primary rationale for utilizing functions is to reuse code; write once and run any time. When you need to write the same piece of code more than once in your application, we may utilize functions. You just need to create a function once, and then call it anytime you need it. In PHP, there are two sorts of functions.

  • The function defined by the user
  • built-in function

In this php tutorial, we will examine the first kind, which is user-created functions, and then we will go through some of the most essential inbuilt functions in PHP.


User-Specific Function

The function generated by the user is referred to as a user-defined function. The term "user-defined" refers to the fact that the function definition is written by the user.

Advantages

  • It enables top-down modular programming.
  • The program's length has been shortened.
  • Once written, it can be used at any time.
  • Working with user-defined functions necessitates the usage of two key components.

What is a User Defined Function?

Syntax

function function_name([parameters])
{
Statements to be executed;
[return value;]
}

Parameters are the optional inputs to your function; you can supply as many or as few as you wish. The return value indicates the output of your function when it is invoked; it is also optional.

The naming convention for functions is similar to that of variables in that it should begin with an underscore (_) or a letter, it should not contain any special characters, and it should not contain any keywords.

Function Call

Given is the syntax for calling a function.

function_name([Parameters if Any]);

Whenever you wish to run a specified function, you must call it with the appropriate arguments, and if the function returns a value, you must save the result. Let's have a look at a basic example.

For Ex.

<?php
function Display()//function Definition
{
echo "Hello DiscussDesk<br>";
}
Display();//function call
Display();
?>

As you can see from the example, we first built a function named Display(), which accepts no parameters and has no return value. Later in our software, we invoked the Display() method twice, which executed the code twice.

Output

Hello DiscussDesk
Hello DiscussDesk

Function returning value

To return a value, add a return statement at the conclusion of the function body. The following is an example of this.

For Ex.

<?php
function sum($a,$b)
{
$c=$a+$b;
return $c;
}
echo "10 + 20 =".sum(10,20)."<br>";
echo "10.30 + 20.30 =".sum(10.30,20.30)."<br>";
?>

Output

10 + 20 = 30
10.30 + 20.30 = 30.6

So, to summarize, in this session, we learned what php function is. Also covered is how to use a user-defined function in PHP.


Comments

Popular posts from this blog

Python Tutorial for Beginners

Learn Python with Industrial Experts with Examples Python is a popular general-purpose high-level, interactive and object-oriented scripting language that was invented by Guido Rossum in 1990. Python programming is commonly used in Computing Science in Artificial Intelligence, Human language Generation, Neural Networks and other specialized fields. Python for beginners , it has a basic syntax that is easy to use. That makes Python an outstanding language for novice to learn technology. Why Learn Python It is the most common and rapidly growing programming language of the present time. Here are 10 explanations for learning Python:   1.  It is simple and easy to understand  2.  It is very popular and on-demand  3   It has several libraries and frameworks  4.  It is often used in Data science  5.  It is often used in Artificial Intelligence and Machine Learning  6....

Best Way To Learn SEO

Learn SEO Before learning the subject, let’s understand something about it. Search engine optimization (SEO) is basically a process of improving the positions in the organic (non-paid) search results in the search engines. Please note that the higher the website is, the more people will be able to see it. The history of the SEO go back to the 90s when there were search engines emerged as for the first time. Nowadays, it is an essential marketing strategy and also an ever-growing industry.  Even the basic can change can usually make a very big difference in the terms that how search engines will see your website. In order to learn SEO , one should learn the basics of it from an SEO guide for beginners that is available on the web generally with all the points that cover all the critical topics and the basics.   The good thing about learning is that you’ll find basically tons of the information on the internet for free but the user should be able to ...

Download Free PHP Projects

PHP is a language used in the server scripting process. It is not used in static websites but if you want an interactive web page then PHP is the best way to write the code. The software is completely open-source and you can download it for free. You can also opt for some free PHP projects to understand the whole language properly. If you search online, you will find a lot of PHP projects available for download. You can get them and start practicing.   Administrative Panel with Login and Registration Facility   If you are searching for PHP free projects online then this project can be of great help to you. Here you have to make a full-fledged administrative panel with the feature of user login and user registration process. So, you will get to learn how to make a user management portal by using PHP software. You can get the project requirement and source code online.   Student Record Management   It is another unique project for your PHP practice. ...