Home > OS >  I want to output PHP code from Blade in Laravel. In addition, we would like to output the result of
I want to output PHP code from Blade in Laravel. In addition, we would like to output the result of

Time:10-21

I'm new to PHP.

I am currently creating an App in Laravel. When I write echo in the php directive of Blade and pass the php code as a string as an argument, the contents of the php code is output to HTML as it is. What I want to do is to have the HTML output as the result of the execution of the php code written in the argument of the echo.

In a simple way, I can put a judgment in the php directive of Blade and divide it into two branches: one that outputs the contents of the echo argument as it is, and another that outputs the result of the execution of the php code. For example, changing the URL and switching between the above two results is not a problem. It's a bit of a roundabout way of doing things, but I'm doing it because I need the two results above and I don't want to affect the logic of the one that outputs the contents of the echo argument as it is passed.

What I came up with is to prepare a separate App in Laravel, get the HTML output of the contents passed to the echo argument in the separate App, execute the PHP code, and return it as HTML. However, I am not very knowledgeable about infrastructure and have no clue if this is possible or not.

Can you please give me some wisdom? If I didn't understand your question, please forget it.

Thank you.

jpg file of the image


Postscript. Thank you kind-hearted people.

Indeed {{ echo '<? php echo date("Y-m-d"); ? >'; }} also has a way to write it, which I had forgotten. (I also modified the jpg file of the image) The actual code also has echo that spans multiple lines, and I think I wrote this question in confusion.

I was using {{ echo '<? php echo date("Y-m-d"); ? >'; }} of the code and I want the output of the result. I understand that the background behind wanting this is complex and I am talking about something awesomely weird.

Thank you.

CodePudding user response:

If you just need to echo something in the blade go with {{}}. It executes the PHP code and outputs the result. no need to wrap the code in PHP directives or echo function.

For example, in your blade:

{{ date("Y-m-d") }}

Using PHP directive (@php) is helpful when there are a couple of lines that need to process and you can't place that logic in your controller.

@php
  // some multiple PHP codes here
@endphp

Please visit the doc page for more.

CodePudding user response:

You can show your code by using pre & code tag

 <pre>
      <code>
        // Here is your code 
     
        $number1 = 5;
        $number2 = 5;
        echo $result = 'result = ' . $number1   $number2;
    
      </code>
  </pre>

And you can output the result like this -

@php

  $number1 = 5;
  $number2 = 5;
  echo $result = 'result = ' . $number1   $number2;

@endphp

Output: result = 10

CodePudding user response:

{{ date("Y-m-d") }}

I guess this should work.

  • Related