Thursday , April 25 2024
Home / Core PHP / PHP 5 Functions

PHP 5 Functions

The real power of PHP comes from its functions; it has more than 1000 built-in functions.

PHP User Defined Functions
Besides the built-in PHP functions, we can create our own functions.

A function is a block of statements that can be used repeatedly in a program.

A function will not execute immediately when a page loads.

A function will be executed by a call to the function.

Create a User Defined Function in PHP
A user defined function declaration starts with the word “function”:

Syntax

function functionName() {
code to be executed;
}
Note: A function name can start with a letter or underscore (not a number).

Tip: Give the function a name that reflects what the function does!

Note Function names are NOT case-sensitive.
In the example below, we create a function named “writeMsg()”. The opening curly brace ( { ) indicates the beginning of the function code and the closing curly brace ( } ) indicates the end of the function. The function outputs “Hello world!”. To call the function, just write its name:

Example

Select Code
<?php
function writeMsg() {
  echo "Hello world!";
}

writeMsg(); // call the function
?>

About v.shakya

I am V.Shakya, Software Developer & Consultant I like to share my ideas, views and knowledge to all of you who come across my website. I am young, enthusiastic, highly motivated and self disciplined person. I completed my studies in Master of Computer Application and currently giving my technical expertise to one of the Big IT company. I have more than fifteen years of experience in vast field of Programming , Designing and Development of websites and various software's.

Check Also

PHP 5 Arrays

An array stores multiple values in one single variable: Example <?php $cars=array("Volvo","BMW","Toyota"); echo "I like …

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.