Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
PHP | call_user_func() Function
Next article icon

PHP create_function() Function

Last Updated : 31 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The create_function() is an inbuilt function in PHP which is used to create an anonymous (lambda-style) function in the PHP.

Syntax:

string create_function ( $args, $code )

Parameters: This function accepts two parameters which is describes below:

  • $args: It is a string type function argument.
  • $code: It is a string type function code.

Note: Usually, these parameters will be passed as single quote delimited strings. The reason for using single quoted strings is to protect the variable names from parsing, otherwise, double quotes will be needed to escape the variable names, e.g. \$avar.

Return Value: This function returns a unique function name as a string, Otherwise returns FALSE on error.

Below programs illustrate the create_function() function in PHP:

Program 1: Creating an anonymous function with create_function()




<?php
  
// Create a function from information 
// gathered at run time,
  
$newfunc = create_function('$a, $b', 'return
       "ln($a) + ln($b) = " . log($a * $b);');
  
echo "New anonymous function: $newfunc\n";
echo $newfunc(2, M_E) . "\n";
?>
 
 
Output:
  New anonymous function: lambda_1  ln(2) + ln(2.718281828459) = 1.6931471805599  

Program 2: Create a general function with create_function()




<?php
  
// General function that can apply a set of 
// operations to a list of parameters.
  
function Program($value1, $value2, $arr)
{
    foreach ($arr as $val) {
        echo $val($value1, $value2) . "\n";
    }
}
  
// Create a bunch of math functions
$f1 = 'if ($a >= 0) { return "b * a^2 = ".
       $b * sqrt($a);} else { return false; }';
  
$f2 = "return \"min(a, b) = \".min(\$a, \$b);";
  
$farr = array(
    create_function('$x, $y', 'return 
       "a hypotenuse: ".sqrt($x * $x + $y * $y);'),
      
    create_function('$a, $b', $f1),
    create_function('$a, $b', $f2)
);
  
echo "first array of anonymous functions" . 
        "\nParameter is a = 2 and b = 3\n";
Program(2, 3, $farr);
  
// Now make a bunch of string functions
$sarr = array(
    create_function('$a, $b', 'return 
     "Lower case : " . strtolower($a) ;'),
    create_function('$a, $b', 'return 
    "Similar Character : " .
    similar_text($a, $b, $percent);')
);
  
echo "\nSecond array of anonymous functions" .
      "\nParameter is a = GeeksForGeeks and" .
      "b = GeeksForGeeks\n";
  
Program("GeeksForGeeks", "GeeksForGeeks", $sarr);
?>
 
 
Output:
  first array of anonymous functions  Parameter is a = 2 and b = 3  a hypotenuse: 3.605551275464  b * a^2 = 4.2426406871193  min(a, b) = 2    Second array of anonymous functions  Parameter is a = GeeksForGeeks andb = GeeksForGeeks  Lower case : geeksforgeeks  Similar Character : 13  

Reference: http://php.net/manual/en/function.create-function.php



Next Article
PHP | call_user_func() Function

M

Mithun Kumar
Improve
Article Tags :
  • PHP
  • Web Technologies
  • PHP-function
  • PHP-Function-handling

Similar Reads

  • PHP | function_exists() Function
    The function_exists() is an inbuilt function in PHP. The function_exists() function is useful in case if we want to check whether a function() exists or not in the PHP script. It is used to check for both built-in functions as well as user-defined functions. Syntax: boolean function_exists($function
    2 min read
  • PHP get_defined_functions() Function
    The get_defined_functions() function is an inbuilt function in PHP which returns the all defined functions in the array format. Syntax: array get_defined_functions( bool $exclude_disabled = true )Parameters: This function accepts one parameter that is described below: $exclude_disabled: It will chec
    2 min read
  • PHP current() Function
    The current() function is an inbuilt function in PHP. It is used to return the value of the element in an array which the internal pointer is currently pointing to.The current() function does not increment or decrement the internal pointer after returning the value.In PHP, all arrays have an interna
    2 min read
  • PHP count() Function
    The count() function in PHP is used to count the number of elements in an array or the countable properties of an object. The function returns an integer value representing the number of items present. Syntax: count($array, mode)In this syntax: $array (mandatory): Refers to the array, whose elements
    3 min read
  • PHP | call_user_func() Function
    The call_user_func() is an inbuilt function in PHP which is used to call the callback given by the first parameter and passes the remaining parameters as argument. It is used to call the user-defined functions. Syntax: mixed call_user_func ( $function_name[, mixed $value1[, mixed $... ]]) Here, mixe
    2 min read
  • PHP | ftp_connect() function
    The ftp_connect() function is an inbuilt function in PHP which is used to create a new connection to the specified FTP server or Host. When connection is successful then only other FTP functions can be run against the server. Syntax: ftp_connect( $ftp_host, $ftp_port, $timeout ); Parameter: This fun
    2 min read
  • PHP func_get_arg() Function
    The func_get_arg() function is an inbuilt function in PHP which is used to get a mentioned value from the argument passed as the parameters. Syntax: mixed func_get_arg( int $arg ) Parameters: This function accepts a single parameter as mentioned above and described below. $arg: This parameter holds
    2 min read
  • PHP define() Function
    The define() function is basically used by programmers to create constant. Constants in PHP are very similar to variables and the only difference between both are the values of constants can not be changed once it is set in a program. define() returns a Boolean value. It will return TRUE on success
    2 min read
  • PHP ob_get_clean() Function
    The ob_get_clean() function is an in-built PHP function that is used to clean or delete the current output buffer. It's also used to get the output buffering again after cleaning the buffer. The ob_get_clean() function is the combination of both ob_get_contents() and ob_end_clean(). Syntax: string|f
    2 min read
  • PHP ftruncate( ) Function
    The ftruncate() function in PHP is an inbuilt function that is used to truncate(shorten) an open file to the specified length. The file and the new size of the file are sent as parameters to the ftruncate() function and it returns True on success and False on Failure. If the size specified in the pa
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences