How to work in functions PHP?

If there is any task in your program which you train to execute repeatedly then instead of writing the code for that task at a different place in the program you can create a task and whenever you want to execute that task You can call that function at a different place if you need to.

By doing this, your time is also saved and computer memory is also saved, as well as your program also becomes short and readable, this is called code re-usability because you can use the same code at different places.

Syntax:-

<?php

    function functionName(){
        Statement;
    }

    functionName();
    
?>

Example:-

<?php
	function functionName(){
		echo "This is PHP function <br>" ;
	}

        function newFunction(){
   
		echo "This is New PHP function <br>" ;
	}

	functionName();
        functionName();
        echo "Hi this is an example <br>";
        newFunction();
        newFunction();
?>

Output:-

PHP Functions with Parameters

PHP gives you the option of passing your parameters inside a function so that you can pass more parameters.

<?php

    function functionName(parameter1,parameter2){
        Statement;
    }

    functionName(argument1,argument2); 
    
?>

Example:-

<?php
	function sum($a, $b){
        $sum = $a + $b; 
		echo "This in parameter function $sum" ;
	}

	sum(10,20);
?>

Output:-

Related Posts

Associative Array in PHP?

An associative array is a simple and very important array of PHP. This array is also used the most, it is very easy to understand this array,…

For Loofs in PHP

In for-loop initialization, condition and increment happen on the same line. The loop keeps on repeating as long as the condition is true. When the for loop…

If Else Conditionals in Php?

If Else statement is the most important feature of any programming language, PHP is used to run any Code Of Block according to If Else conditions. This…

What are operators in PHP? PHP – Operator Types

Operators are used to developing data. In Programming Languages, Operators are taken from Mathematics and Operator Programmers work with Data. An operator is a chain of symbols…

String Functions in PHP?

When many characters are written together in sequence, they form a string. For example, India is a string. It is made up of i, n, d, i,…

What is the Variable And how does it work?

The variable is seen here as a container, which you can use to store data information. It doesn’t make any difference to him, so it becomes easy…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x