PHP10-PHP Functions

The real power of PHP comes from its functions. In PHP - there are more than 700 built-in functions available. PHP Functions In this...

The real power of PHP comes from its functions.
In PHP - there are more than 700 built-in functions available.

PHP Functions

In this tutorial we will show you how to create your own functions.
For a reference and examples of the built-in functions, please visit our PHP Reference.

Create a PHP Function

A function is a block of code that can be executed whenever we need it.

Creating PHP functions:
  • All functions start with the word "function()"
  • Name the function - It should be possible to understand what the function does by its name. The name can start with a letter or underscore (not a number)
  • Add a "{"  - The function code starts after the opening curly brace
  • Insert the function code
  • Add a "}"  - The function is finished by a closing curly brace

Example

A simple function that writes my name when it is called:

<html>
<body>
<?php
function writeMyName()
  {
  echo "Kai Jim Refsnes";
  }
writeMyName();
?>
</body>
</html>


Use a PHP Function

Now we will use the function in a PHP script:

<html>
<body>
<?php
function writeMyName()
  {
  echo "Kai Jim Refsnes";
  }
echo "Hello world!<br />";
echo "My name is ";
writeMyName();
echo ".<br />That's right, ";
writeMyName();
echo " is my name.";
?>
</body>
</html>

The output of the code above will be:

Hello world!
My name is Kai Jim Refsnes.
That's right, Kai Jim Refsnes is my name.


PHP Functions - Adding parameters

Our first function (writeMyName()) is a very simple function. It only writes a static string.
To add more functionality to a function, we can add parameters. A parameter is just like a variable.
You may have noticed the parentheses after the function name, like: writeMyName(). The parameters are specified inside the parentheses.

Example 1

The following example will write different first names, but the same last name:

<html>
<body>
<?php
function writeMyName($fname)
  {
  echo $fname . " Refsnes.<br />";
  }
echo "My name is ";
writeMyName("Kai Jim");
echo "My name is ";
writeMyName("Hege");
echo "My name is ";
writeMyName("Stale");
?>
</body>
</html>

The output of the code above will be:

My name is Kai Jim Refsnes.
My name is Hege Refsnes.
My name is Stale Refsnes.

Example 2

The following function has two parameters:

<html>
<body>
<?php
function writeMyName($fname,$punctuation)
  {
  echo $fname . " Refsnes" . $punctuation . "<br />";
  }
echo "My name is ";
writeMyName("Kai Jim",".");
echo "My name is ";
writeMyName("Hege","!");
echo "My name is ";
writeMyName("Ståle","...");
?>
</body>
</html>

The output of the code above will be:

My name is Kai Jim Refsnes.
My name is Hege Refsnes!
My name is Ståle Refsnes...
 

PHP Functions - Return values

Functions can also be used to return values.

Example

<html>
<body>
<?php
function add($x,$y)
  {
  $total = $x + $y;
  return $total;
  }
echo "1 + 16 = " . add(1,16);
?>
</body>
</html>

The output of the code above will be:

1 + 16 = 17
Name

ADO,131,ASP,3,C++,61,CORE JAVA,1,CSS,115,HTML,297,index,5,JAVASCRIPT,210,OS,47,PHP,65,SAD,53,SERVLETS,23,SOFTWARE ENGINEERING,245,SQL,71,TCP/IP,1,XHTML,9,XML,18,
ltr
item
Best Online Tutorials | Source codes | Programming Languages: PHP10-PHP Functions
PHP10-PHP Functions
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/10/php10-php-functions.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/10/php10-php-functions.html
true
357226456970214079
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content