1

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.

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.

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

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
  • As an additional tip, rather than relying on global variables, just pass the necessary parameters to your function: define as function pickDate($month, $day) { ... } and call as pickDate($month, $day);. That way, you can call it lots of times with different variables, and from other functions, etc. Commented Sep 1, 2013 at 19:30
  • Thanks so much for the quick tips and answers, everyone. That makes sense now. Its nice to understand a bit more how functions work. I hope the function was clear enough to be useful to someone besides me. stackoverflow is great. Commented Sep 2, 2013 at 11:16
  • @james if an answer works for you or solves the problem, then plz mark that as answer which would help others ;) Commented Oct 23, 2013 at 8:57

3 Answers 3

4

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;
Sign up to request clarification or add additional context in comments.

4 Comments

With this solution the pickdate will be printed as soon as $displayFolder is defined, which is an undesired effect.
Sorry, didn't see that pickDate() echoes directly instead of returning. I'll update my answer.
It's also a good idea to explain what you've changed in the code and why, rather than leaving the reader to do a "spot the difference" between before and after code blocks.
Thanks, I edited my answer to explain the difference between "before and after" and added a fix for that print issue. Any other suggestions will be greatly appreciated.
1
  • don't use the function to echo stuff, this usually gives you less control on where is that string printed. Just return stuff and decide where you want it printed afterwards.

Instead of

echo "$monthOption";

Try

return $monthOption;
  • You are not evaluating the function inside your string. Either enclose it in moustaches or even better, split your string:

Instead of

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

Try

$displayFolder .="<form action='gallery.php' enctype='multipart/form-data' method='post'     name='editPic' id='editPic'><tr><td>";
$displayFolder .=pickDate();
$displayFolder .="<input type ='text' name='year' size='4'maxlength='4' 
value='$year'/></form></td> </tr>";

1 Comment

That splitting technique makes excessive verbose, specially if he has to use more than one function in the string.
0

Concat php function in string like

$str = "left string".pickDate()."right string";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.