PHP Dynamic Variables

by 12:41 PM 11 comments
In any language a variable is something you use to store data, normally you give a variable its name and use it by that name.

$my_var = 5;
echo $my_var;

There is this concept called dynamic variables in PHP where you can dynamically name variables for example if you needed 100 variables named var 1 to 100 you would have to write all those variables one after another and use them throughout your code.

With dynamic variables you can create the variable names dynamically in code for example in a for loop or from user input. Here is an example how you'd create 100 dynamic variables:

for($i = 0; $i < 100;$i++)
{
${"var" . ($i+1)} = $i;
}

echo $var1;
echo $var 40;

You'll have to wrap the $var with {} and concatenate with . to create dynamic variables.


Unknown

Android Developer at Appsix LLC

I love stickers and reading books. A cool website that I've found was commitstrip.com
Currently reading: The pragmatic programmer, and The C programming language.
Currently working: an android app, and on a web project with laravel.
Im a 9gag enthusiast.
I write code at work and at home for fun or out of pure boredom.
Programming Languages that I've worked with: Java PHP C# PLSQL JavaScript
Thanks for reading.



11 comments :

  1. Nice and good article. It is very useful for me to learn and understand easily.
    PHP Training in Delhi
    PHP Training institute in Delhi

    ReplyDelete
  2. This article is a great article that I have seen in my career so far, it helps me a lot in storing dynamic variable in PHP, and will continue to do so in the future.

    website development company in Surat Gujarat

    ReplyDelete
  3. I found this information is very impressive. if anyone interested to learn PHP with real-time exposure. we recommend to take our short-term fast-track php course: Best PHP Training Institute in Chennai

    ReplyDelete
  4. Being one of the pioneers in the field of Entrance Automation & Security Solutions, over 25 Years of intense experience, EARTH CONTROL SYSTEMS offers a wide range of solutions. Not only we offer the most suitable features for any and every architectonic project, but we are also capable of customizing niches and colors to integrate our products and solutions in the environment in which they are located.
    • Established in 95th year of 20th Century
    • Experience gained of over 25 years of successful works
    • Professionally managed
    • Provides solution for Industrial, Commercial, Private & Public Sectors
    • Offers the most suitable features for any architectonic projects
    • Capable of customizing niches and colors to integrate innovations
    Top automation company in India

    ReplyDelete
  5. Lc Highway Is The World's First Biggest Platform Where Anyone Can Read & Write Career Reviews Freely And Book Counsellors With A Money-Back Warranty System. Lc Highway Is Also Helping Students By Providing Other Aspects Like Comparing Career, Career Tests, Career Games, Career Reports And Also Availing Career Camps In Schools. Lc Highway Is An Initiative To Help The Education Sector And Also Provide A Platform Where People Can Discuss Their Career Success In The Form Of Career Reviews.

    ReplyDelete

  6. Quick up for the best offer of AWS DevOps Training in Chennai from Infycle Technologies, Excellent software training in Chennai. A massive place to learn other technical courses like Power BI, Cyber Security, Graphic Design and Animation, Block Security, Java, Oracle, Python, Big data, Azure, Python, Manual and Automation Testing, DevOps, Medical Coding etc., with outstanding training with experienced trainers with a fresh environment with 100+ Live Practical Sessions and Real-Time scenario after the finalisation of the course the trainee will able to get through the interview in top MNC’s with an amazing package for more enquiry approach us on 7504633633, 7502633633.

    ReplyDelete