View Full Version : how can I prepend a string?
james438
02-25-2008, 02:34 AM
how do you prepend a value to a string.
For example
$a='great';
prepend 'pop ' to $a;
$a == 'pop great';
sort of like the reverse of .=
So I don't get what you mean.
When you use .= have the text go in the beginning not front?
james438
02-25-2008, 02:59 AM
yes.
$a='what';
$a.=' if?';
//$a becomes 'what if?';
What I want to do is the opposite. I want to know if there is some sort of function or way that I can prepend the string so that you can add 'so ' to $a to make $a become 'so what if?'.
Well, why dont you just:
$a='what';
$a.=$a.' if?';
//$a becomes 'what if?';
james438
02-25-2008, 03:23 AM
because $a already has a pregenerated value. Nice thought though. I just found this simple answer though ;)
$a='if?';
$b='what ';
$a="$b$a";
//$a becomes "what if?"
Isn't that just like what I posted?
EDIT: Try what I posted.
james438
02-25-2008, 03:30 AM
No. You appended the string. I prepended the string. Since it is short here is the code that I am using it in:
<?php
$a=0;$b='0000000';
foreach ($_POST as $field[] => $value[])
{
if (strpos($field[$a],'check')===0)
else $value[$a]="$b$value[$a]";
echo "$field[$a] = $value[$a]<br>";$a++;
}
echo "pp $_POST[loot_Lasl] pp";
?> to do what you suggested would erase the passed value, which is why I needed to prepend the string.
P.S. the script is only in its infancy, so no need to critique it. I am merely designing it right now.
o then you would judt:
$a='what';
$a=$a.' if?';
//$a becomes 'what if?';
$a = ' if?';
$a = 'What' . $a;$a .= $b is just a short form of $a = $a . $b.
james438
02-25-2008, 03:41 AM
that works too :)
djr33
02-25-2008, 04:09 AM
Though you now have your answer, you've entirely overthought this:
$a = 'abc'; $b = 'xyz';
$a.$b becomes 'abcxyz'
To insert a space, that is another step, using $a.' '.$b
And of course you'll have to pick what value is $a, and what is $b.
In short, using the . operator will add two strings.
And as Twey has pointed out, the .= is just a shortcut to $a = $a.$b;. There is no shortcut for using =., and equivalent of $a = $b.$a;
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.