Back to questions
JuniorTheoryCommon
What happens when you run this program?
<?php
$colors = ['red', 'green'];
echo $colors[1], "\n";
echo $colors[2] ?? 'none', "\n";
echo $colors[2], "\n";green, none, then a Warning: Undefined array key 2
- Expecting a missing key to read as a silent
null: PHP printsWarning: Undefined array key 2first, then readsnull, soechoprints an empty line. - Expecting the script to stop at
$colors[2]: a missing key is a warning, not a fatal error, so that line still prints and the script runs on.
- How do you check that index
2exists before reading it? - Which index holds the last element of a list of
count($colors)elements?