How come when I echo $p, the variable which Im trying to fetch using this loop doesnt get displayed in the path.
$name_image2="picture.jpg";
for ($i=2; $i<=$nr_of_pics; $i++){
$img='name_image'.$i;
echo $$img; gives me this: 'picture.jpg' which is correct.
but when echoing $p like this:
$p="/SV/main/temp_images/$$img"; echo $p;
I get this: SV/main/temp_images/name_image2 --> the variable 'name_image2' doesnt get called here, why?
I want it to say: SV/main/temp_images/picture.jpg
Thanks
$p = "/SV/main/temp_images/" . $$img;
Ought to fix it.
Also, I would recommend learning how to use arrays. They are a much better way to have a set of data instead of variable variables.
Try $p="/SV/main/temp_images/{${$img}}";
When PHP is parsing the string and comes to a $, it looks at the next character to see if it makes a valid variable name. If not, it moves on. In this case, that means that the second $ is correctly interpreted, but the first one has already been passed by. The answer is to enclose the inner expression in brackets, so that it will be parsed before the outer one is.
Related
for($i=0;$i<=9;$i++){
$try[$i] = $_POST[''echo $i;'.AA1.'];
}
But i am getting an error like unexpected echo when i try to use a variable in post methods braces.
Please give me a more optimum solution if you guys find out.
Use this:
for($i=0;$i<=9;$i++){
$try[$i] = $_POST[$i.'AA1'];
}
Make sure that the following indexes exist in your POST aray: 0AA1 -> 9AA1.
The error you are getting is caused by several issues:
using echo inside []. (You don't need to echo a variable's value to use it.)
wrong concatenation
I am trying to output array content in a for loop. When I use the literal name of the array, it works. When I try to use a variable assigned to that name it fails. I need to use a variable to allow the page to display the correct content based on user's choice.
the array, saved as an include, is 2-dimensional of the form:
$newsArray2012 = array(
array(string_a, string_b, stringc),
array(string_a, string_b, stringc),
array(string_a, string_b, stringc), etc.,
);
The for loop looks like this:
for ($row = 0; $row < 5; $row++) {
echo "<p class='portrait'><a name='".$newsArray2012[$row][1]."'></a>\n
<em>".$newsArray2012[$row][0]."</em></p>\n
<img src='/Pictures/".$newsArray2012[$row][1]."' width='72' height='80' alt='' />\n";
echo $newsArray2012[$row][2]."\n";
}
That works.
I've assigned $newsCurrent = newsArray2012. When I substitute $newsCurrent for $newsArray2012 in the for loop, I do not get any output. In testing, I found that print_r($$newsCurrent) will dump the full array and even though that's goofy, I tried the $$ version in the for loop but it does not work there.
Where have I gone wrong?
OK, since $$newsCurrent worked with print_r, but not in the for loop, I assumed that PHP had a problem with the $$. Since it allowed the $$ in print_r, I made the leap that it would accept a re-assignment as follows:
$pageBios = $$newsCurrent;
This actually worked. I placed $pageBios in the for loop and had success. I'm not happy with it because the $$ format is not right - at least I've never seen it. So while I need to move forward due to deadlines, I would really like to clean this up - so please feel free to show me a non-moronic approach. Thanks.
Right I had to make a variable as shown below.
$questionID = '$quizinfo[\'Q' . $ques_num . '_ID\']';
But now the code wont run when shown anywhere it shows like $quizinfo['Q1_ID'] Instead of getting the variable that the php got earlier that behind it.
I need to be done like that as there 15 questions in a quiz and the ids I held and I change the number every time to get the new ID number but it not getting them how do I fix it?
checking it I put in.
echo $quizinfo['Q1_ID'];
And it worked correctly so what do I need to do to make it work?
Try:
$questionID = $quizinfo['Q' . $ques_num . '_ID'];
It should work.
When you write:
$questionID = '$quizinfo[\'Q' . $ques_num . '_ID\']';
$quizinfo[…] is not interpreted. It is taken as a string.
See also:
Php variables inside strings
PHP - concatenate or directly insert variables in string
PHP: variables in strings without concatenation
PHP: Beware of Variables Inside Strings
Mixing PHP variable with string literal
Since it is just php, why do you escape it?
$questionID = $quizinfo['Q' . $ques_num . '_ID'];
should do it. Also, You have ]' which should be replaced by '] at the end.
I think you are looking to use a variable for your array index:
$index = "Q{$ques_num}_ID";
echo $quizinfo[$index];
The problem you're experiencing is that you've turned the whole expression into a string with a number inside it, and so no array look-up was being performed.
As an aside: having indices that are human-readable is nice for humans, but not necessary for a computer. Unless you have a particular need to do it this way, I'd change this so it is just numerically indexed - it'll simplify your code.
I have a long PHP file and I want to copy all the variable names only and build an insert sql query. Is there a way where I can search for a pattern using regular expression and concatenate the find result till I collected all the variable and spit it out in a statement?
I am using TextMate and am familiar with regular expression search. Regex search result give $0,$1 and so forth argument. Do not know if this possible though. Solution in any editor will do not just text mate.
I have just too many variable (+100) don't feel like copy every single one. Here my sample file
$ID = $_POST['id'];
$TXN_TYPE = $_POST['txn_type'];
$CHARSET = $_POST['charset']
$CUSTOM = $_POST['custom'];
You could try something with get_defined_vars(). However this function also lists GLOBAL vars. You can use this snippet to remove them if you don't want them and display only the vars you defined
$variables = array_diff(get_defined_vars(), array(array()));
However this snippet generates Notices and I haven't found a way to solve them yet.
If you've only got $_POST variables you can loop through the $_POST array itself
You create the SQL programmatically while looping through the array.
My own solution is, do the inverse. It is not probably possible.
Leave only the variable names Remove all the rest. Use
[space].+ regex to remove everything that is after the variable name.
clean the file so that only variable names are left. then do a couple more find and replace to bring the variable name in the form you want.
If you're looking to match only the variable names (not the $_POST array indices), then the regular expression is pretty much provided in the PHP documentation:
\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
This will, of course, include $_POST, but that should be easy enough to remove. If not, you could do it with negative lookahead (if TextMate supports it):
\$(?!_POST($|[^a-zA-Z0-9_\x7f-\xff]))[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
I've got this problem when trying to construct a variable name
(which should output a corresponding array element.)
I start with setting the variables for testing purposes, normally the piece variable would hold the first element of an array $piece.
$wordnumber = 0;
$piece[0] = "forever";
I later echo them right before my problem to see if they're still ok.
echo "$piece[0]";
echo "$wordnumber";
The output is ok.
forever0
But then comes the problem, as I'm trying to make a function that automatically handles every single array element, so I want it to construct the next corresponding variable every time. However somehow it has no value after construction.
$name = ${'piece[' . $wordnumber . ']'};
echo "$name";
outputs nothing...
I've tried a lot of different formatting, I really don't know why I'm failing so hard here.
The code isn't part of any function right now btw.
Update:
$name = $piece[$wordnumber] solves the problem
I'm curious though why my previous formatting didn't work as expected.
Update: Question solved by André, the problem was that $piece[0] wasn't actually part of an array. So $piece was the actual variable. After storing an actual array $piece = array("Redish", "Yellow", "Green"); at start and using global $piece; in my function everything started working like a charm.
I haven’t tested the code, but I think your first approach didn’t work because your variable’s name is “piece”, not “piece[0]”. In other words, ${'piece'}[0] should work, but ${'piece[0]'} is wrong. Try add this in the very beginning of your script and PHP should display you some complaints:
error_reporting(E_ALL);
ini_set("display_errors", 1);
Try just $name = $piece[$wordnumber]; and echo $name
this will output "forever"