views:

79

answers:

8

This should be simple to answer. When I have a variable, say $id, and in a string, I want it between two underlines. Something like this:

$id = 1;
$myString = "row_$id_info";

Now, php will see "row_" and the variable $id_info - And that's not what I want.

So my question is plain: How do i break an in-string variable in php?

Thanks for all replies

+5  A: 

You mean this:

$id = 1;
$myString = "row_" . $id . "_info";

Or

$myString = "row_{$id}_info";

See: PHP String Concatenation

Sarfraz
No.............
Codemonkey
@Codemonkey: Then?
Sarfraz
@Codemonkey: can you make up your mind whether the answers here are correct or wrong? They all work **exactly** as you intend.
BoltClock
I wrote no before you updated your reply with the correct answer. Codaddict gave me the correct reply first, and I intent to mark his reply as correct as soon as the timer allows me.
Codemonkey
@Codemonkey: Your question wasn't clear and most came up with the same answer like me. Later i posted another possibility which is what you were looking for but before me Codadict posted it :)
Sarfraz
I asked how to break an in-string variable. Obviously putting the variable out of the string wasn't what I asked for :P At least, that's how I see it
Codemonkey
+1  A: 

$myString = "row_".$id."_info";

Thomas Clayson
No.............
Codemonkey
whats wrong with that?! what do you want?
Thomas Clayson
That's obvious. See the answer marked as correct.
Codemonkey
This is a completely valid and accurate answer. Codemonkey is being stubborn.
Capt Otis
+4  A: 

In such cases enclose the variable in {}

$id = 1;
$myString = "row_{$id}_info"; // $myString is row_1_info
codaddict
Perfect!.......
Codemonkey
if it't possible, you should use string concatenation instead of this - it's a bit faster and (and thats the important point) it's more readable (in my optinion)
oezi
+1  A: 

Actually that should do the trick since quotes are parsed for varaibles and you actually supplied the name. However concatenating is what you're asking for.

$id = 1;
$myString = 'row_'.$id.'_info';
Camoy
please use code-formatting (mark the code-block and press the "101010"-button above the input-editor)
oezi
+1  A: 
$id = 1;
$myString = "row_{$id}_info";
Denis
+1  A: 

Use curly braces:

$myString = "row_{$id}_info";
Fabian
+1  A: 
$myString = sprintf("row_%d_info", $id);

Using this $id is also checked against being numeric.

halfdan
Good to know! Thanks.
Codemonkey
A: 
$myString = "row_{$id}_info";

or, "better" (more readable) and faster:

$myString = "row_".$id."_info";

EDIT:

plase take a look at this link - putting variables directly into a string is up to 8% slower than string concatenation. this isn't the best reason for not using variables in strings, but it is one - the best reason is a better readable code if you use string concatenation (but thats only my opinion)

oezi
-1 for "faster"
stereofrog
If I'm not mistaken, putting variables in-string the way I do is slower to execute than sowing the variables together with "." - Particularly in loops
Codemonkey
@ stereofrog: please take a look at my edit...
oezi
+1, nothing wrong with pointing out an alternative idiom is faster, though I find interpolated variables more readable myself.
Paul Dixon
@oezi: How can the best reason be the code is more readable? The entire point of interpolated variables is that it's more readable. String concatenation looks like crap. (Not considering personal opinions)
Codemonkey
@Codemonkey - disagree string concatenation is brilliantly readable. Scanning through code you will be able to see concatenated strings much easier than including variables in-line **especially** with {} around them (imo)
Thomas Clayson
@oezi: whether concat is "faster" or not (it is not) has nothing to do with the question. The -1 was for being off topic.
stereofrog
@stereofrog: so it would be better to say "this are the possibilities you have" and leave the author of the question in the dark about which he should use? whats wrong with giving additional information (which some of the others here have done, too - without getting a downvote (or isn't halfdans "checked against being numeric" offtopic, too?))
oezi
@oezi: this "interpolation vs concat" stuff is old stupid bullshit - this is what is wrong about it.
stereofrog