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

Posted on 2014-08-19
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.


$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>";



<?php echo $options ?>


Open in new window

Question by:LB1234
    LVL 107

    Assisted Solution

    by:Ray Paseur
    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!
    LVL 11

    Accepted Solution

    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

    makes the notice disappear.
    LVL 1

    Author Comment

    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?
    LVL 1

    Author Closing Comment

    Thanks Gents!
    LVL 11

    Expert Comment

    by:Radek Baranowski
    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 ?
    LVL 107

    Expert Comment

    by:Ray Paseur

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now