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

Mick79's avatar

Can't see variable inside @php @endphp directive

I am inside a for loop and need to associate a value to a variable without outputting it on the screen. This is inside the loop

<p>Play Ended at --> {{$end = $p->updated_at->toTimeString()}}</p>
                                    @php $end = $p->updated_at->toTimeString() @endphp

the $p->updated_at->toTimeString() is recognised perfectly in the

tag but not inside the @php directive.

Can anyone offer any insight. My goal is attribute that value to $end without it displaying on the screen.

0 likes
9 replies
Mick79's avatar

@Sinnbeck Urgh... what happens is that I am a total asshole.

It's fine it does work. It's my brain that's the issue.

Cheers for replying to this inside a minute lol.

Mick79's avatar

@Sinnbeck My IDE was throwing a red flag at it so I just went into "something is wrong" mode. It executes as I wanted it to.

Idiot.

Sinnbeck's avatar

@Mick79 hehe yeah it probably don't like it. Personally I would look into moving it out of the view as well (or at least inside a helper directive or similar)

Mick79's avatar

@Sinnbeck I'd like to do that too. I hate using @php inside blades. To me it's an indicator that you're not doing things right. In this instance I can't see any other way.

Sinnbeck's avatar

@Mick79 I'm sure there is a way. Why do you need to set it as a variable? Can't you just use an accessor on the model?

Mick79's avatar

@Sinnbeck You might be going above my pay grade there...

The reason I'm doing it as I am is because I'm taking data that's being captured in the browser by user interactions. I still don't know if that means I can't do it the way you've suggested haha.

btw I was browsing around for solutions this morning and stumbled upon your blog. Very cool. Bookmarked.

Sinnbeck's avatar

@Mick79 thanks! Maybe show the whole code block and we can try and see if it's possible to clean it up a bit

Please or to participate in this conversation.