🟡 Control Flow · Lesson 09
Functions in PHP
What is a Function?
A function is a reusable block of code. Define it with the
function keyword.Example
<?php
function greet($name) {
return "Hello, $name!";
}
echo greet("Aman");
?>Hello, Aman!
Default Parameters
<?php
function power($base, $exp = 2) {
return $base ** $exp;
}
echo power(5); // 25
echo power(2, 3); // 8
?>
Summary
- Define functions with
function name($params) { return ...; }. - Parameters can have default values.
Function क्या है?
Function code का reusable block है। इसे
function keyword से define करें।Example
<?php
function greet($name) {
return "Hello, $name!";
}
echo greet("Aman");
?>Hello, Aman!
Default Parameters
<?php
function power($base, $exp = 2) {
return $base ** $exp;
}
echo power(5); // 25
echo power(2, 3); // 8
?>
सारांश
- Functions
function name($params) { return ...; }से define करें। - Parameters की default values हो सकती हैं।
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.