🟡 Control Flow  ·  Lesson 09

Functions in PHP

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 हो सकती हैं।
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।