oompahloompah Posted October 19, 2004 Share Posted October 19, 2004 How do I call a function from within the heredoc syntax in PHP? I would like to use a function to display parts of the entire text within the heredoc syntax repeatedly throughout itself. For example, the text that I would like to display is: Mary had a little lamb, little lamb, little lamb. The entire sentence is contained within the heredoc syntax and I would like to use a function to repeat little lamb three times instead of having to type it in full within the syntax. Can this be done? I'm tearing my hair out on this. Edit: I could probably use a variable to store little lamb but unfortunately the piece of text that I wish to repeat has slight variations that makes a function more suitable instead. Quote Link to comment Share on other sites More sharing options...
borfast Posted October 19, 2004 Share Posted October 19, 2004 I'm pretty sure the heredoc syntax does not allow you to use functions within it, only variables Quote Link to comment Share on other sites More sharing options...
oompahloompah Posted October 19, 2004 Author Share Posted October 19, 2004 Ahhh.. Is there a way of storing the heredoc text in a variable in one particular function in a file and call it up again within another heredoc text in another function on another file? Quote Link to comment Share on other sites More sharing options...
TCH-Don Posted October 19, 2004 Share Posted October 19, 2004 Can you explain in more detail what you are trying to do. And maybe give examples of what yo have tried. Don't forget when using heredoc the delimiter must be by itself on a line after the text The only exception is a ; Quote Link to comment Share on other sites More sharing options...
oompahloompah Posted October 19, 2004 Author Share Posted October 19, 2004 I would like to have two files using the heredoc syntax, one handling the user interface and the other handling the headers and footers of a page. The one handling the UI is a function accepting parameters to set the attributes of the UI for example, a button would have the title and href link attributes which would be set via a function. The function handling the headers and footers has buttons inside them and I would like to somehow get the output of the earlier-mentioned function at various places within the heredoc text output of this function. I have tried using a return instead of an echo for the UI function in one file. Then I called the UI function from the other file containing the header/footer function. I plugged the output of the UI function to a variable and I inserted the variable into the header/footer heredoc text. The error I get is a parse error stating something about an unexpected $ sign. I've checked my brackets and they all match. I've checked my variable names and they all have their corresponding $ signs. I've also checked that when the UI function accepts a parameter, it has '' delimiting the parameters. Would be grateful to see a working implementation of this idea. Quote Link to comment Share on other sites More sharing options...
borfast Posted October 19, 2004 Share Posted October 19, 2004 Well, if the variable is a global variable (if it is visible in the scope of the function where you want to use it), then yes, I think you can. Quote Link to comment Share on other sites More sharing options...
oompahloompah Posted October 20, 2004 Author Share Posted October 20, 2004 What is the difference between superglobal and global variables? I think I understand how they work when it comes to passing data between a form and another output page but I don't know how to implement a superglobal in this case. The reason why I'm asking is because I read up somewhere on the PHP manual which says that globals would soon be deprecated? In this case, which superglobal array should I use? Environment, session, server, request, post, get, cookies or? Quote Link to comment Share on other sites More sharing options...
stevevan Posted October 20, 2004 Share Posted October 20, 2004 Forgive me for butting in, but for my own knowledge, what is "heredoc syntax"? Thx....resume thread. Quote Link to comment Share on other sites More sharing options...
TCH-Rob Posted October 20, 2004 Share Posted October 20, 2004 Forgive me for butting in, but for my own knowledge, what is "heredoc syntax"? Thx....resume thread. Here-doc syntax is just an easy convention for specifying strings that take up several lines and/or that could have otherwise reserved characters Here is more information; http://kwiki.org/?HereDoc Quote Link to comment Share on other sites More sharing options...
borfast Posted October 20, 2004 Share Posted October 20, 2004 The superglobals are environment variables that are readily available anywhere in a script. A global variable is a variable you declare as being accessible in the scope of where you declare it to be accessible. What you have seen about global variables being deprecated is something that has already happened. Quite some time ago, actually It's basically a security measure: by default, PHP used to have the register_globals configuration directive set to 1 and they changed it to 0. Read more about it here and here. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.