PHP add Value to Variable in Variable before 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
add a comment |
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 '18 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
– Dan
Nov 21 '18 at 19:48
You can't define a variable before using it.
– Funk Forty Niner
Nov 21 '18 at 21:10
add a comment |
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 '18 at 19:42
SteveO24SteveO24
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 '18 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
– Dan
Nov 21 '18 at 19:48
You can't define a variable before using it.
– Funk Forty Niner
Nov 21 '18 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 '18 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
– Dan
Nov 21 '18 at 19:48
You can't define a variable before using it.
– Funk Forty Niner
Nov 21 '18 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 '18 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 '18 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
– Dan
Nov 21 '18 at 19:48
Can you explain the goal here, your example isn't possible, but I bet whatever you actually trying to achieve is
– Dan
Nov 21 '18 at 19:48
You can't define a variable before using it.
– Funk Forty Niner
Nov 21 '18 at 21:10
You can't define a variable before using it.
– Funk Forty Niner
Nov 21 '18 at 21:10
add a comment |
3 Answers
3
active
oldest
votes
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 |
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 |
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 '18 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 '18 at 20:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 19:55
DanDan
5,44531432
5,44531432
add a comment |
add a comment |
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 |
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 |
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 '18 at 19:49
jfadichjfadich
3,4381723
3,4381723
add a comment |
add a comment |
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 '18 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 '18 at 20:00
add a comment |
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 '18 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 '18 at 20:00
add a comment |
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 '18 at 19:49
AndreasAndreas
16.5k41744
16.5k41744
Thank you! It works fine!
– SteveO24
Nov 21 '18 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 '18 at 20:00
add a comment |
Thank you! It works fine!
– SteveO24
Nov 21 '18 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 '18 at 20:00
Thank you! It works fine!
– SteveO24
Nov 21 '18 at 19:57
Thank you! It works fine!
– SteveO24
Nov 21 '18 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 '18 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 '18 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.
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 '18 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
– Dan
Nov 21 '18 at 19:48
You can't define a variable before using it.
– Funk Forty Niner
Nov 21 '18 at 21:10