Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lam784's avatar

Render php from string

Hello,

Is there a way to render php inside a string?

So I got an article from the controller, and in $article->content, it has php code in it, then on the view, I use {!! $article->content !!} to output the article content, but the php code in $article->content got commented out:

FROM: TO: <--?php $question = "example"; ?>

I am on 5.2

0 likes
5 replies
KamalKhan's avatar

You should never allow this as it is a security risk. Raw PHP code should not go into the database. There is always a way to avoid this. Depends on what your goal is. {!! ... !!} is for raw html output and that too after sanitization or if you trust the content.

Snapey's avatar

This is a really bad idea. Have a rethink.

lam784's avatar

@Snapey I know this is not the way to do it, but some articles in the database are ancient, and they all have questions and answers built in them using php, and if I were to extract them, it is going to take forever.

@michaelnguyen547 The code would like something like

//<?php $questions = array('something');

//foreach ($questions as $question){

echo "<p>$question</p>";

} //?>

Some html code

Ignore the //

Please or to participate in this conversation.