.Ben Posted February 1, 2007 Share Posted February 1, 2007 Hello, I realized that you have made a mistake on the PHP code on the Arrays lesson. http://www.phpfreaks.com/tutorials/42/1.php On this code example: <?php $bakery = array( "cherry" => "5.00", "apple" => "4.00", "other" => "2.00", ); echo "cherry pie costs $bakery['cherry'], apple pie costs $bakery['apple'], and that other pie costs $bakery['other']."; ?> Correction: <?php $bakery = array( "cherry" => "5.00", "apple" => "4.00", "other" => "2.00", ); echo "cherry pie costs ".$bakery['cherry'].", apple pie costs ".$bakery['apple'].", and that other pie costs ".$bakery['other']."."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/36621-php-correction-on-your-site/ Share on other sites More sharing options...
redbullmarky Posted February 1, 2007 Share Posted February 1, 2007 the original method also works. the double quotes for a string (") instead of singles (') allow you to embed variables like this. i actually use the original way quite a bit, however I seperate them from the content using { } like: <?php $string = "my name is {$myname['forename']} {$myname['surname']}"; ?> just to avoid any confusion. either way, both ways are valid and will work. Quote Link to comment https://forums.phpfreaks.com/topic/36621-php-correction-on-your-site/#findComment-174545 Share on other sites More sharing options...
obsidian Posted February 1, 2007 Share Posted February 1, 2007 For sake of argument, though, I will add that the original posted "error" will produce warnings; however, it will not break your script. If you use an array value within double quotes, you need to either encapsulate it within brackets like redbullmarky shows, or leave out the quotes around the key all together: <?php // This will produce warnings echo "cherry pie costs $bakery['cherry'], apple pie costs $bakery['apple'], and that other pie costs $bakery['other']."; // The following are all acceptable (no warnings or errors) echo "cherry pie costs $bakery[cherry], apple pie costs $bakery[apple], and that other pie costs $bakery[other]."; echo "cherry pie costs {$bakery['cherry']}, apple pie costs {$bakery['apple']}, and that other pie costs {$bakery['other']}."; echo "cherry pie costs " . $bakery['cherry'] . ", apple pie costs " . $bakery['apple'] . ", and that other pie costs " . $bakery['other'] . "."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/36621-php-correction-on-your-site/#findComment-174554 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.