CodeIgniter 4 view shows Undefined variable
Undefined variable $titleShort 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.
Fix it fast
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/*.php2Match what you see, then jump to its fix
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
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
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix