Jump to content

Calling A Function From Within Heredoc Syntax


Recommended Posts

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. :lol:

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...