Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

CodeIgniter 4 view shows Undefined variable

ErrorUndefined variable $title

Short answer

In CodeIgniter 4 a view only sees the keys of the array you pass as the second argument to view(). Each key becomes one variable. The variable is undefined when the key is missing or spelled differently, or when saveData is off and a later view() call no longer holds earlier data.

  • Applies to CodeIgniter 4.5, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Compare the keys in the controller's data array with the variable name in the view file. They must match exactly.

    grep -n "view(" app/Controllers/*.php
  2. 2Match what you see, then jump to its fix

    1. 1Undefined variable $title while the controller sets a similar keyThe key is missing from the data array, or spelled differentlyFix
    2. 2The variable works in the first view() call but not in a later oneEarlier view data is cleared by saveDataFix

What does the error look like?

  • Undefined variable $title

Cause 1 of 2

The key is missing from the data array, or spelled differently

1Check

Each array key becomes a variable of the same name inside the view. A key called page_title gives $page_title, not $title.

grep -rn "\$title" app/Views/

2Fix

Pass the data as the second argument and use the same name in the view.

// controller
return view('blog_view', ['title' => 'My title']);

// app/Views/blog_view.php
<h1><?= esc($title) ?></h1>

3Verify

Reload the page; the heading shows the title.

curl -s http://localhost:8080/blog | grep "<h1>"

Confirmed by codeigniter.com

Did this fix it?

Cause 2 of 2

Earlier view data is cleared by saveData

1Check

By default, data passed to view() is kept for later calls in the same request. When saveData is false, either per call or in app/Config/Views.php, each call sees only its own array.

grep -rn "saveData" app/

2Fix

Pass the data again to each call that needs it, or leave saveData at its default.

return view('header', $data) . view('content', $data);

3Verify

Both parts of the page show the variable.

curl -s http://localhost:8080/blog | grep -c "My title"

Confirmed by codeigniter.com

Did this fix it?

How do I stop it coming back?

  • Pass one $data array to every view that is part of the same page.
  • Use the same key names in the controller and the view.
  • Run PHP 8 in development: an undefined variable is reported as a warning there.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.