PHP Functions
What is Functions?
A function is a block of statements that can be used repeatedly in a program.
A function will not execute automatically when a page loads.
A function will be executed by a call to the function.
1- PHP Built-in Functions.
2- PHP User Defined Functions.
PHP has more than 1000 built-in functions, and in addition you can create your own custom functions.
- PHP Built-in Functions
- String Functions
- Numeric Functions
- Date Function
- Array Function
PHP User Defined Functions
1- Function Pass by Value
2- Function Pass by Reference
Advantages of User-defined Functions
1- As we have already seen a simple example of using a function above, you must have understood how time-saving it can be for large programs. Here are a few advantages of using functions for you:
2- Reusable Code: As it's clear from the example above, you write a function once and can use it for a thousand times in your program.
3- Less Code Repetition: In the example above we just had one line of code in the function, but what if we have 10 lines of code. So rather than repeating all those lines of code over and over again, we can just create a function for them and simply call the function.
4- Easy to Understand: Using functions in your program, makes the code more readable and easy to understand.
Function Pass by Value
1- Function with arguments.
<?php
function yourName($fname) {
echo "$fname";
}
yourName("Test User");
?>
Output: Test User
<?php
function nameAge($name, $age) {
echo $name.', '.$age;
echo '
';
}
nameAge("Test1", 25);
nameAge("Test2", 35);
nameAge('Test3', "20");
?>
output:
Test1, 25
Test2, 35
Test3, 20
Function with default argument value
<?php
function setHeight($minheight = 50) {
echo "The height is : $minheight ";
}
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>
Output :
350
50 // in this case in the function we have not pass any value to print default value
135
80
Functions - Returning values
<?php
function sum(int $x, int $y) {
$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(5, 10) ;
echo "7 + 13 = " . sum(7, 13) ;
echo "2 + 4 = " . sum(2, 4);
?>
Recursive function
A function that calls itself is called recursive function.
<?php
function factorial($n){
if ($n < 2 factorial(5) = ". factorial(5);
?>
Passing Arguments by Reference
When a function argument is passed by reference, changes to the argument also change the variable that was passed in. To turn a function argument into a reference, the & operator is used:
Related post
View ArticlesEknowledgePoint
Increase Your Knowledge with usEknowledgePoint is an innovative online learning platform that aims to democratize education by providing a vast array of courses and educational content to learners of all ages and backgrounds. It offers a diverse range of subjects, from academic topics to practical skills, making it a one-stop destination for anyone looking to expand their knowledge base.
5 comments
Knowledge is the fuel that powers our journey through life. It's platforms like EknowledgePoint that transform information into enlightenment, curiosity into competence, and dreams into reality. Embrace the opportunity to learn, and you'll find that the doors of possibility swing wide open. 🚀💡 #EknowledgePoint.
Thank you for your kind words! At EknowledgePoint, we are dedicated to empowering individuals on their learning journey. Your enthusiasm for knowledge and personal growth inspires us to keep providing the best educational resources and experiences. Let's continue to explore and learn together!.
Thanks For Your Support.
"I'm constantly amazed by the quality of instructors on EknowledgePoint. Learning from experts in their fields is both enlightening and empowering. 🌟👨🏫 #EknowledgePoint.
EknowledgePoint isn't just a platform; it's a community of learners. The support and camaraderie here are truly inspiring. 🤝🌍 #EknowledgePoint.
Leave a reply
Your email address will not be published. Required fields are marked *