Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial php的内置函数create_function创建出来的函数 参数列表与函数体引号问题

php的内置函数create_function创建出来的函数 参数列表与函数体引号问题

Jun 23, 2016 pm 02:17 PM

create_function创建的函数

使用php的内置函数create_function创建出来的函数。
create_function('$item','return $item*3;')
问一下各位 为什么创建出来的函数 参数列表与函数体都要加上引号呢??

回复讨论(解决方案)

string create_function ( string $args , string $code )
两个参数都是字符串

那提交给函数的参数不是也可以是变量吗

因为 create_function 要求传入2个字符串参数

你的代码等价于

function tmp($item) { return $item*3; }$b = array_map('tmp', $a);
Copy after login

用闭包就可以
$b = array_map(function($item) { return $item*3; }, $a);
Copy after login

那提交给函数的参数不是也可以是变量吗

$item已经是提交给匿名函数的变量了。

$a=array(100,200,300);
$b=array_map(create_function('$item','return $item*3;'),$a);
你的意思是说('$item','return $item*3;')它们两个只是create_function这个函数的两个参数而已 它内部会自动创建这个函数是吗

$a=array(100,200,300);
$b=array_map(create_function('$item','return $item*3;'),$a);
var_dump($b);
create_function要求传入两个字符串 可是我这里$a的参数是100 200 300 是数值型的呀 求帮助

function tmp($item) { return $item*3; }
$b = array_map('tmp', $a);
写成这样,你是否有疑问呢?

create_function 要求传入两个字符串
并不代表 create_function 产生的函数也要求传入两个字符串
create_function 要求传入的两个字符串,分别是:匿名函数的参数列表和函数体

$a=array(100,200,300);
$b=array_map(create_function('$item','return $item*3;'),$a);
var_dump($b);
create_function要求传入两个字符串 可是我这里$a的参数是100 200 300 是数值型的呀 求帮助

你似乎混淆一个概念:
create_function是创建匿名函数,他本身(create_function)是一个函数,需要传入的参数是字符串。
而创建出来的函数的参数就是你要传递的参数,也就是函数的 形参 ,100,200,300是传给创建出来的函数的 实参,由于创建的匿名函数内部是直接使用该参数的,所以传进去是数值型,使用的就是数值型。如果传递的不是数值,例如是字符串0123,由于有*操作符,所以会先转化为数值然后操作:
请看:
$a=array(100,200,"011");
$b=array_map(create_function('$item','return $item*3;'),$a);
var_dump($b);

谢谢你 明白了。对了还有一个问题 我是初学者 有很多不明白地方 函数的形参都是以变量来定义的吗

是的都是以变量的形式来定义的

非常感谢上面的高手们

默默收藏

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles