PHP Tutorials By Vineet Kumar Saini                                  Functions in PHPIntroductionPHP is composed of a set ...
PHP Tutorials By Vineet Kumar SainiFunctions with ParameterWe can add arguments (parameters) to a function. Using paramete...
PHP Tutorials By Vineet Kumar SainiExample2<?phpfunction sum ($X, $Y) // two argument i.e. $X and $Y{$Z=$X + $Y;echo "X=$X...
PHP Tutorials By Vineet Kumar SainiFunctions with Return valuesIn these types of functions, a function returns some values...
PHP Tutorials By Vineet Kumar Sainiecho "The Sum of 5 and 7 = ".$Sum;?>OutputIn the above example we build a sum() functio...
Upcoming SlideShare
Loading in...5
×

Functions in PHP

1,642

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,642
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
193
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Functions in PHP

  1. 1. PHP Tutorials By Vineet Kumar Saini Functions in PHPIntroductionPHP is composed of a set of one or more functions. Functions divide large programs intosmaller, 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 beplaced anywhere in a page. PHP has more than 700 built-in functions.Types of FunctionsIn this article I will describe only three types of functions, which are as follows:  Simple Functions  Functions with Parameter  Functions with Return valuesSimple FunctionsA Simple function does not take any arguments (parameters) or values. The function nameshould start with a letter or underscore (_), but not a number.Syntax function functionName() { message ; }Example<?phpfunction CompnyName(){echo "MCN Solutions Pvt. Ltd. Noida.";}echo "My company name is ";CompnyName();?>OutputIn the above example we build a CompnyName() function and print a message inside thisfunction. This function is called later for printing the message.
  2. 2. PHP Tutorials By Vineet Kumar SainiFunctions with ParameterWe can add arguments (parameters) to a function. Using parameters, we can add morefunctionally 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<?phpfunction CompnyName ($fullname) // one argument i.e. $fullname{echo $fullname;}echo "My company name is ";CompnyName ("MCN Solutions Pvt. Ltd. Noida.");?>OutputIn 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. 3. PHP Tutorials By Vineet Kumar SainiExample2<?phpfunction sum ($X, $Y) // two argument i.e. $X and $Y{$Z=$X + $Y;echo "X=$X, Y=$Y, <br> Sum=$Z ";}sum(3,4);?>OutputIn 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. 4. PHP Tutorials By Vineet Kumar SainiFunctions with Return valuesIn these types of functions, a function returns some values through the argument(parameter). Remember that a function should return at least one value.Example1<?phpfunction sum ($X, $Y){$Z=$X + $Y;return $Z;}echo "The Sum of 3 and 4 = ".sum(3,4);?>OutputIn 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<?phpfunction sum ($X, $Y){$Z=$X + $Y;return $Z;}$X=5;$Y=7;$Sum=sum ($X,$Y); // assign the value of Z to $Sum
  5. 5. PHP Tutorials By Vineet Kumar Sainiecho "The Sum of 5 and 7 = ".$Sum;?>OutputIn 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. 5and 7 respectively. The sum of these values are returned by $Z to the $sum.ConclusionSo in this article you saw types of functions and the use of functions in PHP. Using thisarticle one can easily understand the concept of functions in PHP.

×