473,498 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting a function result while in heredoc

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
Dec 19 '06 #1
6 6229

bill wrote:
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.
>
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?
>
bill
Dec 20 '06 #2
Rik
bill wrote:
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...
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
Dec 20 '06 #3
Rik wrote:
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

Dec 20 '06 #4
Rik
Toby Inkster wrote:
Rik wrote:
>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
Dec 20 '06 #5
bill wrote:
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
Dec 20 '06 #6
bill wrote:
[snip]
Short, simple question: is there anyway to get php to evaluate a
function while in a heredoc block ?
[snip]
Rik replied:
[snip]
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

Dec 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2550
by: James | last post by:
Please help - getting very desperate! Sun, 12 October 2003 05:39 I have PHPDEV 4.2.3 from Firepages.com.au as the upgrade to 4.3.0 did not work. I also had an abortive download from PHP.NET as...
7
11427
by: flupke | last post by:
Hi, i have php script where i use a heredoc type of variabele to print out some html. In the heredoc, i use the values of several other vars. php snippet: $div=<<<END_DIV <DIV...
3
2067
by: tshad | last post by:
I have a page that I am getting a username and password as a random number (2 letters, one number and 4 more letters) I have 2 functions I call: *************************************************...
2
2134
by: Matt F | last post by:
I can't seem to get heredoc to populate correctly with variables through a form. <textarea name="Template" rows="10" cols="80">Template Here</textarea> Contents could be something like: I want...
0
1006
by: Heshan Suri | last post by:
Hi, I have written a the following python code to analyse a function or a method. I am having a script which is having a python function (add) and a class (MyClass) with a method (multiply).When...
7
5529
by: Jeff | last post by:
I have this: $content = <<<TD $D TD; that fails silently and stops php even though errors are turned on
2
1638
by: Adam Cantrell | last post by:
How could I count the number of newlines in a heredoc variable? This outputs 0, but I thought heredoc preserved newlines? <? $test = <<<END <a href="test5.php">adam</a> <p> Hi There </p>...
2
1372
by: someusernamehere | last post by:
hi, I have some heredoc on this way: $foo = <<<bar <form action="index.php" method="POST" name="user"> ............................................................................. HTML code...
2
2300
by: ziycon | last post by:
I have a site that has been using the HEREDOC syntax for awhile, after a recent upgrade to the site a lot of pages are erroring with the below error, i can't figure it out at ll, any ideas? All the...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4590
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.