/    Sign up×
Community /Pin to ProfileBookmark

Why Changing Var Names Cause Error ?

Folks,

This from w3schools works

[code]
<!DOCTYPE html>
<html>
<body>

<?php
$age = array(“Peter”=>”35”, “Ben”=>”37”, “Joe”=>”43”);
asort($age);

foreach($age as $x => $x_value) {
echo “Key=” . $x . “, Value=” . $x_value;
echo “<br>”;
}
?>

</body>
</html>

[/code]

Now change the $x to $y and $x_value to $value.
And you get error! Why ?

[code]
<!DOCTYPE html>
<html>
<body>

<?php
$age = array(“Peter”=>”35”, “Ben”=>”37”, “Joe”=>”43”);
asort($age);

foreach($age as $y => $value) {
echo “Key=” . $y . “, Value=” . $value;
echo “<br>”;
}
?>

</body>
</html>
[/code]

Try changing code here and see for yourself.

https://www.w3schools.com/php/phptryit.asp?filename=tryphp_array_asort

PHP Parse error: syntax error, unexpected ‘”Key=”‘ (T_CONSTANT_ENCAPSED_STRING) in /home/tXHEDx/prog.php on line 10

On my localhost, I got error a few times but can’t reproduce anymore and so gave you error from w3schools as error over there always reproducible.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumFeb 11.2021 — Your modified code works for me without issues, in my test file as well as on w3schools.
Copy linkTweet thisAlerts:
@developer_webauthorFeb 12.2021 — @Sempervivum#1627862

Thanks.

Ok then. By looking at the error, what was wring or what did I do wring in my code while renaming the variables ?
×

Success!

Help @developer_web 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.19,
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,
)...