Variables as array index references
Posted: Sat Jun 21, 2003 12:37 am
PHP is quickly becoming more complicated than most complex
typed variable languages for me. I need some more basic
education.
In almost every tutorial I have seen, examples of using variables
as array index references take the form $array[$index]. In point of fact
this never works. For example:
<?
$user = array( 'name' => 'Big' , 'surname' => 'Bopper' );
$name = 'Big';
$surname = 'Bopper';
echo 'Name = ' . $user[$name] . $user[$surname];
?>
Will only produce:
Name=
The Apache error_log only indicates 'undefined index:'
In another example, there seems to be a 'type' problem:
<?
$theday = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
$day_of_week = date("w");
echo "$day_of_week <br>";
echo $theday[$day_of_the_week];
?>
produces '5' (as day of the week) and the Apache error_log indicates:
undefined variable: day_of_the_week line 5
undefined index: line 5
At least in this second example, it occurred to me that maybe the
array index had to be an integer rather than a string. If I substitute
echo $theday[5]; for echo $theday[$day_of_the_week];
the script works correctly and produces "Friday" in the output.
I thought that PHP was somewhat like Python or Java and type
conversion was somewhat automatic or at least you didn't have to
worry as much about it. This apparently is not the case.
What do I have to do to make these references work correctly?
1. Can I convert the string to an integer with a built-in PHP function in
the second example? ( I have yet to find it in the manual)
2. Why won't the first example work since the index is already
a string?
TIA
typed variable languages for me. I need some more basic
education.
In almost every tutorial I have seen, examples of using variables
as array index references take the form $array[$index]. In point of fact
this never works. For example:
<?
$user = array( 'name' => 'Big' , 'surname' => 'Bopper' );
$name = 'Big';
$surname = 'Bopper';
echo 'Name = ' . $user[$name] . $user[$surname];
?>
Will only produce:
Name=
The Apache error_log only indicates 'undefined index:'
In another example, there seems to be a 'type' problem:
<?
$theday = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
$day_of_week = date("w");
echo "$day_of_week <br>";
echo $theday[$day_of_the_week];
?>
produces '5' (as day of the week) and the Apache error_log indicates:
undefined variable: day_of_the_week line 5
undefined index: line 5
At least in this second example, it occurred to me that maybe the
array index had to be an integer rather than a string. If I substitute
echo $theday[5]; for echo $theday[$day_of_the_week];
the script works correctly and produces "Friday" in the output.
I thought that PHP was somewhat like Python or Java and type
conversion was somewhat automatic or at least you didn't have to
worry as much about it. This apparently is not the case.
What do I have to do to make these references work correctly?
1. Can I convert the string to an integer with a built-in PHP function in
the second example? ( I have yet to find it in the manual)
2. Why won't the first example work since the index is already
a string?
TIA