EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP Call Function

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP Call Function

PHP Call Function

Introduction to PHP Call Function

In PHP, the functions are of two types i.e. built-in and user-defined functions. There are a lot of built-in functions that can be called directly from the program by the programmers or the developers. These built-in functions have a specific meaning for the task to be performed. A user-defined function is specifically developed by the program that contains the code to be performed with respect to the application. The developer writes a set of code that has to perform the task based on the requirement in the application.

Define and Call a Function in PHP

There are two types of functions in PHP i.e. Built-in and User-defined functions. These functions are used in the program to perform various tasks. In PHP, a huge number of functions are defined and can be used anywhere in the program. Though it has a lot of advantages of not writing the code again and again and the functions come with the package itself, it is easy for the developer to use the built-in functions. But all the built-in functions have a predefined meaning and perform a specific task itself. So the developer started developing user-defined functions where the developer can write the specific task code inside any function and can be used anywhere in the program. The user-defined functions are called in the program in a class or in a method of a program to perform the task based on applications requirement.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How to Create a User-Defined Function?

In PHP, a user-defined function has to be declared with the keyword “function”. If we declare the function name and the code to be executed has been written inside it then it can be called anywhere in the program.

Syntax:

function function_name()
{
//statements
}

In the above syntax, the function_name is the name of the function that is to be called in the program and function is the keyword that is used to declare the function. In PHP, a function is declared with the function keyword prefixed with the function name and the calling of a function in a program is done by just calling the name of the function wherever required.

Examples to Implement PHP Call Function

Let’s take examples to understand the concept of calling a function in PHP.

Example #1

In the below example, the function Write_Output is the name of the function and the function is called in the program with the same name below. This line will call the defined function and executes the statements written inside the function and will exit once the last statement of the code gets executed.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,694 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Code:

<?php
function Write_Output()
{
echo "This is the sample example of a function!";
}
Write_Output();
?>

Output:

PHP Call Function eg1

Example #2

1. In PHP, a function can be a parameterized one i.e. passing arguments to the function. An arguments are just like the variables we define in the program. We can simply pass the arguments after the name of the function inside the parenthesis and can be added as much as we want by adding a comma in between the arguments. These arguments will be mapped while calling the function. It will throw error if you are not passing the correct number of arguments when calling the function.

Code:

<?php
function Employee($ename) {
echo "$ename Patil \n";
}
Employee("Akash");
Employee("Prakash");
?>

Output:

PHP Call Function eg2

In the above example, Employee is the name of the function and ename is the argument passed to the function in the declaration. The Employee function is called below the function declaration and the values are passed to the function while calling. The output of the program will be the names of the employee passed in the function while calling it.

2. In the below example, the employee names are printed and also the employee ID is printed along with it. When an organization has chunks of data to be stored in the database, we can simply write this kind of code to handle the data with ease.

Code:

<?php
function Employee($ename, $id) {
echo "Employee name is $ename and Employee id is $id \n";
}
Employee("Lakshmi","780540");
Employee("Rohit","780541");
Employee("Jenny","780542");
?>

Output:

Output

In PHP, the variables are not strictly based on the datatypes depending upon its value or data. The datatypes are loosely coupled and do not follow any strict rules. So we can add, subtract and do multiple operations on variables that have different data types as well.

Example #3

As we have seen all the examples, we can clearly see that the user-defined functions are more beneficial to the developer as it helps a lot from an application perspective and also is used to get the desired output. The goal of using functions in a particular program is that it can create his own code and develop the application based on requirements.

Code:

<?php
function mulNumbers(int $x, int $y)
{
return $x * $y;
}
echo mulNumbers(5, 13);
?>

Output:

Output

As we have seen in the above example that the integer can be multiplied with a string and a string can be used along with float etc. So PHP is a loosely typed language. In the above example, we could clearly see that the variable x and y are declared as integer and while calling they just use one variable as integer and another one as a string and the desired output also gets fetched.

Conclusion

In this article, we discussed how to call functions in PHP i.e. user-defined function. Then we discussed different forms of user-defined functions and syntax with example. The functions can be written for a specific application and to perform a specific task i.e. calculation of payroll, adding new entries, etc. So the developer can easily modify the code for the flexibility and can call it anywhere in the program.

Recommended Articles

This is a guide to PHP Call Function. Here we discuss how to define and how to create user-defined PHP Call Function along with examples and code implementation. You can also go through our other suggested articles to learn more –

  1. Continue in PHP
  2. String in PHP
  3. Destructor in PHP
  4. PHP Superglobal Variables

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More