unknown os platform
Free Beginner PHP Tutorials - basic functions without parameters | Whats-online.info

Free Beginner PHP Tutorials - basic functions without parameters

2016:09:23 / Science and Tutorials

PHP basic function without parameters and no return value

PHP user -defined functions

We can define PHP Function as a block of code which takes one or more parameters and can repeatedly be used in a program.

Advantages of functions
-Functions minimize the repetition of code by calling it whenever it is required.
-Functions simplify programming. 
-Functions simplify program maintenance.

PHP Functions are divided into two.
-Built-in functions
-User-defined functions.

Built-in functions

This are already defined functions which are called within a program. Example of PHP defined functions.
are fopen(),strlength(),substring() etc.

User-defined functions

These are functions which are defined by the user to handle an individual block of code.

Rules of writing PHP functions
-A valid function name should start with a letter or underscore, followed by any number of letters, numbers, or underscores. 
-Function name should start with keyword 'function'
-Code inside a function should be enclosed by curly braces {};

For our tutorial, we will discuss how to create a basic user-defined function without parameters.

A function will not run immediately when a page loads.
A function executes by a call to the function. 


<?php function message($argument1 ....$argumentn){    //code of block } ?>

User defined function without parameters or return value to find the area of a rectangle.

<?PHP   function areaOfSquare(){       $length=20;    $width=20;        $area = $length * $width;    echo  "The area of a square is ".$area;  } //calling the function    areaOfSquare(); ?>  

output: The area of a square is 400

The above PHP function example finds the area of a square and prints it.
The areaOfSquare() is then called within the program to execute.

You may also like:

Leave a comment

Subscribe to get replies direct to your mailbox

Submit comment