如何使用PHP调用字符串中的函数 [英] How to call a function in a string using PHP

查看:43
本文介绍了如何使用PHP调用字符串中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个函数来在下拉选项标签中动态插入默认日期.在名为 pickDate 的函数中,我用双引号将末尾的字符串回显.然后在我的代码中,我有一个 for 循环,它生成一个新字符串,在字符串中我试图调用我的函数.

I wrote a function to dynamically insert the default date in a drop down option tag. In my function called pickDate I echo out the string at the end with double quotes. Then later in my code I have a for loop that produces a new string and inside the string I am trying to call my function.

我的问题是,当我在字符串中调用 pickDate() 时,它渲染的是 pickdate()(字面意思)而不是函数的结果.查看我的代码,我的问题是:如何在双引号包围的字符串中调用函数.

My problem is that when I call pickDate() in my string it is rendering out pickdate() (literally) and not the result of the function. Looking at my code, my question is : how do I call a function within a string surrounded by double quotes.

我想知道如何在下面的 for 循环中的字符串中调用它:

I want to know how I can call it in the string in this for loop below:

<?php
$displayFolder .= "<form action='gallery.php' enctype='multipart/form-data' method='post' name='editPic' 

id='editPic'><tr><td>
pickDate()
<input type ='text' name='year' size='4'maxlength='4' 

value='$year'/></form></td> </tr>";
?>

所以问题又是调用 pickDate 函数,因为它是一个字符串,我想知道如何将 pickDate() 结果放入表中.我喜欢它被双引号包围的方式.有人可以告诉我如何在字符串中正确调用该函数而不必将我的字符串更改太多.

So the problem again is calling the pickDate function cause it is in a string and I would like to know how I can out put the pickDate() result in the table. I like how it is all surrounded by double quotes. Can someone please tell me how to call the function properly in the string without having to change my string to much.

推荐答案

替换:

$displayFolder .="<form action='gallery.php' enctype='multipart/form-data' method='post' name='editPic' 

id='editPic'><tr><td>
pickDate()
<input type ='text' name='year' size='4'maxlength='4' 

value='$year'/></form></td> </tr>";

有了这个:

$displayFolder .="<form action='gallery.php' enctype='multipart/form-data' method='post' name='editPic' 

id='editPic'><tr><td>
".pickDate()."
<input type ='text' name='year' size='4'maxlength='4' 

value='$year'/></form></td> </tr>";

上面唯一需要的更改是使用连接运算符(点)将 pickDate() 函数与字符串的其余部分隔离开来.此外,为了使其工作,您的 pickDate() 函数必须返回其结果而不是直接输出,否则它会在 $displayFolder 时立即输出其内容定义.要解决此问题,请将其替换:

The only change needed above was isolating your pickDate() function from the rest of the string using the concatenation operator (the dot). Also, in order for this to work, your pickDate() function must return its result instead of outputting directly, otherwise it'll output its contents as soon as $displayFolder is defined. To fix that, replace this:

echo "$monthOption";

有了这个:

return $monthOption;

这篇关于如何使用PHP调用字符串中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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