skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    Apparently, I was indeed having a "Stupid Day" yesterday and I have now got my PHP code to execute properly by following some of the advice in various answers.

    Here's what I did:

    • I deleted the first php block, the one where I tried using @php and then @endphp and then {{ }}; the second php block was unchanged and still used <?php and ?>.

    • I renamed the file so that it is test01.blade.php instead of test01.php.

    • I moved the file from the root directory of my project to the views folder.

    • I created this route:

      Route::get('/test01', function () { return view('test01'); });

    • I executed npm run dev in a terminal window.

    • I executed php artisan serve in another terminal window.

    • I went to localhost:8000/test01 and my file is now executing properly.

    Thanks to all responders for helping me get back on track. Now I just have to figure out why I can't get Debug to run yet on my little play file but I'll create a fresh question if I can't figure it out myself.


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

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