php variable from variable [duplicate] - php

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.

Related

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

Meaning of extra $ variable in PHP [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 7 years ago.
I want to know about meaning of $$val; what is the actual meaning is?i tried to find meaning of this in google but not understand properly. Please help me in this situations.
For example: suppose i have one variable which has $$value;
meaning of $$value?
You didn't put the language, but I'll assume you mean PHP
That's a variable variable.
That means you ware asking for the value of the variable whose name.is the first variable.
Here's an example, since that's quite confusing:
$foo = "Hi";
$bar = "world";
$world = "Hello!";
echo $$bar; // "Hello!"
php fiddle: http://ideone.com/Ve4YOO
Reference: https://secure.php.net/manual/en/language.variables.variable.php

Using an array without initialization in PHP [duplicate]

This question already has answers here:
Should an array be declared before using it? [closed]
(7 answers)
Closed 7 years ago.
In most languages, I have to initialize an associative array before I can use it:
data = {}
data["foo"] = "bar"
But in PHP I can just do
data["foo"] = "bar"
Are there any repercussions to doing this? Is this "the right way" to write PHP?
Is the same, but is not a good idea, the next is a copy-paste from php documentation.
If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array. This practice is however discouraged because if $arr already contains some value (e.g. string from request variable) then this value will stay in the place and [] may actually stand for string access operator. It is always better to initialize variable by a direct assignment.
Basically it's the same, and no you won't find any problem or repercussion.
But if you like you can do this:
$a = array();
You can read more in the PHP page

PHP strings replace in a file [duplicate]

This question already has answers here:
Redefining constants in PHP
(5 answers)
Closed 8 years ago.
I have a php file like this.
define('TEXT_ONE', 'testvalue1');
define('TEXT_TWO', 'testvalue12');
define('TEXT_THREE', 'testvalue13');
define('TEXT_FOUR', 'testvalue14');
define('TEXT_FIVE', 'testvalue15');
define('TEXT_SIX', 'testvalue16');
define('TEXT_SEVEN', 'testvalue17');
define('TEXT_EIGHT', 'testvalue18');
define('TEXT_NINE', 'testvalue19');
define('TEXT_TEN', 'testvalue10);
define('TEXT_ELEVEN', 'testvalue11');
I want to change some of the defined value through php code.
for ex:- I Want to change above file to
define('TEXT_ONE', 'newtext1');
define('TEXT_TWO', 'newtext12');
define('TEXT_THREE', 'newtext13');
define('TEXT_FOUR', 'newtext14');
define('TEXT_FIVE', 'newtext15');
define('TEXT_SIX', 'newtext16');
define('TEXT_SEVEN', 'newtext17');
define('TEXT_EIGHT', 'newtext18');
define('TEXT_NINE', 'testvalue19');
define('TEXT_TEN', 'newtext10);
define('TEXT_ELEVEN', 'testvalue11');
Can any one help me?
Thanks
define — Defines a named constant.
As the name suggests, that value cannot change during the execution of the script

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

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};

Categories