You are currently viewing PHP Tutorial: How to create functions in PHP?
How to create function in PHP?

PHP Tutorial: How to create functions in PHP?

Definition -: Function is a type of process that is used to execute a specific program or loc (lines of code). In other words, A set of instructions that has a specific name and work to be executed separately according to time.


Hello my all dear Visitors, again!! you’re most welcome in this domain (www.bittutech.com). Today, I want that, you learn something new from here about pre-defined and user-defined functions in PHP. In this PHP tutorial blog, you can learn about How to create functions in PHP? and How to use functions in PHP?


Dear Visitors, i want tell you something before starting the main topic of this blog. PHP and other programming languages use this mechanism(defining functions) to work easily and done with proper manner. This mechanism is the best for all coders and programmers to define a set of instructions that can work efficiently and separately from the other coding.


What is Function in PHP?

Functions are those callable sections of code that you can pass data to, and that can return data to you. Functions let you break your code up, following that old programming dictum: Divide and Conquer. Get some code that needs to be run multiple times in your application? Put that code into a function and after putting it, you can run that lines of code multiple times by calling that function.


Why uses functions instead of normal coding?

This is a very disgusting question to ask yourself because none of all programming languages can’t be run on normal coding or absence of functions. you need at least one function to run your program of which name is main() function, without the main() function, you will get an error message. In some programming languages, the main() function goes to defined explicitly and in some languages, implicitly. In Most of the scripting language, we don’t need to define a main() function to run our script or program, implicitly main() function does associates within our script or program such as javascript, php, e.t.c.


Types of functions

There are mainly two types of function that is used in most of the programming languages.

  • pre-defined function
  • user-defined function

Pre-defined function –

These types of functions are also known as built-in functions. These functions are pre-existed and can only be used, not altering. This is a significant point to be noted in your notes.

User-defined function –

These types of functions are also known as manual functions. These functions are user-defined and can be altered as well as run by the user. This is a significant point to be noted in your notes. Users can edit the instruction according to themselves.


How to create functions in PHP?

So now, we are going to create or define a function in our PHP script. First of all, you should know that How to define the function in PHP Script.

Syntax -: 
  function function_name(argument)
 {
 statement; 
 return return_value;
}

The above example shows the syntax of defining a function. After knowing about the syntax of the function, we should start the program or write a short program for more clarification.


Write a program to define a function

// How to create or define a function in our PHP Script.

<!DOCTYPE html>
<html>
<head>
<title>
Define a Function
</title>
<?php
function addition()
{
    $a=10;  $b=20;
    $c=$a+$b;
    echo "Read the variable's value and gives the output  ".$c;
}
addition();  #call the function from here
?>
</head>
<body>
<h1>This page reads a function's statement</h1><br>
</body>
</html>


Last words-: This is it, for now, we will meet very soon and thank you so much for reading my article. You can read my other articles. Join us on social networks to get the daily and latest updates.


Share post

Prajjwal Singh

Tech Blogger || Web developer || Computer Networking Enthusiast || Microsoft SQL Database Management Expert || Software Debugger || Learned DOS OS Structure

Leave a Reply