Demonic Posted April 16, 2007 Share Posted April 16, 2007 Is it or is it not true that a valid array can be called without single quotes inside a double quote array: <?php //Neel Basu.Temp.ERROR.Understanding error_reporting(E_ALL); $ary = array( "var1" => 'Val1', "var2" => 'Val2'); $ary['var1'] = "New Value"; echo "$ary[var2]"; ?> Now my friend neel_basu said thats an invalid way to code. echoing a variable like that it should be like this: <?php //Neel Basu.Temp.ERROR.Understanding error_reporting(E_ALL); $ary = array( "var1" => 'Val1', "var2" => 'Val2'); $ary['var1'] = "New Value"; echo "".$ary['var2'].""; ?> Now is it or is it not valid by doing both ways. He don't believe me that this is a valid way to code and this is making me angry.. http://uni-code.com/codebox/arrayzz.php Quote Link to comment Share on other sites More sharing options...
suzzane2020 Posted April 16, 2007 Share Posted April 16, 2007 ur friend is rite "" are for string representations Quote Link to comment Share on other sites More sharing options...
Demonic Posted April 16, 2007 Author Share Posted April 16, 2007 How so if PHP doesn't echo out any errors when I tried my example? How come when I do this: it throws an error? <?php //Neel Basu.Temp.ERROR.Understanding error_reporting(E_ALL); $ary = array( "var1" => 'Val1', "var2" => 'Val2'); $ary['var1'] = "New Value"; echo "$ary['var2']"; ?> This is ignoring.. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 16, 2007 Share Posted April 16, 2007 Technically, your friend is correct. If you enable all error reporting, you will see the warnings issued when you don't quote the index of an array. When the index is not quoted, PHP will try to see if it is a Defined Constant, if it isn't, PHP will issue a Warning Message and then use the string value for the index, so the code will work. BTW, if the reference is the only item being echoed, you don't need the double quotes at all: <?php echo $ary['var2']; ?> Ken Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 16, 2007 Share Posted April 16, 2007 you're both right. if you do it your way, it won't throw any errors, but your friend is saying that it isn't good coding practice. he's right about that. altho it is admirable testing the limits of php. let us know what else you find. EDIT: When the index is not quoted, PHP will try to see if it is a Defined Constant, if it isn't, PHP will issue a Warning Message and then use the string value for the index, so the code will work. Ken does this behavior differ between PHP4 and PHP5? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 16, 2007 Share Posted April 16, 2007 This <?php echo "$ary['var2']"; ?> is illegal and PHP will print an Error Message This <?php echo "$ary[var2]"; ?> will cause PHP to produce a Warning Message, which you won't see unless you've turned on reporting of warning messages. This <?php echo "{$ary['var2']}"; ?> and this <?php echo $ary['var2']; ?> are legal. Ken Quote Link to comment Share on other sites More sharing options...
Demonic Posted April 16, 2007 Author Share Posted April 16, 2007 we was referring to my code here for the select boxes :S http://uni-code.com/codebox/order.php Is there any harm in what I've done? <?php //Connection include junk stufferz $c = mysql_query("SELECT * FROM category"); $c2 = mysql_query("SELECT * FROM category"); if(!isset($_POST['update'])){ echo(" <form method='post'> <select name='mfy'> "); while($ct = mysql_fetch_array($c)){ echo ("<option value='$ct[order]'>$ct[name]</option>\n"); } echo (" </select> <select name='order'> <option value='before'>Before</option> <option value='after'>After</option> </select> "); echo (" <select name='cats'> "); while($cd = mysql_fetch_array($c2)){ echo ("<option value='$cd[order]'>$cd[name]</option>\n"); } echo(" </select><br /> <input type='submit' value='Update Category' name='update' /> </form> "); }else{ if($_POST['order'] == "before"){ $current = $_POST['mfy']; $before = $_POST['cats']; $idz = mysql_query("SELECT * FROM category ORDER BY `order` DESC"); $boom = mysql_num_rows($idz); //echo $boom."<br />\n"; $newO = ($before != 0) ? $before-1 : 0; $new2 = $before+1; $orderz = mysql_query("UPDATE category SET `order` = '$newO' WHERE `order` = '$current' ") or die("ERROR"); //$orderz2 = mysql_query("UPDATE category SET `order` = '$new2' WHERE `order` = '$before' ") or die("ERROR"); while($id2 = mysql_fetch_array($idz)){ if($id2[order] > $before || $id2[order] == $current){ continue; } $order1 = $id2[order]+1; $order2 = $id2[id]; mysql_query("UPDATE category SET `order` = '$order1' WHERE `id` = '$order2' ") or die("ERROR2"); } } } echo (" <BR><BR><BR><BR><BR><BR><BR><BR> ================================= <BR> "); $bb = mysql_query("SELECT * FROM category ORDER BY `order` ASC"); while($bbb = mysql_fetch_array($bb)){ echo ("$bbb[id] <b>:</b> $bbb[order] <b>:</b> $bbb[name]<br />\n"); } echo ("<BR> ================================= "); ?> Quote Link to comment Share on other sites More sharing options...
Demonic Posted April 16, 2007 Author Share Posted April 16, 2007 This <?php echo "$ary['var2']"; ?> is illegal and PHP will print an Error Message This <?php echo "$ary[var2]"; ?> will cause PHP to produce a Warning Message, which you won't see unless you've turned on reporting of warning messages. This <?php echo "{$ary['var2']}"; ?> and this <?php echo $ary['var2']; ?> are legal. Ken http://uni-code.com/codebox/arrayzz.php your second example I used this: <?php //Neel Basu.Temp.ERROR.Understanding error_reporting(E_ALL); $ary = array( "var1" => 'Val1', "var2" => 'Val2'); $ary['var1'] = "New Value"; echo "$ary[var2]"; ?> I don't see any errors though Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 16, 2007 Share Posted April 16, 2007 I would get into the habit of always quoting all array index values that are strings. This way, if PHP ever changes the rules, your code won't break. Ken Quote Link to comment Share on other sites More sharing options...
Demonic Posted April 16, 2007 Author Share Posted April 16, 2007 I would get into the habit of always quoting all array index values that are strings. This way, if PHP ever changes the rules, your code won't break. Ken Alright thanks ill start using that coding habit. I was just used to using my way is all. Quote Link to comment Share on other sites More sharing options...
neel_basu Posted April 16, 2007 Share Posted April 16, 2007 OK I am here and here Is The Real fact ----------------------------------------- 1. Always Learn something thats Universaly true Dont deppend on the forgivefullness of any Language. In other Languages Variables Must be Out of Quotes Even In php variables within Single Quotes are treated as Normal Text 2. Here When You Use Array keys / Indexes Without Quotes It Understands it as a CONSTANT that has been defined ABOVE and if(!defined('that_index_or_key')) [something like that] PHP Assumes it as a text. 3. As Constants cannot be in Quotes When Using echo "$ary[var2]"; PHP Is Quite Sure about it that var2 isn't a constantas its in Double Quote Thats Why You dont Need Quotes Over here. And Its not Showing Errors 4. Always Try to Code In this Style echo "Text1".$ary['key']."Text2"; 5. Personaly Speaking I can't not do that type of Coding [Without Quotes] as I Make Libraries. And In development Stage You all should Use error_reporting(E_ALL); Quote Link to comment Share on other sites More sharing options...
neel_basu Posted April 16, 2007 Share Posted April 16, 2007 This Code: <?php echo "$ary[var2]"; ?> will cause PHP to produce a Warning Message, which you won't see unless you've turned on reporting of warning messages. No It Will no Show any Errors As I told Above in point no. 3 Try this <?php $ary['var2'] = "Text"; error_reporting(E_ALL); echo "$ary[var2]"; ?> Quote Link to comment 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.