top of page

What are functions in PHP and how to use them?


PHP is an open source and free programming language. With this language you can easily make static web pages, dynamic web pages, and web applications. It is a server-side language. Let’s read about the functions in it.


Function simply refers to a set of code which performs a specific task. And we can use them many times when there is requirement. And by using functions we can save our time also. And it makes our work easier.


Functions in PHP are same as other languages. But, there are many built-in functions in this language. And, we can also make our own functions in it.


So, for more clarification let`s see an example:


<?php

function Message() {

echo "PHP is simple!";

}

Message();

?>


In this example, you can see that there are two parts:


1. Define a function: Defining function is the first step.

In which you have to use function keyword then give a name to the function. Always remember that is important to start the name of the function with letter. Even underscore can be used.


After this, give parenthesis. They need to be added just after the name of the function. We put parenthesis for giving parameters in it (if needed).

Now, use curly braces for giving statements and conditions inside it.


2. Call a function: Calling a function is the second step or you can say last step also too. For calling a function, you have to write name of the function.


Then check if there is any parameter value which is given in the function then you have to give arguments inside parenthesis just after the name.


PHP Built-in Functions:


There are many built-in functions in this language. But what exactly they are? Well, they are like pre-defined so that is why they are called as built-in too. For using them we just have to use the name of the function and it will automatically repeat a task through a piece of code. For example: echo, print, include, isset, empty, etc.


Here is an example of built in function:


<?php

print "PHP is server side language.";

?>


In this print function will automatically print "PHP is server side language." in the document.


PHP Arguments Function:


Arguments are used to give extra information as discussed above. And in order to use arguments, giving parameters in functions is necessary.

For example, follow this:


<?php

function studentsName($sname) {

echo "$sname ";

}

studentsName("Stale");

studentsName("Hege");

studentsName("Kai Jim");

?>


In the above example, there is a function with one parameter i.e. $name. So, for using this parameter we have to give arguments at the time of calling of a function as you can see in the example also.


There are many types of functions in PHP, but these are basic and easy to understand for beginners. For some more useful tips and guide you must go for PHP training institute in Delhi to learn this language at a good level.

You can also join web development courses to learn other languages too.

Recent Posts

See All

Comments


bottom of page