duanji6997 2013-09-01 19:17
浏览 55
已采纳

如何使用PHP调用字符串中的函数

I wrote this php 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. 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.

Here is what my function looks like:

<?php
function pickDate() {
$dayArray=array();
global $month;
global $day;
$monthArray=array();

$d=1;
$monthArray=array('January','February','March','April','May','June','July','August','September','October','November','December');

$monthOption .="<select  name='month'>";

for($i=0; $i<31; $i++){
array_push($dayArray, "$d");
$d++;
}
for($i=0; $i<12; $i++){

$selected="";

if ($month == "$monthArray[$i]") {
$selected="selected='selected'";
}
$monthOption .="<option value='$monthArray[$i]'
$selected>$monthArray[$i] </option>"; 



}

$monthOption.="</select> <select name='day'>";

for($i=0; $i<31; $i++){

$selected="";

if ($day == "$dayArray[$i]") {
$selected="selected='selected'";
}
$monthOption .="<option value='$dayArray[$i]'$selected>$dayArray[$i] </option>"; 



}

$monthOption.="</select>";

echo "$monthOption";

}//end function
?>

My function works fine. I want to know how I can call it in the string in this for loop below:

<?php
$xml = simplexml_load_file("gallery.xml");
$c1=count($xml->Picture);


for($i=0; $i<$c1; $i++){
$month =(string)$xml->Picture[$i]->picDate->month;
$day =(string)$xml->Picture[$i]->picDate->day;
$year =(string)$xml->Picture[$i]->picDate->year;


$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>";

}

?>

then I output the displayfolder string in a html table below:

<table border= "1px"><tr><td><?php 

print"$displayFolder";?></table>

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.

  • 写回答

3条回答 默认 最新

  • doushang1880 2013-09-01 19:23
    关注

    Replace this:

    $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>";
    

    With this:

    $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>";
    

    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";
    

    With this:

    return $monthOption;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误