-1

I want to be able to initialize variable in PHP like in Java

$x=1;

while($x<10)
{
    $Num$x=$x;
    $x++;
}

And yes it does not work, my purpose is to make a function to initialize $result=mysqli_query($X,$y)

Let's say i know there is table ID and Name, and want to initialize as $ID1,$ID2,$Name1,$Name2 and so on according to the table size, i know you can use while($row=mysqli_fetch_array($result)) loop but i don't see this is a way to do it with $x cannot work as counter.

SilvarCraw
  • 79
  • 2
  • 10
  • Maybe https://stackoverflow.com/questions/9257505/using-braces-with-dynamic-variable-names-in-php is what you are after? I'd use an array though. – chris85 Dec 04 '17 at 03:50
  • 6
    This is not the right syntax for what you're doing, which is called a "variable variable." But that's rarely, if ever, the right approach, anyway. You should use an array, instead. – elixenide Dec 04 '17 at 03:54
  • @SilvarCraw, could you perhaps explain please in a little more detail what you are trying to achieve? (You can edit your question.) – Progrock Dec 04 '17 at 18:44

2 Answers2

3

As mentioned in comments, use an array instead.
Keeping track of X variables will be hard for you later.
Arrays can be foreach looped unlike variables.

$x=1;

while($x<10)
{
    $arr['num' . $x]=$x;
    $x++; // added this in case you don't like the infinite loop
}

This will create an array with keys named 'num1' to 'num9' with value 1-9.

Andreas
  • 23,610
  • 6
  • 30
  • 62
-1
for($i=1; $i<4; $i++)
    ${"ID$i"} = ${"Name$i"} = null;
Progrock
  • 7,373
  • 1
  • 19
  • 25
  • Edit this answer so that it explains why this is a very bad idea and show how to use arrays instead. Tag me when it's done, and I'll upvote after that. – klutt Dec 04 '17 at 05:10
  • @klutt Isn't that what my answer is? – Andreas Dec 04 '17 at 10:35
  • @Andreas Yes, but your answer does not answer the actual question. Even if what OP asks for is a bad idea or not, it is the actual question. I will not flag your answer for not being an answer, but doing so would not be unjustified. – klutt Dec 04 '17 at 11:28
  • @klutt so whatever the OP asks for is what you give them? How many questions have you seen where OP asks for a regex pattern to parse HTML? Is your answer "yes go ahead, do it!" Or "use a parser"? OP asks a question with very limited knowledge of the topic (hence the question in the first place) he/she must show what he/she has tried and failed. That does not in any way mean it's the best solution. – Andreas Dec 04 '17 at 11:33
  • @Andreas It's different from case to case. Several times I have answered questions like this: "You want to solve problem X with Y. That can be done like this: but that's not recommended because of blabla so instead I recommend this method: ". Another approach is to ask OP to edit the question. But yes, in general an answer should answer the question. – klutt Dec 04 '17 at 11:39
  • 3
    Plain stupid in my opinion. SO is as far as I know not for answering questions, it's a knowledge base. The good answers are supposed to be upvoted and the ones who use poor functions/methods get downvoted. It has nothing to do with what OP thinks. He/she accepts but thats it. What is a good answer is meant to be shown by the votes. You are supposed to send the good answers first when you open a question, not 10 bad answers Bering exact copies of OPs poor code. @klutt – Andreas Dec 04 '17 at 11:45
  • In general I assume that OP wants his question answered. Your answer is more of a comment. However, I think it's good enough to be left alone, but not good enough to deserve an upvote. Nevertheless, I feel I've spent more than enough time explaining why I don't upvote your answer. I'm not obligated to up- or downvote any answer at all. If you want to discuss this thing in general terms, feel free to open up a Q in meta. – klutt Dec 04 '17 at 11:50
  • Off course not, you don't have to do anything but I do not think answering a question with a poor code is correct. – Andreas Dec 04 '17 at 12:03
  • We don't know what exactly the OP wishes to do. They asked how to initialise variables (with the names following a particular pattern), in that regard I have provided an answer. Whether they wish to use an array or separate variables to hold values is their prerogative. – Progrock Dec 04 '17 at 12:12
  • Absolutely @Progrock I hold no grudge at you. Answering the question is "good" and expressing that you wouldn't do it this way is fantastic. But to argue that an answer not using the same (poor) method op wants is not an answer is beyond my understanding. – Andreas Dec 04 '17 at 12:16
  • @Andreas I said I won't discuss this anymore, but I realized there's one important thing to point out. There has been several occasions where I need to solve a thing the wrong way for various reasons. In that situation it's pretty annoying to get tons of answers that only says I'm doing stuff in the wrong way. If you want to point out why OP has the wrong approach, please do, but *answer the question*. And after all, OP accepted this answer and not yours. – klutt Dec 04 '17 at 12:21
  • @klutt yes but in that case you as OP should clearly show both in code and in question why the "good method" does not work for you. If you in that case still get those answers then I understand your point. However looking at the questions you have asked here I can't see any evidence of what you say. Can you show me the question(s) you are talking about? Oh.. and here you go: https://meta.stackoverflow.com/questions/360189/is-ops-method-of-trying-to-solve-a-problem-the-way-we-should-answer-it – Andreas Dec 04 '17 at 12:28
  • One would have thought that discussion of your answer belongs with your answer, as for the finer mechanics and philosophy of SO, take it to META. :) – Progrock Dec 04 '17 at 12:30
  • @Andreas It was before my time on Stackexchange, so I cannot show it. – klutt Dec 04 '17 at 12:32