PHP: How to add a variable inside other variable name? - php

How I can add a variable inside other variable definition?
Here is an example: $cars_list_ VARIABLE HERE [$car]

${'cars_list_'.$your_variable}[$var]
But it is extremely ugly. Use an array instead of multiple variables:
$vars_list[$your_variable][$car]
Obviously you need to change your existing code for that to work. But it's worth the time/effort.

While you could use ${'cars_list_'.$somevar}['car'], it is an extremely bad idea. Instead, you should have a multi-dimensional array that you can access with $cars_list[$somevar]['car']

You can do:
${'cars_list_' . 'other_var'} = "stuff";
echo $car_list_other_var;
Returns "stuff"

Related

How to use variable to get the for loop's value in PHP?

How to use a varible to store the value of the for loop?
for($i=1;$i<=$var;$i++)
How to get $i's value and store into another value and show its result?
I am a beginner of PHP and I want to improve my concept, I am very grateful if anyone can helps, Cheers!
Are you want dynamic variable or just a normal variable which will give you the out put like 12345678910
<?php
$var=10;
$a='';
for($i=1;$i<=$var;$i++){
$a.=$i;
}
echo $a;
?>
output: http://3v4l.org/2p8L6
or if you want dynamic variable then use following code to make a variable dynamic
${"num_" . $i} = $i;
output will be
$num_1=1;
$num_2=2;
$num_3=3;
etc
In plain English; PHP is updating the value stored in lopping variable automatically, and this is the whole idea behind using for loops, not only in PHP, but also in other programming languages.
Usually, you would want to use it (the variable $i) as a kind of a counter, and run code in the loop based on the iteration count.

Can I use PHP reserved variable as my variable?

PHP has list of reserved variables, visit here.
If i use one of the reserved keyword as my variable, it is working for me.
<?php
$_GET = 10;
echo $_GET;//10
?>
Please correct me, if my understanding is wrong?
"Predefined" is not the same as "reserved". PHP gives these variables default values, but you can still use their names for your own purposes. But you shouldn't, since it's poor style.
If you pass GET data into that file like http://example.com/example.php?id=1
Then it will conflict with your $_GET variable..
$_GET = Array { [id] => 1 }
After your declaration the value will be changed as
$_GET = 10
It will overwrite the old value..
I can't imagine a situation where you would want to do what you're suggesting.
in the case of $_GET, $_SESSION, they are arrays where you can assign a key value pair at will, that's what they're designed for.
$_SESSION["UID"]=username;
for example
$_GET is not a reserved variable.
$_GET is an associative array of variables passed to the current script via the URL parameters. Also is a superglobal. Read the documentation to learn how to use it
You should use like
$_GET['key']=10;//good
Doing
$_GET =10;//bad
will overwrite the value and it works but is a very wrong way to use a superglobal and they were not designed to work that way

PHP Function pulling an array that matches the parameter

$youtubes = array("lNT4H39G2rw","pF2_qvdm8DQ","_8ytwhhJwco","K16ZRFWR2Mc","9WuPxe7zc6Q","rXZIIclPnd0","J8ZwyN6E3_Q","OEWJbsh0z-4","o62-X0stdFM","aIIiww2Neq0","5TJc-VbNYg0","MYQa1Tgw_z8","alxzFm-bqug","UmI7oyllrlY","RGKFXDHFmn4");
function randomFromArray($data) {
global $$data;
echo $$data[rand(0,count($youtubes)-1)];
}
randomFromArray("youtubes");
I am trying to get this to work as a function, so I can enter the array name as a parameter. It is then supposed to echo a random entry from the array. The bit where it gets the random entry from array works on its own if I substitute it straight in, but I can't seem to get it working as a function.
Any help?
You're using the variable name $youtubes in your randomFromArray function in the call to count (but the variable is not available under that name there).
Btw., why don't you pass in (a reference to) the array instead of its name? Would be much tidier than using global $$data; The following code uses a reference to avoid copying the array (but remember that then, the outside array could be changed from inside the method):
$youtubes = // ...
function randomFromArray(&$data) {
echo $data[rand(0,count($data)-1)];
}
randomFromArray($youtubes);
dont call the function randomFromArray("youtubes"); in this way you call the function with youtubes parametar like a string. and inside the function itself you dont have a $youtubes variable. call the function like this randomFromArray($youtubes);
hope this would help
You are passing the string 'youtubes' into the function, not the array. You want to pass in the name of the array:
randomFromArray($youtubes);
$youtubes = array("lNT4H39G2rw","pF2_qvdm8DQ","_8ytwhhJwco","K16ZRFWR2Mc","9WuPxe7zc6Q","rXZIIclPnd0","J8ZwyN6E3_Q","OEWJbsh0z-4","o62-X0stdFM","aIIiww2Neq0","5TJc-VbNYg0","MYQa1Tgw_z8","alxzFm-bqug","UmI7oyllrlY","RGKFXDHFmn4");
function randomFromArray($data) {
echo $data[rand(0,count($data)-1)];
}
randomFromArray($youtubes);
There are some things fundamentally wrong here.
global $$data
Why is there a double $ sign? And where would this data be coming from?
echo $$data[rand(0,count($youtubes)-1)];
Your function doesn't know the variable $youtubes, since you have never defined it inside of the function. All your function knows is the $data variable that you are passing to it.
randomFromArray("youtubes"); You are passing a string to your function, rather then your array. You probably want this instead:
randomFromArray($youtubes); // Pointing to your actual array, rather then a string
Try reading up on PHP functions first before attempting to use them as you're lacking a lot of basic knowledge about them.
After you put a ; after global $$data, you can try this: $f = $$data; and then echo $f[rand(..)]; if you really want to use names instead variables as others suggested. And if you do that you can use the string (or $f) further in the function code.

PHP How do I retrieve this value?

I have a situation where I need to get the value of a variable, where the name od the variable is passed in a string in a second variable.
For example
$abc = 'the value I want';
$def = 'abc';
Where $def is the only variable name I can garrauntee being accessible. In this situation how can I get the value of $abc.
Cheers
echo $$def; does the job.
Use it very sparsely though, it makes code difficult to maintain!
PHP can have variable variables, like this:
${$def}
It also, incidentally, can have variable object and function names.
You can use "$$def" for this.
Examples:
http://php.net/manual/en/language.variables.variable.php

in php is there a way to dump "all" variable names with their corresponding value?

if the title seems too vague..
uhm i wanted to display every variable that i used to generate a page along with their variable names and values, is it possible and how?
foreach($_SESSION as $varname => $value) {
print "<b>".$varname."</b> = $value <br/>";
}
^the above sample is what i use to display all session variables, what if i need to display the variables i set to display the page? are they registered also in some form of an array or should i also echo them individually?
You can use get_defined_vars() which will give you an array of all variables declared in the scope that the function is called including globals like $_SESSION and $_GET. I would suggest printing it like so:
echo '<pre>' . print_r(get_defined_vars(), true) . '</pre>';
The easiest way to do this is with get_defined_vars().
From the Documentation
This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.
Producing a var_dump of this array will provide you with an extensive list.
var_dump( get_defined_vars() );
A couple other interesting functions in the same area are get_defined_constants() and get_included_files()...

Categories