skip to Main Content

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.

4

Answers


  1. Chosen as BEST ANSWER

    Thank you all for taking the time to answer these strange questions.
    I have decided to look at the following as a solution.
    I referred to Laravel: how to create a rendered view from a string instead of a blade file?.Thank you.
    I don't know if I'm on the right track...

    // `<? php echo date("Y-m-d"); ? >` get the HTML of as a string and pass it as $html
    return IlluminateSupportFacadesBlade::render($html);
    

    If you have any problems with this solution, please let me know.


  2. 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.

    Login or Signup to reply.
  3. {{ date("Y-m-d") }}

    I guess this should work.

    Login or Signup to reply.
  4. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search