This question already has answers here:
How do I include a yet to be defined variable inside a string? PHP
(6 answers)
Closed 3 years ago.
I currently have a row in a mysql table that has a variable in it, $item1. I wanted to know if there is a way to have it act as a variable. It would be easier for the project I'm working on if these variables could be stored in the mysql table and still remain dynamic.
When trying to echo this row, $item1 comes out as $item1, instead of acting as a variable.
So the mysql table looks like so
And the code looks like this.
$item1 = "Test";
echo "$row[1];"
As stated instead of getting "Hello Test"
I am getting
"Hello $item1"
Is there a way around this?
What you are looking for is the eval function of php. But it has nothing to do with mysqli
$item1 = "Test";
$itemFormDatabase = "$item1";
echo eval($itemFormDatabase)
Result
Test
But a note of caution: eval just executes everything as php code. If an attacker can modify the value given to it, he can do anything
Related
This question already has answers here:
What is the proper way to declare variables in php?
(5 answers)
Closed 9 months ago.
The first appearance to this variable $totpro in my code is this way
$totpro = $totpro + $row['profitloss'];
I want to use it to sum all profits, however, I receive this warning message on running
Warning: Undefined variable $totpro
but if I put this code before the previous code it runs with no problems
$totpro = "0";
I don't like using that code to declare the function, it tried
String $totpro
but unexpectedly it didn't work. Now tell me how to define $totpro without to have to use $totpro = "0";
If you are summing numbers, the initial declaration should set the value to 0 (i.e. a number):
$totpro = 0;
You tried "0", which is a string. Technically this will work, but it is not the best way.
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
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
This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 7 years ago.
I'm trying to link several variables from a database into html. All the data is stored in the DB, however I can't figure out how to link the variables through HTML. Below is my code that I have tried, but doesn't work properly.
echo 'test';
I know the code works properly if I just do something like this (it does return the name):
echo $row["name"];
So why doesn't it work properly with the + $row["name"] + in it? It works perfect as long as I don't try and add data with the +'s.
Thank you!
this is the correct way to write this
echo 'test';
Use a . in stead of +.
And on a sidenote, if the data in the db isn't yet escaped, also use htmlspecialchars() on $row["name"]
Try like this.
echo "<a href='".$row['name']."'>test</a>";
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.