How could I construct a variable name from another variable? [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Construct a PHP variable name based on other variable values and static text
$value = '200';
$_200 = 'other';
How could I echo the contents of the second variable, getting its name from the first variable? So basically read the value of $value, prepend an _ and use it as a variable name.

Same as always.
echo ${'_'.$value};

Related

Can you add a variable to a file path? [duplicate]

This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
PHP String Concatenation With Variables [duplicate]
(1 answer)
Concatenating strings retrieved from form $_Post in php
(1 answer)
php String Concatenation, Performance
(12 answers)
Concatenate HTML string and variable in PHP [closed]
(3 answers)
Closed 2 years ago.
I was wondering if you can add a variable to a file-path.
Example:
path: C:\\users\\john\\school\\hardware.properties";
I want to make a variable of the word preceding the . (dot) - in this case, "hardware" - so the user can write a name in a form and search/get another property.
You can just make it a string and add the var into it
$name = $_POST['name']
$path = "C:/.../".$name.".properties"
I just make more sure what Jasper B want to write:
In your form you have some submit input and text input.
You will get datas from inputs so you will just access them like:
$name = $_POST["name"];
After you just put $name variable to path like:
$path = "C:\users\john\school\".$name.".properties";

How to Concatenate String and Variable with DEFINE in PHP [duplicate]

This question already has an answer here:
PHP: Constant name from a variable
(1 answer)
Closed 4 years ago.
I have defined a Constant viz
define('M_3', 'March');
if I
echo M_3;
The output is fine i.e. March.
But if do like this
$m3 = 3;
echo M_.$m3;
The output is M_3
Kindly let know where I am mistaken.
In order to call a constant with a variable you can use this:
echo constant("M_{$m3}");

How to write a Function to scan & remove similar words from a large variable using PHP? [duplicate]

This question already has answers here:
How to remove duplicate values from an array in PHP
(27 answers)
Closed 6 years ago.
I was trying to generate keywords using dynamic data & hence thought this function could be useful for others hence i have answered the question myself.
Here is the function named scan_dups:
function scan_dups($value){
$end = implode(', ',array_unique(explode(', ', $value)));
echo $end;
}
Now when using, just put all your values inside a variable and put that variable inside this function.
Eg:
$a = "a,b,c,d,e,g,f,g,g,s,d,gt,te,h,a";
echo scan_dups($a);
And the output would be:
a,b,c,d,e,g,f,s,gt,te,h

PHP variables to array [duplicate]

This question already has answers here:
How to get a variable name as a string in PHP?
(26 answers)
Closed 9 years ago.
I often see or have to convert a bunch of variables into an array like this:
$array = array("description"=>$description, "title"=>$title, "page"=>$page, "author"=>$author);
Basically, all array keys match the name of the variable that is being passed in. Is there a way to reference a variable name so that it can be passed into the array like so:
$array[varName($description)] = $description;
You could use compact [docs]:
$array = compact('description', 'title', 'page', 'author');
Each argument is a variable name and it will create an array with the key being the name and the value being the value of the variable with that name.
It's the other way round than your approach though.

php variable from variable [duplicate]

This question already has answers here:
Mixing a PHP variable with a string literal
(5 answers)
Closed 9 years ago.
I have a variable and I want to create a variable with that. I get the variable from database and put it together with some text and then I want another variable.
For exampel
$a = $ . "txt" . $d;
Try with this. It will create a variable from another one.
$a = ${'txt'.$d}
P.s. This is a question asked a couple of times. You might have found the answer simply by searching the issue on google.

Categories