Connecting Tech Pros Worldwide Forums | Help | Site Map

Getting a function result while in heredoc

bill
Guest
 
Posts: n/a
#1: Dec 19 '06
Thanks to those that taught me to use heredoc to embed html in
php. I don't want to start the html in php vs php in html
discussion again.,

heredoc works a treat for that which I am doing except:
In the generated html I have a number of checkboxes. I fill
in the checkboxes from a boolean value in a mySQL table. To make
this easier I use a function that takes the boolean and return
either 'checked' or ''. This worked well when I was
concatenating the html into a variable rather than using heredoc,
but it was really messy.

Short, simple question: is there anyway to get php to evaluate a
function while in a heredoc block ?

I know I could assign the results of using the function to
variables outside of the heredoc block or I could break the block
for the function, but the first is cumbersome and the second gets
rid of the clarity of the heredoc approach.

bill

Benjamin
Guest
 
Posts: n/a
#2: Dec 20 '06

re: Getting a function result while in heredoc



bill wrote:
Quote:
Thanks to those that taught me to use heredoc to embed html in
php. I don't want to start the html in php vs php in html
discussion again.,
>
heredoc works a treat for that which I am doing except:
In the generated html I have a number of checkboxes. I fill
in the checkboxes from a boolean value in a mySQL table. To make
this easier I use a function that takes the boolean and return
either 'checked' or ''. This worked well when I was
concatenating the html into a variable rather than using heredoc,
but it was really messy.
>
Short, simple question: is there anyway to get php to evaluate a
function while in a heredoc block ?
You can't do this with any string syntax.
Quote:
>
I know I could assign the results of using the function to
variables outside of the heredoc block or I could break the block
for the function, but the first is cumbersome and the second gets
rid of the clarity of the heredoc approach.
I'm afraid this is the only way. Maybe fill an array with the return of
functions?
Quote:
>
bill
Rik
Guest
 
Posts: n/a
#3: Dec 20 '06

re: Getting a function result while in heredoc


bill wrote:
Quote:
Thanks to those that taught me to use heredoc to embed html in
php. I don't want to start the html in php vs php in html
discussion again.,
>
heredoc works a treat for that which I am doing except:
In the generated html I have a number of checkboxes. I fill
in the checkboxes from a boolean value in a mySQL table. To make
this easier I use a function that takes the boolean and return
either 'checked' or ''. This worked well when I was
concatenating the html into a variable rather than using heredoc,
but it was really messy.
>
Short, simple question: is there anyway to get php to evaluate a
function while in a heredoc block ?
You'd be right back where you started from, php-snippets in HTML-blocks...
Quote:
I know I could assign the results of using the function to
variables outside of the heredoc block or I could break the block
for the function, but the first is cumbersome and the second gets
rid of the clarity of the heredoc approach.

Possible in heredoc:
- scalar variables
- scalar array-values
- scalar object-values

Not possible in heredoc:
- functions/results
- constants

The last is a great irritation.
--
Rik Wasmus


Toby Inkster
Guest
 
Posts: n/a
#4: Dec 20 '06

re: Getting a function result while in heredoc


Rik wrote:
Quote:
Not possible in heredoc:
- functions/results
- constants
>
The last is a great irritation.
<?php
foreach (get_defined_constants() as $k =$v)
$CONSTANTS[$k] = $v;

print "E_USER_ERROR is {$CONSTANTS['E_USER_ERROR']}.";
?>

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Rik
Guest
 
Posts: n/a
#5: Dec 20 '06

re: Getting a function result while in heredoc


Toby Inkster wrote:
Quote:
Rik wrote:
>
Quote:
>Not possible in heredoc:
>- functions/results
>- constants
>>
>The last is a great irritation.
>
<?php
foreach (get_defined_constants() as $k =$v)
$CONSTANTS[$k] = $v;
>
print "E_USER_ERROR is {$CONSTANTS['E_USER_ERROR']}.";
Well, yeah.
The reason for using constants imho is their deployability in functions,
and the fact they never change.
$CONSTANTS will not be a superglobal, so in every function where they have
to be used in such a syntax this would be deployed, or one has to make it
global/use $GLOBALS. Not really a solution, just another workaround. I'd
like to see "{E_USER_ERROR}" work. For now, it's usually either
concatenating small strings or temporarily assign it to a variable, elas.
--
Rik Wasmus


bill
Guest
 
Posts: n/a
#6: Dec 20 '06

re: Getting a function result while in heredoc


bill wrote:
Quote:
Thanks to those that taught me to use heredoc to embed html in php. I
don't want to start the html in php vs php in html discussion again.,
>
heredoc works a treat for that which I am doing except:
In the generated html I have a number of checkboxes. I fill in the
checkboxes from a boolean value in a mySQL table. To make this easier I
use a function that takes the boolean and return either 'checked' or
''. This worked well when I was concatenating the html into a variable
rather than using heredoc, but it was really messy.
>
Short, simple question: is there anyway to get php to evaluate a
function while in a heredoc block ?
>
I know I could assign the results of using the function to variables
outside of the heredoc block or I could break the block for the
function, but the first is cumbersome and the second gets rid of the
clarity of the heredoc approach.
>
bill

Thanks all, I was afraid there was not a simple way to do this.
bill
Jambalaya
Guest
 
Posts: n/a
#7: Dec 22 '06

re: Getting a function result while in heredoc


bill wrote:
[snip]
Quote:
Quote:
Short, simple question: is there anyway to get php to evaluate a
function while in a heredoc block ?
[snip]
Quote:
Rik replied:
[snip]
Quote:
Possible in heredoc:
- scalar variables
- scalar array-values
- scalar object-values
>
Not possible in heredoc:
- functions/results
- constants
>
The last is a great irritation.
[snip]

Indeed. But since you can use object values, you can overload a helper
class using the __get overload. So instead of something like:

echo <<<EOT
<input type="checkbox" name="$name"{get_checked_text($id)}>
EOT;

which unfortunately doesn't work, you could do something like:

echo <<<EOT
<input type="checkbox" name="$name"{$checked->$id}>
EOT;

This might be overkill for your needs but it has worked well for me in
the past. Good luck.

J

Closed Thread