[Solved] How can I create my own function in PHP?

  

4
Topic starter

I want to create function to calculate 3 numbers.

Is it posible to create my very own function in Php?

How can I do it and use it?

2 Answers
3

You should always read the official documentation.

Here is the page for user-defined functions: https://www.php.net/manual/en/functions.user-defined.php

You can also use offline documentation program for software developers: Zeal. I use it a lot!

Here is the code to sum up 3 numbers - it is a nice and easy example of creating own functions:

<?php
 
function sum( $a, $b, $c ) {
    echo $a + $b + $c;
    echo '<br>';
}
 
$x = 100;
$y = 10;
$c = 1000;
 
sum( $x, $y, $c );
sum( 3, 9, 90 );
 
?>

and the results:

php my function result image

On liine #3 where the function is declared in the brackets of sum() - you should I add your own parameters - and then you can use variables or your defined numbers/string, etc.

1

Here is one short YouTube video (8 minuties+ ):

Share: