If you have this error then you must to call to session_start in the begining (at the top) of each php file where you want to use sessions
Recent Posts
Showing posts with label undefined. Show all posts
Showing posts with label undefined. Show all posts
How to solve: Notice: Use of undefined constant XXX - assumed 'XXX'
This notice occurs when a token is used in the code and appears to be a constant, but a constant by that name is not defined.
One of the most common causes of this notice is failure to quote a string used as an associative array key.
For example:
// Wrong
echo $array[key];
// Right
echo $array['key'];
Another common causes is a missing
$
(dollar) sign in front of a variable name:// Wrong
echo varName;
// Right
echo $varName;
It can also be a sign that a needed PHP extension or library is missing when you try to access a constant defined by that library.
How to solve: Notice: Undefined variable
Happens when you try to use a variable that wasn't previously defined.
A typical example would be
foreach ($items as $item) {
// do something with item
$counter++;
}
If you didn't define
$counter
before, the code above will trigger the notice.
The correct way would be to set the variable before using it, even if it's just an empty string like
$counter = 0;
foreach ($items as $item) {
// do something with item
$counter++;
}
How to solve: Fatal error: Call to undefined function
This happens when you try to call a function that is not defined yet. Common causes include missing extensions and includes, conditional function declaration, function in function declaration or simple typos.
Example 1 - Conditional Function Declaration
$someCondition = false;
if ($someCondition === true) {
function fn() {
return 1;
}
}
echo fn(); // triggers error
In this case,
fn()
will never be declared because $someCondition
is not true.
Example 2 - Function in Function Declaration
function createFn()
{
function fn() {
return 1;
}
}
echo fn(); // triggers error
In this case,
fn
will only be declared once createFn()
gets called. Note that subsequent calls to createFn()
will trigger an error about Redeclaration of an Existing function.
In case of a missing extension, install that extension and enable it in php.ini. Refer to the Installation Instructions in the PHP Manual for the extension your function appears in.
In case of missing includes, make sure to include the file declaring the function before calling the function.
In case of typos, fix the typo.
How to solve: Notice: Undefined Index
Happens when you try to access an array by a key that does not exist in the array.
A typical example for an
Undefined Index
notice would be (demo)$data = array('foo' => '42', 'bar');
echo $data['spinach'];
echo $data[1];
The solution is to make sure the index or offset does exist, either by using
array_key_exists
or isset
prior to accessing that index.$data = array('foo' => '42', 'bar');
if (array_key_exists('spinach', $data)) {
echo $data['spinach'];
}
else {
echo 'No key spinach in array';
}
Subscribe to:
Posts (Atom)