SlideShare uma empresa Scribd logo
1 de 5
Baixar para ler offline
PHP Tutorials By Vineet Kumar Saini

                                  Functions in PHP

Introduction

PHP is composed of a set of one or more functions. Functions divide large programs into
smaller, easier to handle pieces. Each function normally performs one specific task.
Functions are used to reduce the code in the programming i.e. reusability. Functions can be
placed anywhere in a page. PHP has more than 700 built-in functions.

Types of Functions

In this article I will describe only three types of functions, which are as follows:

      Simple Functions
      Functions with Parameter
      Functions with Return values

Simple Functions

A Simple function does not take any arguments (parameters) or values. The function name
should start with a letter or underscore (_), but not a number.

Syntax

  function functionName()
  {
    message ;
  }

Example

<?php
function CompnyName()
{
echo "MCN Solutions Pvt. Ltd. Noida.";
}
echo "My company name is ";
CompnyName();
?>

Output

In the above example we build a CompnyName() function and print a message inside this
function. This function is called later for printing the message.
PHP Tutorials By Vineet Kumar Saini




Functions with Parameter

We can add arguments (parameters) to a function. Using parameters, we can add more
functionally to a function. An argument is just like a variable of PHP. An argument
(parameter) is specified inside the parenthesis of a function.

Syntax

 function functionName (argument1,argument2)
 {
   message ;
 }

Example1

<?php
function CompnyName ($fullname) // one argument i.e. $fullname
{
echo $fullname;
}
echo "My company name is ";
CompnyName ("MCN Solutions Pvt. Ltd. Noida.");
?>

Output

In the above example we build a CompnyName() function and pass an argument as a
$fullname. This argument shows the message when you write a message in this function.
PHP Tutorials By Vineet Kumar Saini




Example2

<?php
function sum ($X, $Y) // two argument i.e. $X and $Y
{
$Z=$X + $Y;
echo "X=$X, Y=$Y, <br> Sum=$Z ";
}
sum(3,4);
?>

Output

In the above example we build a sum() function and pass two arguments as $X and $Y.
These arguments i.e. $X and $Y takes the values, when you pass the values in this function.
PHP Tutorials By Vineet Kumar Saini

Functions with Return values

In these types of functions, a function returns some values through the argument
(parameter). Remember that a function should return at least one value.

Example1

<?php
function sum ($X, $Y)
{
$Z=$X + $Y;
return $Z;
}
echo "The Sum of 3 and 4 = ".sum(3,4);
?>

Output

In the above example we build a sum() function and pass two arguments as $X and $Y.
Now we declare a variable $Z, which takes a sum of $X and $Y then we will return $Z.
Which shows the sum of X and y. In the above function we passed 3 and 4, which take by
$X and $Y and returns the sum of these value to the variable $Z.




Example2

<?php
function sum ($X, $Y)
{
$Z=$X + $Y;
return $Z;
}
$X=5;
$Y=7;
$Sum=sum ($X,$Y); // assign the value of Z to $Sum
PHP Tutorials By Vineet Kumar Saini

echo "The Sum of 5 and 7 = ".$Sum;
?>

Output

In the above example we build a sum() function and pass two arguments as $X and $Y.
Now we declare a variable $Z, which takes to sum of $X and $Y then we will return $Z.
Which shows the sum of X and y. In this example we assigned the value to $X and $Y i.e. 5
and 7 respectively. The sum of these values are returned by $Z to the $sum.




Conclusion

So in this article you saw types of functions and the use of functions in PHP. Using this
article one can easily understand the concept of functions in PHP.

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Php Chapter 2 3 Training
Php Chapter 2 3 TrainingPhp Chapter 2 3 Training
Php Chapter 2 3 Training
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
 
Anonymous Functions in PHP 5.3 - Matthew Weier O’Phinney
Anonymous Functions in PHP 5.3 - Matthew Weier O’PhinneyAnonymous Functions in PHP 5.3 - Matthew Weier O’Phinney
Anonymous Functions in PHP 5.3 - Matthew Weier O’Phinney
 
Zend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample QuestionsZend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample Questions
 
Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database Programming
 
Php string function
Php string function Php string function
Php string function
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016
 
Introduction in php
Introduction in phpIntroduction in php
Introduction in php
 
Functions in php
Functions in phpFunctions in php
Functions in php
 
PHP function
PHP functionPHP function
PHP function
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHP
 
Php variables (english)
Php variables (english)Php variables (english)
Php variables (english)
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 

Semelhante a Functions in PHP

Web app development_php_06
Web app development_php_06Web app development_php_06
Web app development_php_06
Hassen Poreya
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
mussawir20
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Muhamad Al Imran
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Muhamad Al Imran
 

Semelhante a Functions in PHP (20)

PHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptxPHP FUNCTIONS AND ARRAY.pptx
PHP FUNCTIONS AND ARRAY.pptx
 
php user defined functions
php user defined functionsphp user defined functions
php user defined functions
 
Web app development_php_06
Web app development_php_06Web app development_php_06
Web app development_php_06
 
Print function in PHP
Print function in PHPPrint function in PHP
Print function in PHP
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
 
Functions in PHP.pptx
Functions in PHP.pptxFunctions in PHP.pptx
Functions in PHP.pptx
 
Arrays &amp; functions in php
Arrays &amp; functions in phpArrays &amp; functions in php
Arrays &amp; functions in php
 
Web Design EJ3
Web Design EJ3Web Design EJ3
Web Design EJ3
 
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Expressions and Operators.pptx
Expressions and Operators.pptxExpressions and Operators.pptx
Expressions and Operators.pptx
 
Functions
FunctionsFunctions
Functions
 
Advanced php
Advanced phpAdvanced php
Advanced php
 
OpenGurukul : Language : PHP
OpenGurukul : Language : PHPOpenGurukul : Language : PHP
OpenGurukul : Language : PHP
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHP
 
ex 2.pdf
ex 2.pdfex 2.pdf
ex 2.pdf
 

Mais de Vineet Kumar Saini

Mais de Vineet Kumar Saini (20)

Abstract Class and Interface in PHP
Abstract Class and Interface in PHPAbstract Class and Interface in PHP
Abstract Class and Interface in PHP
 
Add edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHPAdd edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHP
 
Introduction to Html
Introduction to HtmlIntroduction to Html
Introduction to Html
 
Computer Fundamentals
Computer FundamentalsComputer Fundamentals
Computer Fundamentals
 
Country State City Dropdown in PHP
Country State City Dropdown in PHPCountry State City Dropdown in PHP
Country State City Dropdown in PHP
 
Pagination in PHP
Pagination in PHPPagination in PHP
Pagination in PHP
 
Stripe in php
Stripe in phpStripe in php
Stripe in php
 
Install Drupal on Wamp Server
Install Drupal on Wamp ServerInstall Drupal on Wamp Server
Install Drupal on Wamp Server
 
Joomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDFJoomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDF
 
Dropdown List in PHP
Dropdown List in PHPDropdown List in PHP
Dropdown List in PHP
 
Update statement in PHP
Update statement in PHPUpdate statement in PHP
Update statement in PHP
 
Delete statement in PHP
Delete statement in PHPDelete statement in PHP
Delete statement in PHP
 
Implode & Explode in PHP
Implode & Explode in PHPImplode & Explode in PHP
Implode & Explode in PHP
 
Types of Error in PHP
Types of Error in PHPTypes of Error in PHP
Types of Error in PHP
 
GET and POST in PHP
GET and POST in PHPGET and POST in PHP
GET and POST in PHP
 
Database connectivity in PHP
Database connectivity in PHPDatabase connectivity in PHP
Database connectivity in PHP
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Browser information in PHP
Browser information in PHPBrowser information in PHP
Browser information in PHP
 
Variables in PHP
Variables in PHPVariables in PHP
Variables in PHP
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 

Functions in PHP

  • 1. PHP Tutorials By Vineet Kumar Saini Functions in PHP Introduction PHP is composed of a set of one or more functions. Functions divide large programs into smaller, easier to handle pieces. Each function normally performs one specific task. Functions are used to reduce the code in the programming i.e. reusability. Functions can be placed anywhere in a page. PHP has more than 700 built-in functions. Types of Functions In this article I will describe only three types of functions, which are as follows:  Simple Functions  Functions with Parameter  Functions with Return values Simple Functions A Simple function does not take any arguments (parameters) or values. The function name should start with a letter or underscore (_), but not a number. Syntax function functionName() { message ; } Example <?php function CompnyName() { echo "MCN Solutions Pvt. Ltd. Noida."; } echo "My company name is "; CompnyName(); ?> Output In the above example we build a CompnyName() function and print a message inside this function. This function is called later for printing the message.
  • 2. PHP Tutorials By Vineet Kumar Saini Functions with Parameter We can add arguments (parameters) to a function. Using parameters, we can add more functionally to a function. An argument is just like a variable of PHP. An argument (parameter) is specified inside the parenthesis of a function. Syntax function functionName (argument1,argument2) { message ; } Example1 <?php function CompnyName ($fullname) // one argument i.e. $fullname { echo $fullname; } echo "My company name is "; CompnyName ("MCN Solutions Pvt. Ltd. Noida."); ?> Output In the above example we build a CompnyName() function and pass an argument as a $fullname. This argument shows the message when you write a message in this function.
  • 3. PHP Tutorials By Vineet Kumar Saini Example2 <?php function sum ($X, $Y) // two argument i.e. $X and $Y { $Z=$X + $Y; echo "X=$X, Y=$Y, <br> Sum=$Z "; } sum(3,4); ?> Output In the above example we build a sum() function and pass two arguments as $X and $Y. These arguments i.e. $X and $Y takes the values, when you pass the values in this function.
  • 4. PHP Tutorials By Vineet Kumar Saini Functions with Return values In these types of functions, a function returns some values through the argument (parameter). Remember that a function should return at least one value. Example1 <?php function sum ($X, $Y) { $Z=$X + $Y; return $Z; } echo "The Sum of 3 and 4 = ".sum(3,4); ?> Output In the above example we build a sum() function and pass two arguments as $X and $Y. Now we declare a variable $Z, which takes a sum of $X and $Y then we will return $Z. Which shows the sum of X and y. In the above function we passed 3 and 4, which take by $X and $Y and returns the sum of these value to the variable $Z. Example2 <?php function sum ($X, $Y) { $Z=$X + $Y; return $Z; } $X=5; $Y=7; $Sum=sum ($X,$Y); // assign the value of Z to $Sum
  • 5. PHP Tutorials By Vineet Kumar Saini echo "The Sum of 5 and 7 = ".$Sum; ?> Output In the above example we build a sum() function and pass two arguments as $X and $Y. Now we declare a variable $Z, which takes to sum of $X and $Y then we will return $Z. Which shows the sum of X and y. In this example we assigned the value to $X and $Y i.e. 5 and 7 respectively. The sum of these values are returned by $Z to the $sum. Conclusion So in this article you saw types of functions and the use of functions in PHP. Using this article one can easily understand the concept of functions in PHP.