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条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)