ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing

ExpertRating Home
ExpertRating Benefits
Recommend ExpertRating
Suggest More Tests
Privacy Policy
FAQ
Login
 
PHP Tutorial - Index
PHP Tutorial - Introduction to PHP
PHP Tutorial - Your First PHP Script
PHP Tutorial - Operators
PHP Tutorial - Conditional and Looping
PHP Tutorial - Functions
PHP Tutorial - Classes and Objects
PHP Tutorial - Working with Forms
PHP Tutorial - Advanced PHP Topics
PHP Tutorial - Advanced Class Concepts
     

 
Online Personal Trainer Certification
Personal Trainer Certification (Spanish)
Six Sigma Certification
Find a Personal Trainer
Project Management Certification
Six Sigma Certification
Six Sigma Black Belt Certification
Master Black Belt Certification
Six Sigma Green Belt Certification
Life Coach Certification
Lean Management Certification
Lean Six Sigma Certification
First Aid Certification
CPR Certification
Yoga Certification
Aerobics Certification
Pilates Certification
Business Analyst Certification
SEO Certification
Online Business Writing Course & Certification
Makeup Artist Certification
Baby Sitting Course & Certification
Time Management Certification
Health Club Management Certification
Medical Coding Certification
Medical Billing Certification
Business Analysis Course
Master Trainer Certification
Total Quality Management Certification
Kaizen Certification
Creative Writing Certification
Event Management Certification
Test Search by Job Title
Administrative Assistant Certification
Virtual Assistant Certification
  All Online Courses
   

   
   
   
   
   
   

   
  Home > Courses, Tutorials & eBooks > PHP Tutorial > Functions
 

PHP Tutorial - Functions

 
Page 1 of 1
 

In this chapter you will be getting technical know how of functions. Functions are useful in any type of programming language. Whenever you want to use a particular piece of code over and over again, it is very useful to put that code in a function. This is done so that the code can be reused easily. Another use of using functions is that whenever there is an error in the code, and then it has to be corrected only at one place; in the function. With the use of functions, the entire code can be understood easily.

Declaring Functions

Creating your own function in your script is a straightforward task. It is like creating a function in any other language. The functions in PHP begin with the keyword“function” which is followed by the function name. Following the function name there is a set of parenthesis which contains the parameters to be passed. These parameters are an optional set of variables passed to the function.

Syntax

function <name of function> ([$var1 [= constant]], [$var2 [= constant]], …)

{

Body of the function;

}

 

Your First Function

Now that you have got an idea of th2e functions, you are ready to start with functions. In the following example the string “Welcome to expertrating!” will be printed five times on the browser with the help of functions. By writing a single code once in a function, we can call the same function again in a script.

Example

<html>

<body>

 

<?php

 

function fundisp()

{

for($i = 0; $i < 5; $i++)

{

echo "Welcome to expertrating!<br/>";

}

}

 

echo "After this statement the function will be called.<br/><br/>";

fundisp();

echo "<br/>";

echo "This statement is printed after the function has been called.";

echo "<br/>";

?>

 

</body>

</html>

 
 

Output

Passing Parameters to Functions

In this section we will be discussing about how to pass parameters to a function in PHP. A parameter of a function is a piece of data that a function requires to execute. If we define parameters formally then, function parameters are represented by variable names which are located within the parenthesis of the function definition.

In the following example of passing parameters to functions we will be displaying the string “Welcome to expertrating!.” The difference that this example has got with the previous example is that, in this example the number of times the text will be displayed is not fixed, rather that number is passed as a parameter when the function is called.

Example

<html>

<body>

 

<?php

 

function fundisp($num)

{

for($i = 0; $i < $num; $i++)

{

echo "Welcome to expertrating!<br/>";

}

}

 

echo "After this statement the function will be called.<br/><br/>";

fundisp(7);

echo "<br/>";

echo "This statement is printed after the function has been called.";

echo "<br/>";

?>

 

</body>

</html>

Output

 

     
 

 

 
     
Home  |  About Us  |  Privacy Policy  |  Site Map  |  FAQs  |  Contact Us
 
© ExpertRating 2015. All Rights Reserved.