?
Solved

Why did I first need to declare this variable in PHP?

Posted on 2014-08-19
6
Medium Priority
?
162 Views
Last Modified: 2014-08-19
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

0
Comment
Question by:LB1234
  • 2
  • 2
  • 2
6 Comments
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 total points
ID: 40270353
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
 
LVL 12

Accepted Solution

by:
Radek Baranowski earned 1200 total points
ID: 40270363
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
 
LVL 1

Author Comment

by:LB1234
ID: 40270371
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Closing Comment

by:LB1234
ID: 40270376
Thanks Gents!
0
 
LVL 12

Expert Comment

by:Radek Baranowski
ID: 40270379
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40270399
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question