Jump to content

variable used later in code?


galvin

Recommended Posts

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?

Link to comment
Share on other sites

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>";
}

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.