How to work in functions PHP?

Posted by

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

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:-

Leave a Reply

Your email address will not be published. Required fields are marked *

0
Would love your thoughts, please comment.x
()
x