settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: How can I create my own function in Php?

+3 votes
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?
asked in PHP category by user andrew

2 Answers

+3 votes

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.

answered by user hues
Thanks! Will use the official docs and will try the software you've suggested. CHeers
+1 vote

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

https://www.youtube.com/watch?v=uOLGQ9ZoOSc

answered by user eiorgert
edited by user golearnweb
...