I want PHP to echo a string stored in a variable, immediately followed by a set of square brackets containing some other text. This goes into a form that will be sent back to PHP, so PHP needs to be able to interpret the output of this command as an array item.
Here are the methods I've tried:
$var='string';
echo "string[0]" //works, but only because the variable isn't used
echo "$var[0]"; //PHP tries to treat the string as an array
echo "$var\[0]"; //the slash gets echoed
echo "$var[0\]"; //syntax error
echo "$var"."[0]"; //this is what I'm using now. It's very ugly and I want an alternative
Is there any way to make this work without breaking the string into chunks and concatenating them?
Here are the top-two ways I can think of to do this in a single output statement, the one you choose will end up being what fits your personal preference the most (and there are probably others available as well):
printf('%s[0]', $var);
echo $var . '[0]';
You can use the curly brace syntax. From the PHP manual documentation on Strings:
Any scalar variable, array element or object property with a string representation can be included via this syntax. Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }. Since { can not be escaped, this syntax will only be recognised when the $ immediately follows the {.
Surround the expression within curly braces (like so: {$var}), so PHP knows where the variable begins and ends.
$var = 'foo';
echo "{$var}[0]"; // => foo[0]
This way, you wouldn't have to worry even if the variable was a quoted array index like $var['foo'] either.
Make it explicit where the variable ends with curly brace syntax:
echo "{$var}[0]";
Related
I've been trying to find solution somewhere for this possibly simple fix but, I haven't been able to surprisingly.
How is it possible to stop PHP from assuming a variable is a part of a string. E.g.
The line of code is $string = "slfnnwnfkw49828323$dgjkt^7ktlskegjejke";
how do you stop PHP from thinking '$dgjkt' is a variable within the string when it's really a part of the full string as characters. Thanks
Use this string like $sting = 'slfnnwnfkw49828323$dgjkt^7ktlskegjejke'
You have to use ' instead of " otherwise php tries to find any variables inside your string
Read the manual.
The most important feature of double-quoted strings is the fact that
variable names will be expanded. See string parsing for details:
When a string is specified in double quotes or with heredoc, variables are parsed within it.
There are two types of syntax: a simple one and a complex one. The
simple syntax is the most common and convenient. It provides a way to
embed a variable, an array value, or an object property in a string
with a minimum of effort.
The complex syntax can be recognised by the curly braces surrounding
the expression.
I tried to do redirect with this syntax:
header("location: readMore.php?id=$post['post_id']");
But it didn't work. It worked only after someone suggested to put curly brackets around $post['post_id']!
The correct syntax is:
header("location: readMore.php?id={$post['post_id']}");
What does the curly brackets do in this case?
Quoting the manual:
When a string is specified in double quotes or with heredoc, variables are parsed within it.
There are two types of syntax: a simple one and a complex one. The simple syntax is the most common and convenient. It provides a way to embed a variable, an array value, or an object property in a string with a minimum of effort.
The complex syntax can be recognised by the curly braces surrounding the expression.
Your first code uses simple syntax, and your second code uses a complex one.
The manual does not explicitly state this, but whitespace in simple syntax seems to be an error, rendering your first code invalid. Complex syntax appears to support the same syntax as regular PHP does as far as I can see, but again this does not seem to be actually guaranteed anywhere.
String interpolation is quite flunky in general:
$a = [['derp']];
$b = $a[0];
// Works. It prints derp
echo "$b[0]";
// Doesn't work. It throws an error
echo "$b[ 0 ]";
// Works. It prints derp
echo "{$b[ 0 ]}";
// Doesn't work. It prints Array[0]
echo "$a[0][0]";
// Works. It prints derp
echo "{$a[0][0]}";
// Doesn't work. It prints { Array[0] }
echo "{ $a[0][0] }";
You get similar issues with $object -> foo and $object->foo->bar.
To me, that is pure madness. For that reason I've come to avoid double quoted strings whenever possible (the only thing I used them for are for escape sequences like "\n"). I instead use single quotes and string concatenation, like so:
header( 'location: readMore.php?id=' . $post[ 'post_id' ] );
This lets you use actual PHP syntax for variables without the horrible death trap that is string interpolation.
I came to this question to know more about constant interpolation syntax when those PHP "<<<" things are used to create multiline strings called Heredocs (which allow variable interpolation, unlike Nowdocs).
However, it seems there is no specific syntax for them, and therefore a simple workaround is to create a closure to do so. In here it is just an anonymous function assigned to a variable that will be invoked with parameters:
$int = 'intruder'; // Variable
define('con', '"smart"'); // Constant
// For complex interpolation:
// 1. Define a closure (anonymous function)
// 2. Assign it to a variable with a short name (e.g.: _ )
// 3. Invoke the function by calling the variable with parameters enclosed in ()
$_ = function ($val){return $val;};
$doc = <<<TXT
Hi there,
One day I caught this $int nearby.
I was then told that actually other {$_(con)} $int was there before.
So who came first, the chicken or the egg?
TXT; // Heredoc
echo $doc;
Output:
Hi there,
One day I caught this intruder nearby.
I was then told that actually other "smart" intruder was there before.
So who came first, the chicken or the egg?
You can test the above online on 3v4l. This was based on this answer with a few more examples with operations inside the interpolation brackets.
When you use double or single quotes, PHP will treat whatever is in it as a string unless you tell it that it’s a variable. PHP understands anything after { followed by $ as a variable and treats it as such. Here is an example:
$Text = "XYz";
echo "name-{$Text}";
The other alternative method is to use concatenation. Here is an example:
header("location: readMore.php?id=" . $post['post_id']);
Brackets allow PHP to read what's inside as a variable. You can do that this way too:
header("location: readMore.php?id=" . $post['post_id']);
PHP's simple string interpolation doesn't recognize quoted array keys, which your example demonstrates perfectly. In fact, the correct way to write this is exactly opposite depending on which syntax used: simple vs complex.
Simple syntax - Wrong
Quoted keys cannot be parsed.
header("location: readMore.php?id=$post['post_id']");
Simple syntax - Right
The unquoted string is the associative array key.
header("location: readMore.php?id=$post[post_id]");
Complex syntax - Wrong
It will work, but only if post_id is a defined constant. If not, you'll get a PHP warning.
header("location: readMore.php?id={$post[post_id]}");
Complex syntax - Right
Written just like outside the string.
header("location: readMore.php?id={$post['post_id']}");
To quote the manual on the complex syntax:
// Works, quoted keys only work using the curly brace syntax
echo "This works: {$arr['key']}";
I'd recommend using complex (curly brace) syntax if using quoted keys. And you really should be using them, because outside the string interpolation unquoted keys are actually constants. It's too bad the simple syntax won't allow them, because it makes code reviews and updating old PHP code more difficult.
I'm doing a basic quiz . I don't understand the last part:
$_SESSION["x{$user['serial']}"]
What exactly does x{5} array value mean?
x isn't defined anywhere, and in general object literals should be like {x: 5} – some help with this would be great.
session_start();
function setData($data){
if(is_array($data)){
$x = 1;
foreach ($data as $key => $value) {
$_SESSION["x$value"] = $x;
$x++;
}
}
}
$user = array('id'=>3,'serial'=>5);
setData($user);
echo $_SESSION["x{$user['serial']}"];
The curly brackets is used for expanding complex expressions inside double-quote strings, like arrays.
So, in your example, x{$user['serial']} will expand to "x5". You can also write x$value as x{$value}, both with the same result.
BTW that object literal is for Javascript, not PHP :)
You can't access (all kinds of) arrays in a double quoted string.
For instance:
echo "x$user['serial']";
This results in:
PHP Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in php shell code on line 1
In other words, access array elements in an indexed array ("$user[0]") works, but accessing associative elements with a string literal doesn't.
doesn't work because it is too complicated to parse. By using accolades, you say the part should be interpreted separately.
So:
echo "x{$user['serial']}";
is equivalent to:
echo "x".$user['serial'];
The resulting code is thus:
echo $_SESSION["x".$user['serial']];
But more simple.
Given your sample $user, the result is that you access x5 from $_SESSION.
It is not x{5} because it doesn't mean anything. The expression:
"x{$user['serial']}"
is a string enclosed in double quotes. Inside strings enclosed in double quotes, PHP does what in other languages is called "variables interpolation". In plain English, it searches for variable names and replaces them with their values.
It can easily recognize variable names in strings and replace them but the things become more difficult when you want to use more complex expressions like arrays or objects. It could try to guess what the programmer wants but this is not a solution.
That's why PHP finds only simple variables inside the double quotes and relies on the programmer to signal when they want to insert complex things like arrays and objects. And the programmer uses curly braces ({ and }) to tell PHP about the complex expressions inserted in strings.
Check the documentation about string parsing on PHP manual.
Without using the curly braces, in the string "x$user['serial']" PHP recognizes the variable $user and ignores the ['serial'] part. It attempts to replace $user with its string representation and because it is an array, the final string becomes "xArray['serial']" and that is not what you want.
Using curly braces allows the programmer to write something like "x{$user[$field]}" given the variable $field was initialized forehand with the string 'serial'.
If $user['serial'] == 5 then the final outcome is $_SESSION["x5"].
Curly braces can always be used to enclose the variable names in double quoted strings. They are useful, for example, in this situation:
$sep = '--';
$str = "a{$sep}b";
// echo($str) will produce: a--b
Without curly braces around the variable $sep, the string looks like "a$sepb", PHP finds inside it the variable name $sepb and because no such variable exists, the value of $str becomes "a" and this is not what we intended.
I'm having hard time with quotation marks. I have this line of code;
echo "<a href='$bName'_read.php?bid='$bid'&id='$next_id[id]'>NEXT</a>";
with 3 variables, $bName,$bid, and $next_id[id].
There is something wrong with the quotations I've used. I also tried this;
echo "<a href='".$bName."_read.php?bid=".$bid."&id=".$next_id['id']."'">";
but it's still not working.
Can anyone explain how quoting works in this case please?
You don't need to put single quotes around every PHP variable. It should make sense in HTML instead, for example;
echo "<a href='{$bName}_read.php?bid={$bid}&id={$next_id['id']}'>NEXT</a>";
You need curly braces ({}) around object and array variables, but it is also useful for normal variables. Also, the array index should be in quotes as it is a string (not required for integer indexes).
Additionally, I changed the ampersand (&) to & as & signifies the start of a special character code (just like &), so although in this case it wouldn't be a problem it is best practice to put the HTML char code in, even in a URL.
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
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'];";
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.