AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP Function


<< Previous Page
PHP delete element
Next Page >>
PHP GET and POST

PHP Tutorial » PHP Function

What are PHP functions?

A function is a piece of code that receives one or more parameter then process and returns a value.

PHP has a huge collection of internal functions that can call directly into your PHP code to perform a specific task as: gettype (), print_r (), var_dump, fopen(), fread() etc.

Syntax:

<?php
function functionName()
{
code to be executed;
}
?>

Note: Function names are NOT case-sensitive.

 

Create PHP Function

Let's create a PHP function that will write a simply message to your browser when you call it.

Example (this is HTML - PHP editor, change text, values on this window)
<?php
/* Defining a PHP Function */
function write_Message() {
echo "Welcome to agernic.com.
Today we will have a new lesson:
how to create a function in PHP";
}

/* Calling a PHP Function */
write_Message();
?>

Output: Welcome to agernic.com. Today we will have a new lesson: how to create a function in PHP

 

Related subjects:
PHP syntax What is an array PHP variabl Array to comma separated

 

Tags: php function: function use, return, return type, parameters. optional parameter, exists, use external variable, default parameter value, return array, function list
What are PHP functions?
What is PHP function with examples?
What is PHP function name?
Are there functions in PHP?

 

PHP Functions with Parameters

When you define a function you can specify parameters to accept input values

The parameters work as variables inside function

Example (this is HTML - PHP editor, change value of sumFunction() then click Execute)
<?php
function sumFunction($num_1, $num_2) {
$my_sum = $num_1 + $num_2;
echo "Sum of this two numbers is : $my_sum";
}

sumFunction(34, 21);
?>

Output: Sum of this two numbers is : 55

To see the result of the PHP code above please click Execute

 

PHP Function Arguments

An argument is like a variable

You can add as many arguments you need, pnly separate them with a comma.

Example (this is HTML - PHP editor, change value of surName() then click Execute)
<?php
  function surName($famname) {
echo "$famname Jones.<br>";
}

surName("Janet");
surName("Gregor");
surName("Stanly");
surName("Jim");
surName("Korg");
?>

 



php function: function use, return, return type, parameters. optional parameter, exists, use external variable, default parameter value, return array, function list
What are PHP functions?
What is PHP function with examples?
What is PHP function name?
Are there functions in PHP?
PHP Function, Creating PHP Function - how? - example - php tutorial

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...