Why php does not show undefined index error if it is not an array? [duplicate]
This question already has an answer here:
Why does treating integers as arrays ($int[$index]) not raise any errors in PHP? [duplicate]
3 answers
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$array = ;
$var = 0;
echo $array['foo'];// shows error
echo $var['bar'];//does not show error !
(demo)
This code produces
Notice: Undefined index: foo in /in/WfqtZ on line 10
I'm sorry if this sounds ridicules, but why PHP does not show "Undefined index: bar" in this code ? bar
is obviously an undefined index, what makes PHP think that it is defined ?
php
marked as duplicate by Community♦ Jan 2 at 18:50
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why does treating integers as arrays ($int[$index]) not raise any errors in PHP? [duplicate]
3 answers
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$array = ;
$var = 0;
echo $array['foo'];// shows error
echo $var['bar'];//does not show error !
(demo)
This code produces
Notice: Undefined index: foo in /in/WfqtZ on line 10
I'm sorry if this sounds ridicules, but why PHP does not show "Undefined index: bar" in this code ? bar
is obviously an undefined index, what makes PHP think that it is defined ?
php
marked as duplicate by Community♦ Jan 2 at 18:50
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Because it's PHP?
– Robert Harvey♦
Jan 2 at 18:05
1
@RobertHarvey I'm sorry I can't get it. Do you mean this is an expected behavior, however it is bad and should be removed ?
– Accountant م
Jan 2 at 18:10
1
Run this first to check if it is an array before indexing it: php.net/manual/en/function.is-array.php. Or, y'know, just write your code correctly.
– Robert Harvey♦
Jan 2 at 18:13
2
@RobertHarvey The question is about why. If I wrote$users = ; $user = 0;
and then made a typo in referencing $user instead of $users, like this:echo $user['foo'];
, I would want to know why I'm not getting a notice message about it. I don't think it's feasible to run a variable through is_array() every time you want to reference it.
– RToyo
Jan 2 at 18:17
I know what the question is about. I stopped worrying about stuff like this a long time ago. As a result, I've managed to maintain at least a portion of my sanity.
– Robert Harvey♦
Jan 2 at 18:18
add a comment |
This question already has an answer here:
Why does treating integers as arrays ($int[$index]) not raise any errors in PHP? [duplicate]
3 answers
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$array = ;
$var = 0;
echo $array['foo'];// shows error
echo $var['bar'];//does not show error !
(demo)
This code produces
Notice: Undefined index: foo in /in/WfqtZ on line 10
I'm sorry if this sounds ridicules, but why PHP does not show "Undefined index: bar" in this code ? bar
is obviously an undefined index, what makes PHP think that it is defined ?
php
This question already has an answer here:
Why does treating integers as arrays ($int[$index]) not raise any errors in PHP? [duplicate]
3 answers
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$array = ;
$var = 0;
echo $array['foo'];// shows error
echo $var['bar'];//does not show error !
(demo)
This code produces
Notice: Undefined index: foo in /in/WfqtZ on line 10
I'm sorry if this sounds ridicules, but why PHP does not show "Undefined index: bar" in this code ? bar
is obviously an undefined index, what makes PHP think that it is defined ?
This question already has an answer here:
Why does treating integers as arrays ($int[$index]) not raise any errors in PHP? [duplicate]
3 answers
php
php
asked Jan 2 at 18:04
Accountant مAccountant م
1,93621330
1,93621330
marked as duplicate by Community♦ Jan 2 at 18:50
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Community♦ Jan 2 at 18:50
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Because it's PHP?
– Robert Harvey♦
Jan 2 at 18:05
1
@RobertHarvey I'm sorry I can't get it. Do you mean this is an expected behavior, however it is bad and should be removed ?
– Accountant م
Jan 2 at 18:10
1
Run this first to check if it is an array before indexing it: php.net/manual/en/function.is-array.php. Or, y'know, just write your code correctly.
– Robert Harvey♦
Jan 2 at 18:13
2
@RobertHarvey The question is about why. If I wrote$users = ; $user = 0;
and then made a typo in referencing $user instead of $users, like this:echo $user['foo'];
, I would want to know why I'm not getting a notice message about it. I don't think it's feasible to run a variable through is_array() every time you want to reference it.
– RToyo
Jan 2 at 18:17
I know what the question is about. I stopped worrying about stuff like this a long time ago. As a result, I've managed to maintain at least a portion of my sanity.
– Robert Harvey♦
Jan 2 at 18:18
add a comment |
1
Because it's PHP?
– Robert Harvey♦
Jan 2 at 18:05
1
@RobertHarvey I'm sorry I can't get it. Do you mean this is an expected behavior, however it is bad and should be removed ?
– Accountant م
Jan 2 at 18:10
1
Run this first to check if it is an array before indexing it: php.net/manual/en/function.is-array.php. Or, y'know, just write your code correctly.
– Robert Harvey♦
Jan 2 at 18:13
2
@RobertHarvey The question is about why. If I wrote$users = ; $user = 0;
and then made a typo in referencing $user instead of $users, like this:echo $user['foo'];
, I would want to know why I'm not getting a notice message about it. I don't think it's feasible to run a variable through is_array() every time you want to reference it.
– RToyo
Jan 2 at 18:17
I know what the question is about. I stopped worrying about stuff like this a long time ago. As a result, I've managed to maintain at least a portion of my sanity.
– Robert Harvey♦
Jan 2 at 18:18
1
1
Because it's PHP?
– Robert Harvey♦
Jan 2 at 18:05
Because it's PHP?
– Robert Harvey♦
Jan 2 at 18:05
1
1
@RobertHarvey I'm sorry I can't get it. Do you mean this is an expected behavior, however it is bad and should be removed ?
– Accountant م
Jan 2 at 18:10
@RobertHarvey I'm sorry I can't get it. Do you mean this is an expected behavior, however it is bad and should be removed ?
– Accountant م
Jan 2 at 18:10
1
1
Run this first to check if it is an array before indexing it: php.net/manual/en/function.is-array.php. Or, y'know, just write your code correctly.
– Robert Harvey♦
Jan 2 at 18:13
Run this first to check if it is an array before indexing it: php.net/manual/en/function.is-array.php. Or, y'know, just write your code correctly.
– Robert Harvey♦
Jan 2 at 18:13
2
2
@RobertHarvey The question is about why. If I wrote
$users = ; $user = 0;
and then made a typo in referencing $user instead of $users, like this: echo $user['foo'];
, I would want to know why I'm not getting a notice message about it. I don't think it's feasible to run a variable through is_array() every time you want to reference it.– RToyo
Jan 2 at 18:17
@RobertHarvey The question is about why. If I wrote
$users = ; $user = 0;
and then made a typo in referencing $user instead of $users, like this: echo $user['foo'];
, I would want to know why I'm not getting a notice message about it. I don't think it's feasible to run a variable through is_array() every time you want to reference it.– RToyo
Jan 2 at 18:17
I know what the question is about. I stopped worrying about stuff like this a long time ago. As a result, I've managed to maintain at least a portion of my sanity.
– Robert Harvey♦
Jan 2 at 18:18
I know what the question is about. I stopped worrying about stuff like this a long time ago. As a result, I've managed to maintain at least a portion of my sanity.
– Robert Harvey♦
Jan 2 at 18:18
add a comment |
1 Answer
1
active
oldest
votes
From the documentation:
Array dereferencing a scalar value which is not a string silently yields NULL, i.e. without issuing an error message.
Hm, OK. I thought the OP wanted to know why, but I guess not.
– Robert Harvey♦
Jan 2 at 19:12
He asked about PHP not showing "undefined index" in this situation. The answer covers that. But I could not find any info related to why this is implemented the way it is (really weird imo).
– taavs
Jan 2 at 19:22
I wasn't aware of that line in the documentation before, and if I understand it right, if the official documentation says that, then this is an answer of why because it is an intended behavior made by the the PHP makers. thank you @RobertHarvey for the comments and thank you taavs for the answer
– Accountant م
Jan 2 at 19:33
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
From the documentation:
Array dereferencing a scalar value which is not a string silently yields NULL, i.e. without issuing an error message.
Hm, OK. I thought the OP wanted to know why, but I guess not.
– Robert Harvey♦
Jan 2 at 19:12
He asked about PHP not showing "undefined index" in this situation. The answer covers that. But I could not find any info related to why this is implemented the way it is (really weird imo).
– taavs
Jan 2 at 19:22
I wasn't aware of that line in the documentation before, and if I understand it right, if the official documentation says that, then this is an answer of why because it is an intended behavior made by the the PHP makers. thank you @RobertHarvey for the comments and thank you taavs for the answer
– Accountant م
Jan 2 at 19:33
add a comment |
From the documentation:
Array dereferencing a scalar value which is not a string silently yields NULL, i.e. without issuing an error message.
Hm, OK. I thought the OP wanted to know why, but I guess not.
– Robert Harvey♦
Jan 2 at 19:12
He asked about PHP not showing "undefined index" in this situation. The answer covers that. But I could not find any info related to why this is implemented the way it is (really weird imo).
– taavs
Jan 2 at 19:22
I wasn't aware of that line in the documentation before, and if I understand it right, if the official documentation says that, then this is an answer of why because it is an intended behavior made by the the PHP makers. thank you @RobertHarvey for the comments and thank you taavs for the answer
– Accountant م
Jan 2 at 19:33
add a comment |
From the documentation:
Array dereferencing a scalar value which is not a string silently yields NULL, i.e. without issuing an error message.
From the documentation:
Array dereferencing a scalar value which is not a string silently yields NULL, i.e. without issuing an error message.
answered Jan 2 at 18:28


taavstaavs
35129
35129
Hm, OK. I thought the OP wanted to know why, but I guess not.
– Robert Harvey♦
Jan 2 at 19:12
He asked about PHP not showing "undefined index" in this situation. The answer covers that. But I could not find any info related to why this is implemented the way it is (really weird imo).
– taavs
Jan 2 at 19:22
I wasn't aware of that line in the documentation before, and if I understand it right, if the official documentation says that, then this is an answer of why because it is an intended behavior made by the the PHP makers. thank you @RobertHarvey for the comments and thank you taavs for the answer
– Accountant م
Jan 2 at 19:33
add a comment |
Hm, OK. I thought the OP wanted to know why, but I guess not.
– Robert Harvey♦
Jan 2 at 19:12
He asked about PHP not showing "undefined index" in this situation. The answer covers that. But I could not find any info related to why this is implemented the way it is (really weird imo).
– taavs
Jan 2 at 19:22
I wasn't aware of that line in the documentation before, and if I understand it right, if the official documentation says that, then this is an answer of why because it is an intended behavior made by the the PHP makers. thank you @RobertHarvey for the comments and thank you taavs for the answer
– Accountant م
Jan 2 at 19:33
Hm, OK. I thought the OP wanted to know why, but I guess not.
– Robert Harvey♦
Jan 2 at 19:12
Hm, OK. I thought the OP wanted to know why, but I guess not.
– Robert Harvey♦
Jan 2 at 19:12
He asked about PHP not showing "undefined index" in this situation. The answer covers that. But I could not find any info related to why this is implemented the way it is (really weird imo).
– taavs
Jan 2 at 19:22
He asked about PHP not showing "undefined index" in this situation. The answer covers that. But I could not find any info related to why this is implemented the way it is (really weird imo).
– taavs
Jan 2 at 19:22
I wasn't aware of that line in the documentation before, and if I understand it right, if the official documentation says that, then this is an answer of why because it is an intended behavior made by the the PHP makers. thank you @RobertHarvey for the comments and thank you taavs for the answer
– Accountant م
Jan 2 at 19:33
I wasn't aware of that line in the documentation before, and if I understand it right, if the official documentation says that, then this is an answer of why because it is an intended behavior made by the the PHP makers. thank you @RobertHarvey for the comments and thank you taavs for the answer
– Accountant م
Jan 2 at 19:33
add a comment |
1
Because it's PHP?
– Robert Harvey♦
Jan 2 at 18:05
1
@RobertHarvey I'm sorry I can't get it. Do you mean this is an expected behavior, however it is bad and should be removed ?
– Accountant م
Jan 2 at 18:10
1
Run this first to check if it is an array before indexing it: php.net/manual/en/function.is-array.php. Or, y'know, just write your code correctly.
– Robert Harvey♦
Jan 2 at 18:13
2
@RobertHarvey The question is about why. If I wrote
$users = ; $user = 0;
and then made a typo in referencing $user instead of $users, like this:echo $user['foo'];
, I would want to know why I'm not getting a notice message about it. I don't think it's feasible to run a variable through is_array() every time you want to reference it.– RToyo
Jan 2 at 18:17
I know what the question is about. I stopped worrying about stuff like this a long time ago. As a result, I've managed to maintain at least a portion of my sanity.
– Robert Harvey♦
Jan 2 at 18:18