/    Sign up×
Community /Pin to ProfileBookmark

Banging my head on the desk…

Why is it that the following code…

[CODE]
<?php
$states=array(“AK”,”AL”,”AR”,”AZ”,”CA”,”CO”,”CT”,”DE”,”…etc”);
foreach($states as $key => $value)
{
if ($_SESSION[c_state]=$value) echo ” <option value=”$value” selected>$value</option>n”;
else echo ” <option value=”$value”>$value</option>n”;
}
?>
[/CODE]

Produces the following output?

[CODE]
<option value=”AK” selected>AK</option>
<option value=”AL” selected>AL</option>
<option value=”AR” selected>AR</option>
<option value=”AZ” selected>AZ</option>

<option value=”CA” selected>CA</option>
<option value=”CO” selected>CO</option>
<option value=”CT” selected>CT</option>
<option value=”DE” selected>DE</option>
<option value=”DC” selected>DC</option>
<option value=”FL” selected>FL</option>

…etc…
[/CODE]

… when $_SESSION[c_state] doesn’t equal ANYTHING? I’m so frustrated… Thanks for helping, in advance.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@rch10007Oct 02.2005 — First, you need to use: session_start() before attempting to use any $_SESSION variable.

Second, your 'if' statement isn't enclosed in brackets { } so therefore it is basically being skipped over.
Copy linkTweet thisAlerts:
@leadedauthorOct 02.2005 — That's just a code snippet. I have the session_start at the top. Also, according to the php.net documentation, I don't need the curly brackets if there's one statement after the if. That format works on other parts of my code too...
Copy linkTweet thisAlerts:
@NogDogOct 02.2005 — Need comparison '==' instead of assignment '=' :
<i>
</i>if ($_SESSION[c_state]=[color=red]=[/color]$value)

And to be really correct ([url=http://www.php.net/manual/en/language.types.array.php#language.types.array.foo-bar]more info[/url]):
<i>
</i>if ($_SESSION[[color=red]'[/color]c_state[color=red]'[/color]]==$value)
Copy linkTweet thisAlerts:
@rch10007Oct 02.2005 — missed the assignment operator
Copy linkTweet thisAlerts:
@NogDogOct 02.2005 — missed the assignment operator[/QUOTE]
After you've made that mistake a few hundred times, you learn to look for it when your if blocks execute more than they should. ?
Copy linkTweet thisAlerts:
@leadedauthorOct 03.2005 — Thank you!! I should've known that. I don't do PHP that often (or any programming for that matter), but I totally understand it. Because I wasn't doing a comparison, but rather an assignment, I was giving my session variable a value every time. Duh!
Copy linkTweet thisAlerts:
@agent_x91Oct 03.2005 — Hehe mistaking == and = is all too common. Oddly enough I've never made that mistake? But I dread the time that I do, because I'll never think to look for it?
×

Success!

Help @leaded spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...