spot_img

Functions in PHP – Using and Defining Functions in PHP

How to do PHP Tutorial on Functions in PHP?

Functions in PHP – From our previous tutorials, we discussed some basic functions. For example, we used a count(); function to get the length of an array. It was a built-in function, there are plenty of others as well. So today, we will actually create some functions of our own and use them. But before we start you must understand the scope of a function. Every functioned defined in one page can only be accessible to that specific page only, you can also create a separate file of functions that you need in your web page and link this file to all the webpages.

Function without any Argument

A function can be defined using a very simple structure. First, write the keyword “function” then after space write the name of your function following with ‘()’ and finally for the boundary, we write ‘{}’. Using this kind of syntax a simple function will be like

            function print_Hello(){

                                                echo “Hi there!!!”;

}

Now, once a function is defined we can call it within that file using its name with ‘()’ e.g. print_Hello();

Here is how it will look like in the code

Functions in PHPIt is clear from the above example that you are free to define a function anywhere you want. But calling a function must be on where you want to place it. Since we wanted to output it in the body of the page, that’s why we called the function inside a body tag. The output of the code is

Functions in PHPFunctions With Single Argument

There is a possibility to define a function that takes arguments and applies some functionality to them. For example, what if we write a function that’ll take a String and print it. Here

Functions in PHPFunction With More Than One Arguments

There could be more than 1 arguments in a function. We separate all the arguments with a comma just like this

function anytime($first, $second, $third,. . .)

Return Statement in a Function

Let’s say we need a function that takes 3 integers as arguments and multiply them. But we instead of printing the result want to use the resulting integer in some other calculations. This is possible using a return statement.

Functions in PHPIt is clear from the example above that function returned an integer which we stored in a variable $result. And we were also able to increment in this value before printing it. And as a result, this value was printed.

Functions in PHPPrinting Table by Using a Function

Since now we know how to use a function. It’s time to modify our previous code of printing a table and make it more generic. Here is a function that will take an integer and print its table.

<?php

function table_Of($int){

$count=1;

for($i=0; $i<20; $i++){

echo “$int x $count = “.($int*$count).”<br>”;

$count++;

}

}

?>

And while calling it, we can add any integer we want. The example below shows the running of the code with a value of 5.

Functions in PHPOut of this specific example is given below

Functions in PHPFinal Words

This is how we make our programs much more efficient. Can you go one step further? Here is the task you can perform to test your learning from this tutorial.

  1. Make a function that takes 3 integers as input.
  2. Print a table of the first integer, starting from the second integer and ending at third integer.
  3. For example, if we call it this way table_Of(5,7,17); This output must be printed.

Functions in PHP

Part One: HOW TO RUN YOUR FIRST PHP SCRIPT ON WINDOWS
Part Two: BASICS OF HTML5 ON THE WAY TO PHP LEARNING
Part Three: PHP WORKING WITH DATA TYPES AND VARIABLES
Part Four: IF ELSE STATEMENTS AND ARITHMETIC OPERATIONS IN PHP
Part Five: TUTORIAL ON TYPES OF LOOPS IN PHP – PHP LOOP TYPES
Part Six: INDEXED AND ASSOCIATIVE ARRAYS IN PHP

Main Image Source : Pixabay

Also See : Get Paid For Searching The Web

spot_img
Dave P
Dave P
Be a little better today than yesterday.
spot_img

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay Connected
41,936FansLike
5,721FollowersFollow
739FollowersFollow

Read On

spot_img
spot_img

Latest