Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Get accustomed with PHP Functions

Saturday, October 18th, 2014 by Servage

Functions in PHP is a vital component of programming as it saves lots of time and space of the PHP programmers. If you are familiar with PHP functions, you can do anything with their custom build and make your coding advanced. Offering dynamic and highly user experienced web application through usage of functions is possible.

Syntax for PHP Functions

In PHP or in any programming language repetition of code is harmful in several ways. It takes valuable time of programmers. It makes document bulky hence loading difficult. Code seems cluttered and messy to follow at post-production stages or during maintenance/updates.

 

In due course, PHP functions offer golden opportunities to the PHP developers to make their coding clean, compact, and comprehensive. As PHP functions are the set of predefined commands. Once you write the PHP function, you can execute them as per your wish and several times, just by calling them in the execution code.

 

However, there are more than thousands in-built PHP function exist, but it is easy to build your own easily. You can write one function for one task or several function for an intricate task at a time. Same the way, you can write several functions for several tasks and call them as per needs. These all make PHP function quite simple as well as enough complex to comprehend for a fresher programmer.

 

These flexibilities of PHP functions have made them popular and language more acceptable. It has also contributed to make PHP more Object Oriented Programming language. Therefore, I have initiated this post to introduce beginners to get in touch with PHP functions.

 

User Defined PHP Functions

As stated above, a function is a block of statements in PHP document. They are predefined means they won’t execute immediately when a web page is loading. They are executing when the programmer make a call to the function in code. Otherwise, they are stored in the PHP documents and can be used repeatedly several times just by calling them.

 

You can define a function by creating a declaration statement in the code. They begins with the name of function, a meaningful name is must to remember them. The name of function follows by a parenthesis where the argument is sitting and you can set arguments through their values. Therefore, you have ample chance to include a single argument (simple function) as well as multiple arguments (complex functions) using simple coma separations.

 

Syntax of PHP Functions

Syntax of PHP is function is quite simple and easy to write as its phrases contains only name of function, argument/s, code to execute. For instance:

function functionName() {
code to be executed;
}

Practically we can understand the syntax by putting some example values such as:

<?php
function writeMsg() {
echo "Hello world!";
}
writeMsg(); // call the function
?>

Here you may have noticed that all information passed to function through its arguments in parenthesis just next to the function name. Therefore, if you wish to add complexities, simply add more arguments while calling function or using comma in between them. Look at the following example:

<?php
function familyName($fname,$year) {
echo "$fname Refsnes. Born in $year <br>";
}
familyName("Hege","1975");
familyName("Stale","1978");
familyName("Kai Jim","1983");
?>

 

If we call a function without argument, it will take default value as argument. For instance:

<?php
function setHeight($minheight=50) {
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>

Same the way if you wish to let a function return a value, you can use the return statement.
References & More Reading
PHP 5 Functions
Function
PHP Functions
PHP Function List

Get accustomed with PHP Functions , 3.7 out of 5 based on 3 ratings
Categories: Guides & Tutorials

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.