我如何用变量名称创建多个函数? [英] How can I create multiple functions with variable in name?

查看:103
本文介绍了我如何用变量名称创建多个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能:

$ p $ function display_cell_1_url(){



$ b $ p
$ b

如何声明这个函数10次并替换 _1 _ 在函数名称和函数体?

解决方案

不,不要尝试拥有10个函数!这不是编程的工作方式。



相反,您应该做的是一个函数执行相同的操作 10次 STRONG>。这就是为什么我们有这个令人难以置信的东西叫做循环您可以像这样使用它们:



$ p $ function display_cell_url($ numOfCell){

for($ i = 1; $ i <= $ numOfCell; $ i ++){
echo'< input type =textname =cell _'。$ i .'_ urlid =cell _'。$ i。 '_url'value =''。get_option('cell _'。$ i .'_ url')。'/>';
}
}

//使用这个函数,像这样
$ b $ display_cell_url(10);

这是相当基本的东西。如果您是PHP新手,您应该阅读一些有关该语言基础知识的课程。



Code Academy有一个很好的 免费 PHP课程: HERE


I have this function:

function display_cell_1_url() {
    <input type="text" name="cell_1_url" id="cell_1_url" value="<?php echo get_option('cell_1_url');" />
}

How can I declare this function 10 times and replace _1_ in function name and function body?

解决方案

No, do not try to have 10 functions! That is not how programming works.

Instead what you should do is have one function that does the same thing 10 times. That's why we have this incredible thing called loops. You can use them like so:

function display_cell_url($numOfCell) {

    for($i = 1; $i <= $numOfCell; $i++) {
         echo '<input type="text" name="cell_'.$i.'_url" id="cell_'.$i.'_url" value="'.get_option('cell_'.$i.'_url').'" />';
    }
}

// Use this function like this

display_cell_url(10);

This is fairly elementary stuff. If you are new to PHP you should go through some course about the basics of the language.

Code Academy has a great free PHP course: HERE

这篇关于我如何用变量名称创建多个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆