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

UngaBunga's avatar

Using PHP goto Labels for code folding

Hi,

Lately, I've been using PHP labels in my Controllers for code folding. PHP labels are actually used with goto statements, but I'm using labels only because it's so much easier to fold my code in different IDEs using labels + curly braces.

Here's an example.

<?php

namespace App\Http\Controllers;

use App\Cart;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CartController extends Controller
{
    public function create(Request $request)
    {
        $cart = new Cart;

        SettingCartFields: {
            $cart->field1 = $request->field1;
            $cart->field2 = $request->field2;
            $cart->field3 = $request->field3;
            $cart->field4 = $request->field4;
            $cart->field5 = $request->field5;
            $cart->field6 = $request->field6;
        }

        $cart->save();
    }
}

I have a few questions, are there any performance issues with this? Am I adding some kind of overhead by introducing a lot of labels in my code? Is there any better way to do this?

0 likes
7 replies
Tray2's avatar

I would not use that at all since it clutters the code and the use of extra unnecessary code blocks makes it harder to read.

1 like
Talinon's avatar

If code folding is a concern within a controller, it's likely an indicator that your controller is too fat. Refactor the bloat out of your controller instead of muddying it up with labels.

2 likes

Please or to participate in this conversation.