Easy To Learning
  • Login
  • Sign Up
  • Q&A
  • Home
  • Php Tutorial
  • MySql Tutorial
  • Object-Oriented Programming
  • Java Tutorial
  • HTML Tutorial
  • React JS Tutorials
  • Free Projects
  • Blogs & Articles
  • Others
    • About Us
    • Contact Us
PHP Functions
Easy To Learning

Education Needs Complete Solution

Online Ask Question Question And Answer
  1. Home
  2. PHP Tutorials
  3. PHP Functions

PHP Tutorial

Basic PHP Fundamentals

  • PHP Tutorial Content
  • PHP Introduction
  • PHP History
  • Uses of PHP
  • PHP Features
  • What Is PHP
  • PHP Installation And Software Requirements
  • How To Install Wamp Server
  • How To Install Xampp Server
  • Php Syntax
  • PHP Variables
  • PHP Data Types
  • PHP Constants
  • PHP Echo And Print
  • PHP Operators
  • PHP Assignment Operators
  • PHP String And Logical Operators
  • Operators Categories
  • PHP Comments
  • PHP $ And $$ Variables
  • Super Global Variables

PHP Control Statement

  • PHP Control Structure
  • PHP If Statement
  • If Elseif Statement In PHP
  • Switch Statement In PHP
  • Break Statement In PHP
  • If Elseif Else Statement
  • Continue Statement In PHP

Php Loops

  • What Is Loop
  • For Loop In PHP
  • PHP Foreach Loop
  • PHP While Loop
  • Do While Loop

PHP Arrays

  • PHP Arrays
  • Indexed Arrays
  • Associative Array
  • Multidimensional Arrays
  • Array Functions
  • Array Constants
  • List of Array Functions
  • Array Functions First Exercise

Php String

  • String In PHP
  • PHP String Functions

PHP Functions

  • PHP Functions
  • PHP Parameterized Function
  • PHP Call By Value
  • Call By Reference In PHP
  • Default Argument Values Function In PHP
  • Variable Argument Function In PHP
  • Recursive Function In PHP
  • Math Function In PHP
  • Math Function Examples

PHP Form

  • Form Handling In PHP

PHP Include

  • Include File In PHP

PHP State Management

  • Cookies In PHP
  • Session In PHP

PHP File System

  • File Handling In PHP
  • Open Files In PHP
  • Read Files In PHP
  • Write Files In PHP
  • Delete Files In PHP
  • Append To Files In PHP

PHP Upload & Download

  • File Upload In PHP
  • Download File In PHP

PHP Mail System

  • Mail System In PHP
  • Mail With Attachment In PHP

Basic PHP Program

  • Php Programs
  • Sum Of Digits
  • PHP Even Odd Program
  • Prime Number Program
  • Table Of Number Program
  • Factorial Of A Number
  • Armstrong Number
  • Palindrome Number
  • Fibonacci Series

PHP Functions

Summary: PHP Functions

A PHP function is a piece of code that can be reused many times. It can take input as an argument list and return value.

php functions

In PHP, we can define

  • Conditional function
  • Function within Function
  • Recursive function

Advantage of PHP Functions

  • Code Reusability: PHP functions are defined only once and can be invoked many times, like in other programming languages.
  • Less Code: It saves a lot of code because you do not need to write the logic many times. By the use of the function, you can write the logic only once and reuse it.
  • Easy to understand: PHP functions separate the programming logic. So it is easier to understand the flow of the application because every logic is divided in the form of functions.
  • Reducing duplication of code
  • Decomposing complex problems into simpler pieces
  • Improving the clarity of the code
  • Information hiding

PHP User-defined Functions

We can declare and call user-defined functions easily. Let us see the syntax to declare user-defined functions.

Syntax

function functionname(){  
//code to be executed  
}  

Note: The function name must start with a letter and underscore only like other labels in PHP. It can not start with numbers or special symbols.

PHP Functions Example

File Name: function_file.php

<?php  
function myFunction(){  
echo "Hello PHP Function";  
}  
myFunction();//calling function  
?> 

 Output: Hello PHP Function

PHP Function Arguments

We can pass the information in the PHP function through arguments that are separated by a comma.

PHP supports

  • Call by Value (default),
  • Call by Reference,
  • Default argument values
  • Variable-length argument list.

Let us see the example to pass a single argument in the PHP function.

File Name: function_arg.php

<?php  
function myFunction($user_name){  
echo "Hello $user_name<br/>";  
}  
myFunction("Kamal");  
myFunction("Sonia");  
myFunction("Rani");  
?>  

Output:

Hello Kamal
Hello Sonia
Hello Rani

Let us see the example to pass two arguments in a PHP function.

File Name: function_args.php

<?php  
function getRecord($username,$age){  
echo "Hello $username, you are $age years old<br/>";  
}  
getRecord("Kamal",27);  
getRecord("Sonia",18);  
getRecord("Shivani",23);  
?>  

Output:

Hello Kamal, you are 27 years old
Hello Sonia, you are 18 years old
Hello Shivani, you are 23 years old

PHP Function: Default Argument Value

We can specify a default argument value in the function. While calling PHP function if you do not specify any argument, it will take the default argument. Let us see a simple example of using the default argument value in the PHP function.

Example of default Argument Value

PHP Function: Returning Value

Let us see an example of PHP function that returns a value.

File Name: function_defaultarg.php

<?php  
function cube($n){  
return $n*$n*$n;  
}  
echo "Cube of 3 is: ".cube(3);  
?>  

Output: Cube of 3 is: 27

Series: PHP Tutorials Hub
All Topics
Free Download Projects
Previous Chapter
Next Chapter

Ask a Question

Have a coding query or doubt? Ask here and get help from our community.

500 characters remaining
Submitting...
Free Download Projects
Join Telegram Group Join WhatsApp Group
Related Topics:
PHP Functions Parameterized Function PHP Call By Value PHP Call By Reference PHP Default Arguments PHP Variable Arguments PHP Recursive Function

Member Sign In

Forgot?
Signing in...

Don't have an account? Register here

Create Free Account

Register Captcha
Registering...

Already have an account? Sign In

Ask a Question

500 characters remaining
Submitting...

About Easy To Learning

Easy To Learning is a comprehensive programming learning portal offering free, practical, developer-driven tutorials for PHP, Java, Python, Spring, MySQL, CSS, Bootstrap, CakePHP, and more.

Explore community-contributed questions and answers, and download ready-to-run source code projects.

Quick Links

  • Home
  • Questions & Answers
  • Download Projects
  • Blog & Articles
  • Member Login

Community

  • Telegram Group
  • WhatsApp Community
  • Ask a Question
  • Join as Member

Connect With Us

Stay updated with our newest programming tutorials and free open source projects.

© 2026 Easy To Learning. All Rights Reserved.