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>

Open in new window

LVL 1
LB1234Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
What you got is not an "Error," it is a Notice.  It comes on line 14, where PHP is kind enough to let you know that your script relies on an undefined variable, $options.

The default configuration of PHP will not raise Notice level messages.  IMHO this has always been one of the worst ideas in PHP.  It was well-intentioned in the "old days" when we were trying to make PHP the easiest language for anyone to learn.  But PHP has grown up a lot since then!
0
Radek BaranowskiFull-stack Java DeveloperCommented:
and you are getting the notice because you use inline string concatenation .=

this equals to

$options = $options + "some text";

so $options is expected on the right side of the assignment to be declared already. that's why adding
$options='';

makes the notice disappear.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LB1234Author Commented:
This may sound really dumb, but aren't I defining it at line 14?   How is what I'm doing on line 14 any different than what I did with $selected on line 13?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

LB1234Author Commented:
Thanks Gents!
0
Radek BaranowskiFull-stack Java DeveloperCommented:
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 ?
0
Ray PaseurCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.