Home > Enterprise >  Why can't I execute some trivial PHP code in my Laravel project?
Why can't I execute some trivial PHP code in my Laravel project?

Time:01-27

I am working toward debugging a Laravel 9 app that I am building to get a better understanding of how the edit() and update() methods of my controller work but I can't find anything that describes how to invoke XDebug in VS Code for a Laravel project so I'm trying to work through a video that shows how to do ordinary PHP code in VS Code but that's not working either.

In fact, I can't even get my trivial webpage with trivial PHP to display properly in my web browser. I don't know if I'm simply having a Stupid Day or if there is some step I need to take to make my browser interpret PHP properly instead of treating it all like a big comment.

Here's the webpage with its PHP code that I am trying to display in my browser:

<!DOCTYPE html>
<html lang="en">
    <body>
        <h1>My Glorious Title</h1>
        <?php
            echo "Hello world!";
        ?>
        <?php 
            $text = "Debugging fun with Laravel and PHP";
            echo $text;
            for ($ix=0; $ix<5; $ix  ) {
                print( $ix);
            }
            echo "  ";
        ?>
    </body>
</html>

And this is what the browser (Firefox) sees when I do CTRL-SHIFT-i:

<!DOCTYPE html>
<html lang="en">
    <body>
        <h1>My Title</h1>
        <!--?php echo "Hello world!"; ?-->
        <!--?php 
            $text = "Debugging fun with Laravel and PHP";
            echo $text;
            for ($ix=0; $ix<5; $ix  ) {
                print( $ix);
            }
            echo "  ";
        ?-->
    </body>
</html>

The webpage itself displays only the h1 tag since all the PHP has been commented out.

The PHP code is clearly not being recognized as such but I don't understand why since my Laravel project is full of PHP and works fine.

My file, test01.php, is sitting in the root directory for my project. I'm running on Windows 10 home using an Administrator ID so I can't see this being a security issue. How do I make my browser recognize this as actual PHP code that it should execute?

UPDATE:

I amended my source code so that it was exactly this:

<html lang="en">
    <body>
        <h1>My Title</h1>
        @php "Hello world!"; @endphp

        <?php 
            $text = "Debugging fun with Laravel and PHP";
            echo $text;
            for ($ix=0; $ix<5; $ix  ) {
                print( $ix);
            }
            echo "  ";
        ?>
    </body>
</html>

And this is what I got on my page:

My Title
@php "Hello world!"; @endphp 

When I tried using "double-moustache" syntax around the first PHP block like this:

<!DOCTYPE html>
<html lang="en">
    <body>
        <h1>My Title</h1>
        {{"Hello world!";}} 

        <?php 
            $text = "Debugging fun with Laravel and PHP";
            echo $text;
            for ($ix=0; $ix<5; $ix  ) {
                print( $ix);
            }
            echo "  ";
        ?>
    </body>
</html>

I got this on my webpage:

My Title
{{"Hello world!";}} 

The things I put around my PHP code to delineate it as PHP code are simply not being recognized.

CodePudding user response:

Laravel is including blade template for view

You can use blade commands such as @php @endphp, {{ }}, {!! !!}} and so on.

Please refer to the URL below for details.

https://laravel.com/docs/9.x/blade

CodePudding user response:

You're probably trying to use PHP in a blade template. This is possible, but you'll need to wrap your PHP in @php and @endphp:

@php
    $i = 0;
    $i  ;
    echo $i;
@endphp

Check the docs here

CodePudding user response:

I think that your HTML file did not get parsed in Server. In other words, if you run your html file in a local server or online server, PHP part are parsed in the server and only pure HTML, CSS and Javascript codes are sent to the client.

  • Related