Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this document? Why not share!

No Downloads

Total views

2,393

On SlideShare

0

From Embeds

0

Number of Embeds

1

Shares

0

Downloads

262

Comments

0

Likes

1

No embeds

No notes for slide

- 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. 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. 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. 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. 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.

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment