PHP add Value to Variable in Variable before echo
up vote
-1
down vote
favorite
i am looking for a solution:
$txt = "Welcome to ".$place." City!";
$place = "New York";
echo $txt; // should be "Welcome to New York City!"
The variable "$place" can only be declared after the variable "$txt".
Is that possible?
Thanks
php variables echo
add a comment |
up vote
-1
down vote
favorite
i am looking for a solution:
$txt = "Welcome to ".$place." City!";
$place = "New York";
echo $txt; // should be "Welcome to New York City!"
The variable "$place" can only be declared after the variable "$txt".
Is that possible?
Thanks
php variables echo
When$txt
is set, it is concatenating"Welcome to "
, the value of$place
, and" City!"
to form a new string. So no, you cannot. Why do you want to do this?
– Jordan S
Nov 21 at 19:45
2
Can you explain the goal here, your example isn't possible, but I bet whatever you actually trying to achieve is
– dan08
Nov 21 at 19:48
You can't define a variable before using it.
– Funk Forty Niner
Nov 21 at 21:10
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
i am looking for a solution:
$txt = "Welcome to ".$place." City!";
$place = "New York";
echo $txt; // should be "Welcome to New York City!"
The variable "$place" can only be declared after the variable "$txt".
Is that possible?
Thanks
php variables echo
i am looking for a solution:
$txt = "Welcome to ".$place." City!";
$place = "New York";
echo $txt; // should be "Welcome to New York City!"
The variable "$place" can only be declared after the variable "$txt".
Is that possible?
Thanks
php variables echo
php variables echo
asked Nov 21 at 19:42
SteveO24
11
11
When$txt
is set, it is concatenating"Welcome to "
, the value of$place
, and" City!"
to form a new string. So no, you cannot. Why do you want to do this?
– Jordan S
Nov 21 at 19:45
2
Can you explain the goal here, your example isn't possible, but I bet whatever you actually trying to achieve is
– dan08
Nov 21 at 19:48
You can't define a variable before using it.
– Funk Forty Niner
Nov 21 at 21:10
add a comment |
When$txt
is set, it is concatenating"Welcome to "
, the value of$place
, and" City!"
to form a new string. So no, you cannot. Why do you want to do this?
– Jordan S
Nov 21 at 19:45
2
Can you explain the goal here, your example isn't possible, but I bet whatever you actually trying to achieve is
– dan08
Nov 21 at 19:48
You can't define a variable before using it.
– Funk Forty Niner
Nov 21 at 21:10
When
$txt
is set, it is concatenating "Welcome to "
, the value of $place
, and " City!"
to form a new string. So no, you cannot. Why do you want to do this?– Jordan S
Nov 21 at 19:45
When
$txt
is set, it is concatenating "Welcome to "
, the value of $place
, and " City!"
to form a new string. So no, you cannot. Why do you want to do this?– Jordan S
Nov 21 at 19:45
2
2
Can you explain the goal here, your example isn't possible, but I bet whatever you actually trying to achieve is
– dan08
Nov 21 at 19:48
Can you explain the goal here, your example isn't possible, but I bet whatever you actually trying to achieve is
– dan08
Nov 21 at 19:48
You can't define a variable before using it.
– Funk Forty Niner
Nov 21 at 21:10
You can't define a variable before using it.
– Funk Forty Niner
Nov 21 at 21:10
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
It's unclear what you're trying to accomplish. But this is pretty close to your example and works as expected:
$txt = "Welcome to %s City!";
$place = "New York";
printf($txt, $place); // should be "Welcome to New York City!"
https://3v4l.org/jEQvZ
add a comment |
up vote
0
down vote
You can't do that directly. The string is concatenated using the value of $place
at the time of assignment so any changes to $place
will not be reflected in the string. You can emulate something like this by writing a function that will generate the string once you know the place it should reference.
function welcome($place) {
return 'Welcome to '.$place.' City!';
}
$place = 'New York';
echo welcome($place);
add a comment |
up vote
0
down vote
Yes it's possible.
Just create a placeholder and str_replace it.
Notice I changed "
to '
.
If you use "
it will read $place as a variable. With '
$place is string and can be replaced later in the code.
$txt = 'Welcome to $place City!';
$place = "New York";
echo str_replace('$place', $place, $txt);
https://3v4l.org/DVtQa
Thank you! It works fine!
– SteveO24
Nov 21 at 19:57
1
@SteveO24 no problem. Feel free to accept the answer that you find the best solution. (Not implying that it's mine, they are all very similar)
– Andreas
Nov 21 at 20:00
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
It's unclear what you're trying to accomplish. But this is pretty close to your example and works as expected:
$txt = "Welcome to %s City!";
$place = "New York";
printf($txt, $place); // should be "Welcome to New York City!"
https://3v4l.org/jEQvZ
add a comment |
up vote
1
down vote
It's unclear what you're trying to accomplish. But this is pretty close to your example and works as expected:
$txt = "Welcome to %s City!";
$place = "New York";
printf($txt, $place); // should be "Welcome to New York City!"
https://3v4l.org/jEQvZ
add a comment |
up vote
1
down vote
up vote
1
down vote
It's unclear what you're trying to accomplish. But this is pretty close to your example and works as expected:
$txt = "Welcome to %s City!";
$place = "New York";
printf($txt, $place); // should be "Welcome to New York City!"
https://3v4l.org/jEQvZ
It's unclear what you're trying to accomplish. But this is pretty close to your example and works as expected:
$txt = "Welcome to %s City!";
$place = "New York";
printf($txt, $place); // should be "Welcome to New York City!"
https://3v4l.org/jEQvZ
answered Nov 21 at 19:55
dan08
5,30231430
5,30231430
add a comment |
add a comment |
up vote
0
down vote
You can't do that directly. The string is concatenated using the value of $place
at the time of assignment so any changes to $place
will not be reflected in the string. You can emulate something like this by writing a function that will generate the string once you know the place it should reference.
function welcome($place) {
return 'Welcome to '.$place.' City!';
}
$place = 'New York';
echo welcome($place);
add a comment |
up vote
0
down vote
You can't do that directly. The string is concatenated using the value of $place
at the time of assignment so any changes to $place
will not be reflected in the string. You can emulate something like this by writing a function that will generate the string once you know the place it should reference.
function welcome($place) {
return 'Welcome to '.$place.' City!';
}
$place = 'New York';
echo welcome($place);
add a comment |
up vote
0
down vote
up vote
0
down vote
You can't do that directly. The string is concatenated using the value of $place
at the time of assignment so any changes to $place
will not be reflected in the string. You can emulate something like this by writing a function that will generate the string once you know the place it should reference.
function welcome($place) {
return 'Welcome to '.$place.' City!';
}
$place = 'New York';
echo welcome($place);
You can't do that directly. The string is concatenated using the value of $place
at the time of assignment so any changes to $place
will not be reflected in the string. You can emulate something like this by writing a function that will generate the string once you know the place it should reference.
function welcome($place) {
return 'Welcome to '.$place.' City!';
}
$place = 'New York';
echo welcome($place);
answered Nov 21 at 19:49
jfadich
3,0381519
3,0381519
add a comment |
add a comment |
up vote
0
down vote
Yes it's possible.
Just create a placeholder and str_replace it.
Notice I changed "
to '
.
If you use "
it will read $place as a variable. With '
$place is string and can be replaced later in the code.
$txt = 'Welcome to $place City!';
$place = "New York";
echo str_replace('$place', $place, $txt);
https://3v4l.org/DVtQa
Thank you! It works fine!
– SteveO24
Nov 21 at 19:57
1
@SteveO24 no problem. Feel free to accept the answer that you find the best solution. (Not implying that it's mine, they are all very similar)
– Andreas
Nov 21 at 20:00
add a comment |
up vote
0
down vote
Yes it's possible.
Just create a placeholder and str_replace it.
Notice I changed "
to '
.
If you use "
it will read $place as a variable. With '
$place is string and can be replaced later in the code.
$txt = 'Welcome to $place City!';
$place = "New York";
echo str_replace('$place', $place, $txt);
https://3v4l.org/DVtQa
Thank you! It works fine!
– SteveO24
Nov 21 at 19:57
1
@SteveO24 no problem. Feel free to accept the answer that you find the best solution. (Not implying that it's mine, they are all very similar)
– Andreas
Nov 21 at 20:00
add a comment |
up vote
0
down vote
up vote
0
down vote
Yes it's possible.
Just create a placeholder and str_replace it.
Notice I changed "
to '
.
If you use "
it will read $place as a variable. With '
$place is string and can be replaced later in the code.
$txt = 'Welcome to $place City!';
$place = "New York";
echo str_replace('$place', $place, $txt);
https://3v4l.org/DVtQa
Yes it's possible.
Just create a placeholder and str_replace it.
Notice I changed "
to '
.
If you use "
it will read $place as a variable. With '
$place is string and can be replaced later in the code.
$txt = 'Welcome to $place City!';
$place = "New York";
echo str_replace('$place', $place, $txt);
https://3v4l.org/DVtQa
answered Nov 21 at 19:49
Andreas
14.7k31441
14.7k31441
Thank you! It works fine!
– SteveO24
Nov 21 at 19:57
1
@SteveO24 no problem. Feel free to accept the answer that you find the best solution. (Not implying that it's mine, they are all very similar)
– Andreas
Nov 21 at 20:00
add a comment |
Thank you! It works fine!
– SteveO24
Nov 21 at 19:57
1
@SteveO24 no problem. Feel free to accept the answer that you find the best solution. (Not implying that it's mine, they are all very similar)
– Andreas
Nov 21 at 20:00
Thank you! It works fine!
– SteveO24
Nov 21 at 19:57
Thank you! It works fine!
– SteveO24
Nov 21 at 19:57
1
1
@SteveO24 no problem. Feel free to accept the answer that you find the best solution. (Not implying that it's mine, they are all very similar)
– Andreas
Nov 21 at 20:00
@SteveO24 no problem. Feel free to accept the answer that you find the best solution. (Not implying that it's mine, they are all very similar)
– Andreas
Nov 21 at 20:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419450%2fphp-add-value-to-variable-in-variable-before-echo%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
When
$txt
is set, it is concatenating"Welcome to "
, the value of$place
, and" City!"
to form a new string. So no, you cannot. Why do you want to do this?– Jordan S
Nov 21 at 19:45
2
Can you explain the goal here, your example isn't possible, but I bet whatever you actually trying to achieve is
– dan08
Nov 21 at 19:48
You can't define a variable before using it.
– Funk Forty Niner
Nov 21 at 21:10