B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP PHP Callback Functions | LiveCodeProgramming

PHP Callback Functions

Callback functions are functions passed as arguments to other functions. They are often used in array operations or custom logic handling.

Callback functions वे functions होते हैं जिन्हें हम किसी अन्य function को argument के रूप में पास करते हैं। यह अक्सर array operations या कस्टम लॉजिक में उपयोग होते हैं।

Passing Named Function as Callback

You can pass the name of a function as a string to another function and call it from there.

आप किसी function का नाम string के रूप में किसी दूसरे function को पास कर सकते हैं और वहाँ से उसे कॉल कर सकते हैं।

<?php
function greetUser($name) {
  echo "Hello, $name!";
}

function executeCallback($callback, $value) {
  $callback($value);
}

executeCallback("greetUser", "Ravi");
?>
Hello, Ravi!

Passing Anonymous Function as Callback

You can also use anonymous functions as callbacks.

आप anonymous functions को भी callback के रूप में उपयोग कर सकते हैं।

<?php
function process($callback) {
  $callback("Amit");
}

process(function($name) {
  echo "Welcome, $name!";
});
?>
Welcome, Amit!