/    Sign up×
Community /Pin to ProfileBookmark

array –doubt

hi

i would like to know how the output differs becos of bracket usage..wt s the main usage of bracket
<?
$arr = array(‘fruit’ => ‘apple’, ‘veggie’ => ‘carrot’);
print $arr[‘fruit’]; // apple
print $arr[‘veggie’]; // carrot
<br>
<?
print $arr[fruit]; // apple
?><br><hr>
<?
define(‘fruit’, ‘veggie’);
// Notice the difference now
print $arr[‘fruit’]; // apple
?>
<br>
<?
print $arr[fruit]; // carrot
?>
<br>
<?
print “Hello $arr[fruit]”; // Hello apple
?><br>
<?
print “Hello {$arr[fruit]}”; // Hello carrot–[B]clarification needed here[/B]
?>
<br>
<?
print “Hello $arr[fruit]”; // Hello apple
?>
<br><hr>
<?
// Concatenation is another option
print “Hello ” . $arr[‘fruit’]; // Hello apple
?>

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@keerthiauthorDec 22.2005 — does it mean that the next array element will be printed as output..am i correct?
Copy linkTweet thisAlerts:
@NogDogDec 22.2005 — It means that when you want to have the value of a variable expressed within a double-quoted string, by wrapping the variable name inside of curly braces you help the parser recognize the precise variable name. This is not necessary for simple variable names which are easily determined by the parser, such as [font=courier][b]"This is a $variable inside a string"[/b][/font]. But you might need clarification for this: [font=courier][b]"This_is a {$variable}_inside_a_string"[/b][/font]. And any time you want to use an array element variable inside a double-quoted string, you need to use the braces as well or the parser may get confused: [font=courier][b]"This is an {$array['variable']} inside a string"[/b][/font].
×

Success!

Help @keerthi 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...