Skip to main content
added 10 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

This code is from my Kohana project. How tocan I make it more beautiful? Should I use try-catch instead? How not to write exit() two timescould I avoid writing exit() twice?

public function action_index()
{
    $id = $this->request->query('id');
    if (!empty($id)) {
        $ticket = ORM::factory('ticket')
            ->where('id', '=', $id)
            ->find();
        if (!empty($ticket)) {
            $event = ORM::factory('event')
                ->where('id', '=', $ticket->event_id)
                ->find();
            if (!empty($event)) {
                $this->template->ticket = $ticket->id;
                $this->template->name = $ticket->name;
                $this->template->event = $event->title;
            } else {
                exit();
            }
        } else {
            exit();
        }
    }
}

This code is from my Kohana project. How to make it more beautiful? Should I use try-catch instead? How not to write exit() two times?

public function action_index()
{
    $id = $this->request->query('id');
    if (!empty($id)) {
        $ticket = ORM::factory('ticket')
            ->where('id', '=', $id)
            ->find();
        if (!empty($ticket)) {
            $event = ORM::factory('event')
                ->where('id', '=', $ticket->event_id)
                ->find();
            if (!empty($event)) {
                $this->template->ticket = $ticket->id;
                $this->template->name = $ticket->name;
                $this->template->event = $event->title;
            } else {
                exit();
            }
        } else {
            exit();
        }
    }
}

This code is from my Kohana project. How can I make it more beautiful? Should I use try-catch instead? How could I avoid writing exit() twice?

public function action_index()
{
    $id = $this->request->query('id');
    if (!empty($id)) {
        $ticket = ORM::factory('ticket')
            ->where('id', '=', $id)
            ->find();
        if (!empty($ticket)) {
            $event = ORM::factory('event')
                ->where('id', '=', $ticket->event_id)
                ->find();
            if (!empty($event)) {
                $this->template->ticket = $ticket->id;
                $this->template->name = $ticket->name;
                $this->template->event = $event->title;
            } else {
                exit();
            }
        } else {
            exit();
        }
    }
}
edited tags; edited title
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

What is best practice to write such Looking up the name and event for a PHP code with conditionsticket

edited title
Link

What is best practice to write such a PHP code with conditions

Source Link
Loading