Variable Variables? Was that a typo? Variable variables: no that’s not a typo. That’s what this topic is about. PHP variable variables are dynamic variables, meaning that you can generate variable names dynamically in PHP. How is this done? If you prepend a dollar sign to a string, the string will then become a variable. Let’s create a regular variable $a. The variable $a will contain the string “bear.” We’ll then create another variable, and this time we’ll add an additional dollar sign before it. We’ll call the new variable variable $$a. <?php $a = “bear”; $$a = “cow”;
Tag: PHP Variables
More Math? x + 3 = 5 Kind of Variables? I wrote about variables in the PHP Variables Intro article, but I’m going to dedicate the next two articles explaining variables even further. Three articles on variables? Yes. There’s that much material that needs to be covered. Recap: In PHP, variables start with a dollar sign, followed by either the underscore or a letter, followed by a series of letters, numbers, and/or underscores. They are case sensitive, so $awesome, $Awesome, $aWesome, and $AWesome are all different variables. PHP allows you to assign a value to a variable and then reassign
Can you even store data? The best way to introduce PHP variables is to start thinking about math. You might remember that there are these things called x and y variables that used to store numbers. If x = 2 and y = 5 then what is x + y? Just substitute 2 for x and 5 for y and you get 7. Variables are just storage containers. You can store any data type that you would like inside of them; we’ll look at strings and integers below. In PHP, variables start with a dollar sign, followed by either the