AvinashK
AvinashK

Reputation: 3423

heredoc format and its implementation

i wrote the following code...

<?php

$conn=mysql_connect("localhost","","");
if($conn)
echo "connection established";

mysql_select_db("moviesite");

$que="select * from movie";
$result=mysql_query($que,$conn);
?>

<html>
<title>movie database</title>
<body>
<table border=1 align=center>
<th colspan=2>
PEOPLE<a href='movie.php?action=add & id='>[ADD]</a>
</th>

<?php
$ta_row=<<<eod
    <tr>
    <td>
    $mov_name
    </td>
    <td>
    <a href='movie.php?action=edit & id=$mov_id'>[EDIT]</a>
    <a href='movie.php?action=delete & id=$mov_id'>[DELETE]</a>
    <td>
    </tr>
eod;

while($row=mysql_fetch_array($result))
{
$mov_id=$row["id"];
$mov_name=$row["name"];
echo $ta_row;
}
?>

now the above code didnt print the name or the id of the movie(the fields were blank)...so i cut and paste $ta_row inside the while loop after the definition of $mov_id and $mov_name...this sorted out all problems...but my question is why did the code fail earlier...as it is i was outputting ta_row after defining $mov_id and $mov_name so if $ta_row is just a string its content variables should have got changed...why didnt they change?

Upvotes: 0

Views: 465

Answers (2)

jprofitt
jprofitt

Reputation: 10974

It didn't work when you had it before the loop because it was trying to use the value of the variables $mov_id and $mov_name as they existed right then. Since they weren't defined at that point in the code, there was nothing to add in there. After that, $ta_row was simply a variable that contained a string, and only a string -- no variables, of the HTML.

If you'd like to move it out of the loop, use some placeholders like ##MOV_ID## and ##MOV_NAME## then you can just

echo str_replace(array('##MOV_ID##', '##MOV_NAME##', array($mov_id, $mov_name), $ta_row);

to get the desired result

Upvotes: 0

DaveRandom
DaveRandom

Reputation: 88657

Because a heredoc is just like any other string - it's value is fixed when you declare it. If the variables $mov_id and $mov_name weren't set when you declared $ta_row, you can't use them in $ta_row.

If you want to do something like you tried to do in the first place, you would have to use something like str_replace() or sprintf() - replace place-holders in the original string with the actual values you want to display.

Consider this:

$myVar = "Some string containing a $var";
$var = 'Variable';
echo $myVar;
// Outputs: Some string containing a 

Now this:

$var = 'Variable';
$myVar = "Some string containing a $var";
echo $myVar;
// Outputs: Some string containing a Variable

...Heredocs behave exactly like a double-quoted string (from this point of view, at least).

Alternatively you could do this:

$myVar = 'Some string containing a $var';
$var = 'Variable';
echo $myVar;
// Outputs: Some string containing a $var
echo str_replace('$var',$var,$myVar);
// Outputs: Some string containing a Variable

...because $myVar is now single-quoted, $var appears in it literally. Then when you run str_replace(), it is replaced with the value of $var, instead of the literal string.

It's probably worth you re-reading this.

Upvotes: 3

Related Questions