/    Sign up×
Community /Pin to ProfileBookmark

call a function from inside heredoc?

Hi,

i have this code

[code=php]$fn = ‘testfunction’;
function testfunction($argument) { whatever; }
$string = <<< heredoc
plain text and now a function: {$fn(‘someargument’)}
heredoc;[/code]

as far as i know, that should make it but it wont work, i get the following error:

[QUOTE]

Parse error: syntax error, unexpected ‘(‘, expecting ‘}’ in /home3/tokyoint/public_html/asakusa.htm on line 82

[/QUOTE]

why is that? what am i doing wrong? its possible to have a function executed inside heredoc, isnt?

Thank you.

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 01.2011 — I do not believe you can do that, just as you cannot call a function within a double-quoted string (you have to concatenate, instead). A couple alternatives are:
[code=php]
$result = testfunction('someargument');
$string = <<<EOD
plain text and now a function result: $result
some more text
EOD;
[/code]

...or...
[code=php]
$string = sprintf(<<<EOD
plain text and now a function result: %s
some more text
EOD
,
testfunction('someargument')
);
[/code]
Copy linkTweet thisAlerts:
@supercainauthorJun 01.2011 — Really? i read somewhere it was possible from PHP 5.0 on, but i really didnt get how to do it. I had already considered the first alternative you gave me, the problem is that i have to call the function many times on the same page always by using a different argument so it would take too much extra work to declare a variable every time that occurs. This is the full function:

[code=php]function getstatus($room)
{
$query = mysql_query("SELECT status FROM rooms WHERE room='$room'");
$stat = mysql_fetch_array($query);
if ($stat[0] == 1)
{
$status ="<B>Available!</B>";
}
else
{
$status ="occupied";
}
return $status;
}[/code]


if i could somehow have the function give me the variables i could use them directly in the heredoc without having to call the function itself, something like $stat[0], $stat[1], $stat[2], etc. one variable for each record of the "status" column but i really cant figure out how to do it. Any suggestions please?

Thank you.
Copy linkTweet thisAlerts:
@NogDogJun 01.2011 — Something like this?
[code=php]
<?php
function getStatusAll()
{
$status = array();
$query = mysql_query("SELECT room, status FROM rooms");
while ($row = mysql_fetch_assoc($query)) {
$status[$row['room']] = ($row['status'] == 1)
? "<B>Available!</B>"
: "occupied";
}
return $status;
}

$availability = getStatusAll();
foreach($availability as $room => $status) {
echo <<<EOD
Blah blah blah.
Room: $room
Blah blah blah
Availability: $status
Blah blah blah.
EOD;

}
[/code]
Copy linkTweet thisAlerts:
@supercainauthorJun 02.2011 — Thats pretty interesting, something like that is what i need, i was just wondering if it was possible to define all the php before getting to the echo part since there is so much html code in between the variables, the code will look something like this:

[code=html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD><?php $lang="en"; ?>
[...a lot of html code...]

<TD class="roomtable">201</TD>
<TD class="roomtable">Middle size</TD>
<TD class="roomtable">59,000</TD>
<TD class="roomtable">$status_room201</TD>

[...more html code...]

<TD class="roomtable">202</TD>
<TD class="roomtable">big size</TD>
<TD class="roomtable">59,000</TD>
<TD class="roomtable">$status_room202</TD>

[...and so on...][/code]


so as you see it would be more practical to define first the php and then print out all the html code altogether, besides they are on separate pages, one with all the php code (functions, variables, etc.) and then a bunch of pages with html code like the one above. Is it possible to do something like that?

Thank you.
Copy linkTweet thisAlerts:
@NogDogJun 02.2011 — I don't know if this helps any or if we're each visualizing something else, but there's no reason to echo chunks of static HTML within PHP. Only go into PHP mode when you need to output something that is [i]not[/i] static.
[code=php]
<?php
$status = array();
$query = mysql_query("SELECT room, status FROM rooms");
while ($row = mysql_fetch_assoc($query)) {
$status[$row['room']] = ($row['status'] == 1)
? "<B>Available!</B>"
: "occupied";
}
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>Example</title></head>
<body>
<p>This is a bunch of static HTML text.</p>
<p>Now we're about to the point where we need the status for room #123.</p>
<p>The status for Room #123 is: <b><?php echo $status[123]; ?></b>.</p>
<p>This is a bunch of static HTML text.</p>
<p>Now we're about to the point where we need the status for room #456.</p>
<p>The status for Room #456 is: <b><?php echo $status[456]; ?></b>.</p>
<p>Okay, that's enough boring stuff for now.</p>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@supercainauthorJun 02.2011 — Im using heredoc because it comes in pretty handy when using templates, but regardless of that im just looking for a way to do something like this:

$status[0] = give me the first record in field "status"

$status[1] = give me the second record in field "status"

$status[2] = give me the 3rd record in field "status" and so on...

this way i can insert these variables whenever i need them into any place of the html, regardless whether i use heredoc or <?php echo $status[0]; ?>

The last code you posted really seems to be able to do that, does it?

Thanks again.
Copy linkTweet thisAlerts:
@supercainauthorJun 02.2011 — I tested the code now and it works beautifully!! thank you! i just would like to ask you a few things so i can understand better how you did it. How does mysql_fetch_assoc work? as far as i understand, it associates the 2 fields, but i got lost after that.

$status[$row['room']] = ($row['status'] shouldnt mean that both values are the same? also, i didnt know you could use '?' to replace the 'if' statement and ':' to replace 'else'. That was very informative, i really appreciate it.
Copy linkTweet thisAlerts:
@NogDogJun 02.2011 — I tested the code now and it works beautifully!! thank you! i just would like to ask you a few things so i can understand better how you did it. How does mysql_fetch_assoc work? as far as i understand, it associates the 2 fields, but i got lost after that.

$status[$row['room']] = ($row['status'] shouldnt mean that both values are the same? also, i didnt know you could use '?' to replace the 'if' statement and ':' to replace 'else'. That was very informative, i really appreciate it.[/QUOTE]


mysql_fetch_assoc() returns an array of values for one result row, where the array keys will be the names of the fields (columns) that were selected (i.e.: it's an [b]assoc[/b]iative array). So when we build the $status array, we're using the value of the "room" column ($row['room']) as the array key for $status while the value will be the phrase determined by whether or not $row['status'] is 1 or not (via the ternary "? :" operator).

It may get a little confusing when you first look at it to realize that we're using an array variable as the key for another array:
[code=php]
$row['room'] = 123;
$row['status'] = 1;
$status[$row['room']] = ($row['status'] == 1) ? 'Available' : 'Occupied';
[/code]
...is in effect doing...
[code=php]
$status[123] = 'Available';
[/code]
Copy linkTweet thisAlerts:
@supercainauthorJun 03.2011 — Thank you for your explanation, it was very informative. Now in order to understand it even better lets say we want to have an elseif in there. How would you write it in that format? for example, since

[code=php]$status[$row['room']] = )($row['status'] == 1 ? 'Available' : 'Occupied'; [/code]

more or less equals this:

[code=php]if($row['status'] == 1) { $status='Available' } else { $status='Occupied'}; [/code]

how would you write this:

[code=php]
if($row['status'] == 1) { $status='Available' } elseif ($row['status'] == 2) { $status='reserved'} elseif ($row['status'] == 3) { $status='whatever'} else { $status='Occupied'};
[/code]


sorry if im being to ambitious, you already gave me the answer i was looking for but i would like to get to understand it as much as possible.

Thank you.
Copy linkTweet thisAlerts:
@NogDogJun 03.2011 — I'd probably go with a switch() then, rather than a trying to do a one-liner.
[code=php]
while($row = mysql_fetch_assoc($result)) {
switch($row['status']) {
case 1:
$text = 'available';
break;
case 2:
$text = 'reserved';
break;
case 3:
$text = 'whatever';
break;
default:
$text = 'occupied';
}
$status[$row['room']] = $text;
}
[/code]

Now, what I might [I]really[/I] do is create a separate table in the DB that contains those status words, and add a JOIN to the original query to grab that word based on the status value, and avoid all this stuff. ?
Copy linkTweet thisAlerts:
@supercainauthorJun 03.2011 — That was a very nice and clean solution, i think i will use it next time i need to do something like that.

Thank you for all your help.
×

Success!

Help @supercain spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...