Jump to content

$counter inside a variable name


russthebarber

Recommended Posts

If I have a for loop with a variable called $counter, how can I use the value of this inside a variable name. For example:

 

If I want a bunch of variables called $d1name, $d2name, $d3name ...etc. what's the correct syntax to use? The following gives me a string but not a variable name:

 

$myVariable = "$"."d".$counter."name";

Link to comment
Share on other sites

Edit: Basically says the same ^^^

 

Please simply just use an array. Arrays are for sets of related data.

 

Creating a series of numbered variables takes three times longer to reference, takes more overall code, and also requires that you keep track of how many of them you created. Arrays don't have any of these drawbacks.

Link to comment
Share on other sites

These variables already have values that are stored in a mysql table. What I am looking for is the correct syntax to use to reference these variable names. I don't care if I use an array or not but the problem is still the same...... I am confused with the syntax that will enable me to use the variable or array reference inside the name of another variable.

 

There may be 100 variables to enter into the mysql table but I am trying to avaoid listing all 500 like this:

 

$sql= "UPDATE `bobtracks`.`charts` SET `d01track_artist` = '$d01artist' WHERE `charts`.`chart_no` = '$chart_no'";

$result = mysql_query($sql);

$sql= "UPDATE `bobtracks`.`charts` SET `d02track_artist` = '$d02artist' WHERE `charts`.`chart_no` = '$chart_no'";

$result = mysql_query($sql);

$sql= "UPDATE `bobtracks`.`charts` SET `d03track_artist` = '$d03artist' WHERE `charts`.`chart_no` = '$chart_no'";

$result = mysql_query($sql);

 

...etc.

 

 

 

 

 

 

 

Link to comment
Share on other sites

Thanks for all the tips, people. I will bear all this info in mind and can learn much from it. I don't want to seem ungrateful for the tips but it really would be nice if the question asked was the priority to answer. I see this so often in forums that everybody is very keen to tell the newbie he is doing it all wrong that they ignore the actual question. Like I say, I am grateful for the advice but please bear this in mind.

 

Thank you tomfmason for actually answering the question asked.

Link to comment
Share on other sites

I see this so often in forums that everybody is very keen to tell the newbie he is doing it all wrong that they ignore the actual question
If I said to you "hey I'm building a house and I have an entire wall made out of Lego already, how do I run electrical wire through lego walls?" what would you say?  Would you tell me the answer, or would you say "use wood to make a house"?

 

We don't want to help you do something wrong, only to have you come back in 3 weeks saying "my variable variables are acting up and I can't possibly debug them because I've used variable variables so I can't tell what's happening."

 

This is why the usenet/forum mantra is usually "tell us what you're trying to accomplish, don't ask how to implement what you've decided is the solution."

 

That being said, ${"SomeStringWith" . $variables} is the actual solution, but it's wrong, and if you come back with problems someone will ask you why you've done it wrong.

 

In case you haven't found it, the PHP manual section on arrays is here.

 

-Dan

Link to comment
Share on other sites

Given that variable variables take three times longer to reference (to set or to get a value) than an array, that you apparently have a lot of data you are processing this way, and that your code was probably already taking two-three times longer than necessary (based on what you did show you are doing), taking the advice from experienced programmers and making simple changes to the method you are using would likely result in an overall 4x-5x performance increase over what you have ended up with. I would take that kind of increase in performance any day, especially if the script will be used over and over.

 

So yes, we also see newbies not taking simple advice to cut their losses and make the process of getting a working script go easier for them; take many times longer to produce working code because of the amount of unnecessary code and fighting a bad design; and coming up with code that is so complicated for what it is actually accomplishing that it cannot be understood by the person who wrote it and now wants help with it or anyone trying figure out what is doing in order to give that help.

Link to comment
Share on other sites

Don't agree, sorry. If someone asks me how to repair a tyre on their bike....I do not suggest that they drive a car instead as it's quicker. I can only say how I would have answered this if it were a topic closer to my area of expertise:

 

Here's how you put variables inside of variables but be warned it's not a good way of working in general. If you want more on that I'd be happy to help.

 

 

Link to comment
Share on other sites

Don't agree, sorry. If someone asks me how to repair a tyre on their bike....I do not suggest that they drive a car instead as it's quicker.

Excellent use of an incorrect analogy.  Well done.  Nobody told you to use PERL, we told you to use PHP properly.  If someone asked how to repair the tire on their bike with tin foil and paste, would you help? 

 

You've still missed my point:  You're a new developer.  We expect you to come back.  We have no problem with you coming back, but in order to decrease our future headaches, we tell you the proper way to do things so that in a month when you come back, your code isn't completely unreadable.  You're perfectly welcome complain about it, but now you know the right way to do things. 

 

The fact that arrays would make your code readable, easier to maintain, and legible is in addition to the fact that arrays will simply work faster and more efficiently, and will allow your application to be more powerful.

 

-Dan

Link to comment
Share on other sites

Dan,

 

Thanks for getting back to me. I really feel quite strongly about this as forums are such a great way for people to learn and it's so important for people to use them in the most useful way possible. But I really feel we are going around in circles a bit. So, I'm stopping after this.

 

You've still missed my point

 

No I haven't! I haven't missed any of your points. You gave me some great advice. Have you missed my point? Here it is again....

 

This was great answer to my question:

 

$foo = "bar";

${$foo} = "test";

echo $bar;

 

That's my point. If I wanted to ask about optimising speed for my database I would have done. I asked about variables inside of variables. Thanks again for the tips but I'm sorry that you can't take my point on board that answering my question was what was needed the most and any other help or advice would have been thankfully received afterwards. In fact it was thankfully received.

Link to comment
Share on other sites

And yet, after this entire conversation, you've still missed the point.  Here it is for the last time:  Variable variables are wrong, in almost every situation.

 

Even with all the discussions, all the analogies, and all the experts telling you that you're doing it wrong, you still think that we should have provided you with the method to do it the wrong way. We wouldn't be very helpful if that's what we did, would we?

 

You're still learning.  Part of the learning process is differentiating between "possible" and "correct."  Yes, it's entirely possible to use variable variables.  In fact, it's possible to combine variable variables, anonymous functions, and variable function names to make code that is completely illegible and impossible to debug.  This is the difference between "possible" and "correct."  What you were asking was possible, but it was incorrect.  In an attempt to try to HELP you rather than simply acting like breathing PHP manuals, we told you the right way to do it.

 

In the future, if all you want is the syntax for how to do a specific thing, read the PHP manual, that's what it's for.  Since you asked a question of other human beings, we provided additional information on proper PHP usage.

 

-Dan

Link to comment
Share on other sites

open mouth and insert foot  :shrug:

 

This was great answer to my question:

 

$foo = "bar";
${$foo} = "test";
echo $bar;

That's my point. If I wanted to ask about optimising speed for my database I would have done. I asked about variables inside of variables. Thanks again for the tips but I'm sorry that you can't take my point on board that answering my question was what was needed the most and any other help or advice would have been thankfully received afterwards. In fact it was thankfully received.

 

 

I answered that because while everyone else was giving you the correct answer no one had actually answered the question you asked. There are very very few occiasions where this would be a correct method. An example(correct) usage would be from my cakephp rest implementation. Another example could be something like this

 

<?php
class Foo {
    function dispatch($method) {
        $m = "do_$method";
        if(in_array($m,get_class_methods($this))) {
            return $this->{$m}();
        } else {
            return "invalid method";
        }
    }
    
    function do_something() {
        return "Something";
    }
}

$foo = new Foo();
echo $foo->dispatch($_GET['method']);
?>

 

As others have stated there are very few occasions where this would be a correct method. Your case would be much better suited with an associative 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.