Question

I'm trying to write a php script that will generate a variety of new php pages, but I'm finding that I'm unable to write a square bracket out. When I escape a square bracket in the same way as other characters (ie [ ) the leading \ is written to the new page, which results in code that doesnt work:

echo $row\['Value'\];

When I do not escape the bracket, the page fails, and the same thing happens when I try and substitute asc(91).

I have seen other examples that use code like $row->Value, but I tried that and it didn't work. If anyone can help me output a square bracket, or knows of another method by which I can fetch a value from a row without using one at all, I'd be very grateful

Was it helpful?

Solution

Your echo would appear as an array reference to PHP. Try this:

echo $row, "['Value'];"

assuming that you want the value of $row to be output, and not the literal text $row. If you want the literal text, (e.g. you're trying to build a PHP script on the fly), then either of these should do the trick:

echo '$row[\'Value\'];';
echo "\$row['Value'];";

OTHER TIPS

How about this:

echo sprintf("\$row['%s']", $value);  // either scenario
echo sprintf("%s['Value']", $row);  

Keep in mind that PHP automatically parses double quote strings ("), and tries to find variabels within. So, the bracket is probably not the issue, the $ variable prefix (coupled with the parser) probably is.

There are a couple other answers that work but I want to elaborate:

The "echo" construct can take a variable or a string. You can't echo a string to the screen in the same way that you do a variable. For example: echo hello; will not behave as you might think. You need to include it in quotes such as echo "hello";

You can also use single quotes. Single quotes and double quotes behave differently. For example:

$foo = "bar";
echo $foo;
echo "$foo";
echo '$foo';

The first will echo "bar", the second will also echo "bar" because PHP looks for variables in double quotes strings. The third will echo '$foo' because PHP does not try to do variable substitution in a single quoted string. So you can do (as @mark-b said):

echo "\$row['Value']";

or

echo '$row[\'Value\']';

Now, that $row->value syntax that you saw, is object notation. It is assuming that $row is an object and not an array. Objects are a whole other ballgame.

You're talking about code generation in your question, so I expect you also want to output the 'echo' statement in the generated code. Assuming you want to save the output into a file so it can be easily executed, you want to use something like fwrite or file_put_contents, I expect. You need to think in terms of strings, which can be a bit tricky when you're seeing code.

Something like this should work:

fwrite($fp, 'echo $row[\'Value\'];'."\n");

Note how the single and double quotes work. \n is resolved to a newline, but anything in the single quotes is treated as a string and is printed as is, apart from \', which should print a literal single quote in the output file.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top