galvin Posted July 6, 2011 Share Posted July 6, 2011 I have code at the top of my page like this... $N = "<td class='num'>$i</td>"; Then later on, I have code like this snippet below. But this code doesn't seem to be doing what I want (i.e. the $i variable doesn't seem to be respected). So is something wrong with structuring things this way? I'm trying to store repeatable code up top just once (since I use it A LOT) and then use it where needed using this code below, but something isn't right with it. for ($i=1; $i<=8;$i++) { echo "<tr>" . $N . "</tr>"; } FYI, it's outputting nothing where $i is. So in all 8 instances, it's outputting this.. <tr><td class='num'></td><tr> Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/241171-variable-used-later-in-code/ Share on other sites More sharing options...
Alex Posted July 6, 2011 Share Posted July 6, 2011 Simply put, it doesn't work that way. The string, $N, will be assigned a value where it's declared including the value of $i, nothing. Quote Link to comment https://forums.phpfreaks.com/topic/241171-variable-used-later-in-code/#findComment-1238780 Share on other sites More sharing options...
Psycho Posted July 6, 2011 Share Posted July 6, 2011 That is because at the time that you define $N, the $i variable is either not defined or an empty string. When you define a string using double quotes, the string is defined using the values of the variables at the time you define it. The PHP parser has no clue that you plan to use a different value later on. Try this: $N = "<td class='num'>[i_VAR]</td>"; for ($i=1; $i<=8;$i++) { echo "<tr>" . str_replace("[i_VAR]", $i, $N) . "</tr>"; } Quote Link to comment https://forums.phpfreaks.com/topic/241171-variable-used-later-in-code/#findComment-1238781 Share on other sites More sharing options...
PFMaBiSmAd Posted July 6, 2011 Share Posted July 6, 2011 Or you could write a function that would accept and operate on the current value at the time the function is called. Quote Link to comment https://forums.phpfreaks.com/topic/241171-variable-used-later-in-code/#findComment-1238785 Share on other sites More sharing options...
galvin Posted July 6, 2011 Author Share Posted July 6, 2011 PFMaBiSmAd, is writing a function the better option in your opinion? I have a ton of code where "$i" was in the repeatable code at the top of my pages, so doing mjdamato's solution (which works, thanks!) will require a lot of str_replace's in my code. Although, now that I say that, I guess any function I create will need to contain a str_replace anyway. Anyway, just curious which method you think is better before I embark on updating all of my code Quote Link to comment https://forums.phpfreaks.com/topic/241171-variable-used-later-in-code/#findComment-1238787 Share on other sites More sharing options...
PFMaBiSmAd Posted July 6, 2011 Share Posted July 6, 2011 I'm not sure how 'real' your example is, but a function would be used like this - <?php function td_class_num($i){ return "<td class='num'>$i</td>"; } for ($i=1; $i<=8;$i++) { echo "<tr>" . td_class_num($i) . "</tr>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241171-variable-used-later-in-code/#findComment-1238789 Share on other sites More sharing options...
galvin Posted July 6, 2011 Author Share Posted July 6, 2011 If I have "$i" acting as an index in an array, and I want that to get replaced via mjdamato's solution using [i_VAR], how would I write it. Below is not correct. Yes, I sometimes get really confused with all the brackets and quotes and whatnot if ($alldata['[i_VAR]']['image'] == "") In other words, I had this code to start... if ($alldata['$i']['image'] == "") ...but now want to implement mjdamato's solution, how would I rewrite it with [i_VAR] so that all the quotes and brackets are accurate? I'm still not sure if I'm going the route of a function or the [i_VAR] solution, but just trying to clear this up regardless Quote Link to comment https://forums.phpfreaks.com/topic/241171-variable-used-later-in-code/#findComment-1238792 Share on other sites More sharing options...
PFMaBiSmAd Posted July 6, 2011 Share Posted July 6, 2011 Your last question has nothing really to do with the first question and the solutions already posted (unless you are trying to make a template system that has embedded conditional logic in it, in which case you wouldn't do it this way at all.) The [i_VAR] is simply a unique place holder/tag that you make up and put into a STRING at the point where you want to substitute/replace it with an actual value when the code is executed. Your if ($alldata[$i]['image'] == "") code isn't the same situation, because it is not a string you are trying to replace values in. It's an array that expects array index values to address the elements of the array. Quote Link to comment https://forums.phpfreaks.com/topic/241171-variable-used-later-in-code/#findComment-1238798 Share on other sites More sharing options...
galvin Posted July 6, 2011 Author Share Posted July 6, 2011 Ok, makes perfect sense, thanks for explaining! Quote Link to comment https://forums.phpfreaks.com/topic/241171-variable-used-later-in-code/#findComment-1239067 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.