LB1234
asked on
Why did I first need to declare this variable in PHP?
Please see the following code: the commented line below marks the variable PHP demanded I declare, but I use variables all the time without declaring them first. Why did PHP throw an error? I knew the remedy but wanted to expand my understanding. Thanks.
<?php
$selected_car = "chevy";
$car_choices = array ("ford", "chevy", "dodge");
//without declaring this variable I get an error?
$options = "";
foreach ($car_choices as $car_choice) {
$selected = ($selected_car == $car_choice) ? "selected" : "";
$options .= "<option $selected % {$car_choice}>{$car_choice}</option>";
}
?>
<select>
<?php echo $options ?>
</select>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Gents!
here you are making actual initialization by assignment:
$selected = ($selected_car == $car_choice) ? "selected" : "";
and here, you try to assign (initialize) $options value of uninitialized variable $options:
$options .= "<option $selected % {$car_choice}>{$car_choice }</option> ";
is it clear enough ?
$selected = ($selected_car == $car_choice) ? "selected" : "";
and here, you try to assign (initialize) $options value of uninitialized variable $options:
$options .= "<option $selected % {$car_choice}>{$car_choice
is it clear enough ?
PHP only has two string operators.
http://php.net/manual/en/language.operators.string.php
See also:
http://php.net/manual/en/language.operators.assignment.php
http://php.net/manual/en/language.operators.string.php
See also:
http://php.net/manual/en/language.operators.assignment.php
ASKER