Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to call a function in a string using PHP
    primarykey
    data
    text
    <p>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 <code>pickDate()</code> in my string it is rendering out <code>pickdate()</code> (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.</p> <p>Here is what my function looks like:</p> <pre><code>&lt;?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 .="&lt;select name='month'&gt;"; for($i=0; $i&lt;31; $i++){ array_push($dayArray, "$d"); $d++; } for($i=0; $i&lt;12; $i++){ $selected=""; if ($month == "$monthArray[$i]") { $selected="selected='selected'"; } $monthOption .="&lt;option value='$monthArray[$i]' $selected&gt;$monthArray[$i] &lt;/option&gt;"; } $monthOption.="&lt;/select&gt; &lt;select name='day'&gt;"; for($i=0; $i&lt;31; $i++){ $selected=""; if ($day == "$dayArray[$i]") { $selected="selected='selected'"; } $monthOption .="&lt;option value='$dayArray[$i]'$selected&gt;$dayArray[$i] &lt;/option&gt;"; } $monthOption.="&lt;/select&gt;"; echo "$monthOption"; }//end function ?&gt; </code></pre> <hr> <p>My function works fine. I want to know how I can call it in the string in this for loop below:</p> <pre><code>&lt;?php $xml = simplexml_load_file("gallery.xml"); $c1=count($xml-&gt;Picture); for($i=0; $i&lt;$c1; $i++){ $month =(string)$xml-&gt;Picture[$i]-&gt;picDate-&gt;month; $day =(string)$xml-&gt;Picture[$i]-&gt;picDate-&gt;day; $year =(string)$xml-&gt;Picture[$i]-&gt;picDate-&gt;year; $displayFolder .="&lt;form action='gallery.php' enctype='multipart/form-data' method='post' name='editPic' id='editPic'&gt;&lt;tr&gt;&lt;td&gt; pickDate() &lt;input type ='text' name='year' size='4'maxlength='4' value='$year'/&gt;&lt;/form&gt;&lt;/td&gt; &lt;/tr&gt;"; } ?&gt; </code></pre> <p>then I output the displayfolder string in a html table below:</p> <pre><code>&lt;table border= "1px"&gt;&lt;tr&gt;&lt;td&gt;&lt;?php print"$displayFolder";?&gt;&lt;/table&gt; </code></pre> <p>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 <code>pickDate()</code> 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. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload