Reputation: 25
I have some PHP code mixed with HTML and i need to store this into a variable but i can't figure out how can i do that. When this code is executed, is called from a shortcode that calls the function and works perfectly.The PHP code render the HTML and shows the information from the DB, but now i need to store the result (this html divs boxes with information from the database)
into a variable in order to send this variable to a function in wordpress that can update a page. When i try it, there is a error that say echo isnt spected. I know that this variable doesn't spect php code inside, so how can i do that? store the result into a variable?
any suggestion will be very apreciated thank you in advance:
$ultimosProyectosHome+="
echo '<div class="cont-proyectos">';
echo '<div style="text-align:center;"><a href="'. $url_actual .'"? page_id="'. $id_paginaFicha .'" target="_blank">';if($pregunta6Valor!=""){echo '<img src="http://www.xxx.xx/xxx/xxx/'.$pregunta6Valor.'" alt="Cover"></a></div>';} else{echo '<img src="'.$imgDesafio.'" alt="Cover"></a></div>';}
echo '<h3>';if($pregunta2Valor != ""){ echo $pregunta2Valor;}else{echo $Nodefinido;}echo'</h3>';
echo'<a href="'. $url_actual .'"?page_id="'. $id_paginaFicha .'" target="_blank">';
echo'<h4>';if($pregunta1Valor != ""){ echo $pregunta1Valor;}else{echo $Nodefinido;} echo'</h4>';
echo'</a>';
echo'<p>'; if($pregunta3Valor != ""){ echo getSubString($pregunta3Valor,198,$id_paginaFicha);}else{echo $Nodefinido;} echo'</p>';
echo'<div class="redesSocialesTLP" id="redes">';
echo'<a href="https://twitter.com/intent/tweet?url="'. $url_client .'"?page_id="'. $id_paginaFicha .'"&text="'. $pregunta1Valor .'" target="_blank">';
echo'<i class="fa fa-twitter fa-2x"></i>';
echo'</a>';
echo'<a href="https://www.facebook.com/sharer/sharer.php?u="'. $url_client .'"?page_id="'. $id_paginaFicha .'" target="_blank">';
echo'<i class="fa fa-facebook fa-2x"></i>';
echo'</a>';
echo'<a href="https://plus.google.com/share?url="'. $url_client .'"?page_id="'. $id_paginaFicha .'" target="_blank">';
echo'<i class="fa fa-google-plus fa-2x"></i>';
echo'</a>';
echo'</div>';
echo'</div>';
";
Upvotes: 1
Views: 3117
Reputation: 57418
Your code may benefit from the answer below but you're doing a different, and more dangerous, thing. You've a blob of HTML and PHP and want to execute it to store its results. To do this, you need something like eval()
. Or equivalently, you can write the code to a temporary PHP file with unguessable name, then re-include it in your script while wrapping it in output buffering.
I've seen it done using a "sandbox" server with limited rights, but that's complicated to implement.
The danger depends on where your code comes from. If someone else can modify it, then he will be able to execute arbitrary code on your server, which you definitely want to avoid.
For this reason some templating languages have been developed - you may want to look into Twig, for example.
Otherwise, you can now do:
$bunch = "<BUNCH OF MIXED CODE FROM ABOVE>";
ob_start();
try {
eval($bunch);
} catch (\Exception $err) {
print "There has been an error.";
}
$ultimosProyectosHome += ob_get_clean();
The correct way:
$html = '';
...
// Remove all occurrences of this
echo "Something";
// and this
echo "<br />";
// and write it like this instead.
$html .= 'Something';
$html .= '<br />';
This can become awkward if you have functions that produce output. You'll have to rewrite them to return the output (just like above, end with
return $html;
and then wherever you had
callToFunction(parameters);
you'll refactor with
echo callToFunction(parameters);
Then, further refactor it to read
$html .= callToFunction(parameters)
iteratively.
The lazy way
Using output buffering. A great way to hog memory, lose control of your script, and play merry hell whenever you'll want to debug it (unless you go with Xdebug, but then why do the lazy thing in the first place?).
ob_start();
...
$html = ob_get_clean();
Upvotes: 3
Reputation: 3534
What you're looking for is output buffering.
Call ob_start();
before echo'ing your content, then at the end:
$content = ob_get_clean();
Everything that was output between those two functions will then be stored in $content
.
There's further information on these functions - and other options as well - in the PHP manual.
Keep in mind output buffering should generally be a last resort - don't get too comfortable with doing this everywhere!
By the way, you can avoid all the calls to echo
by just ending the PHP block (?>
and starting it again when you're done (<?php
).
Upvotes: 1