runeveryday Posted July 28, 2009 Share Posted July 28, 2009 how many ways that we can do to output an array's result, eg:$row=array{...} as far as i know,the followings are the output's format,which is right?and why? echo $row[$key]; echo $row[key]; echo $row['key']; i have tested the echo $row[$key]; is wrong, and what is the difference of the rest.thank you! Link to comment https://forums.phpfreaks.com/topic/167761-the-way-of-output-an-arrays-result/ Share on other sites More sharing options...
waynew Posted July 28, 2009 Share Posted July 28, 2009 $row[$key] should work, IF $key represents an index number of the array or an associative key. $row['key'] will work IF there is an associative key inside the array called key. I've seen $row[key] being used, but I've never used it myself as I'm pretty sure that $row['key'] and $row[key] are the same thing. Link to comment https://forums.phpfreaks.com/topic/167761-the-way-of-output-an-arrays-result/#findComment-884692 Share on other sites More sharing options...
Mark Baker Posted July 28, 2009 Share Posted July 28, 2009 I've seen $row[key] being used, but I've never used it myself as I'm pretty sure that $row['key'] and $row[key] are the same thing. They're not. 'key' with the quotes is treated as a string value of 'key' key (without the quotes) is treated as a constant called key, and PHP looks to the definition of this constant and uses that value. If no constant called key exists, then PHP will interpret it as a string with a value of 'key'.... and give you a Notice that it has done this. error_reporting(E_ALL); ini_set('display_errors', 1); define('key','lock'); $test = array('key' => 1, 'lock' => 2); $key = 'lock'; echo $test[$key].'<br />'; echo $test['key'].'<br />'; echo $test[key].'<br />'; echo $test[lock].'<br />'; Link to comment https://forums.phpfreaks.com/topic/167761-the-way-of-output-an-arrays-result/#findComment-884768 Share on other sites More sharing options...
waynew Posted July 28, 2009 Share Posted July 28, 2009 Constants should be in all uppercase. Link to comment https://forums.phpfreaks.com/topic/167761-the-way-of-output-an-arrays-result/#findComment-884771 Share on other sites More sharing options...
Mark Baker Posted July 28, 2009 Share Posted July 28, 2009 Constants should be in all uppercase.Again wrong. Constants are normally all uppercase by convention rather than be necessity, but default behavious is case-sensitive. You can force them to be case insensitive with the optional third parameter for define(). error_reporting(E_ALL); ini_set('display_errors', 1); define('Constant1','Hello World'); define('Constant2','Hello Flowers',false); define('Constant3','Hello Trees',true); echo constant1.'<br />'; echo Constant1.'<br />'; echo CONSTANT1.'<br />'; echo constant2.'<br />'; echo Constant2.'<br />'; echo CONSTANT2.'<br />'; echo constant3.'<br />'; echo Constant3.'<br />'; echo CONSTANT3.'<br />'; Link to comment https://forums.phpfreaks.com/topic/167761-the-way-of-output-an-arrays-result/#findComment-884777 Share on other sites More sharing options...
patrickmvi Posted July 28, 2009 Share Posted July 28, 2009 Personally I don't use the single or double-quotes as it requires more typing but I agree that it would probably be more efficient to use them as PHP does spit out notices saying that this is bad if you have notices turned on. Link to comment https://forums.phpfreaks.com/topic/167761-the-way-of-output-an-arrays-result/#findComment-884810 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.