How to append a statement using the null coalesce operator to a string?
I have a string with an operator in:
echo 'abc.' . $type ?? 'no type' . 'xyz';
If $type exists I want that to be the value, if it does not, I want 'no type' to be the value.
But with the above I just get a $type undefined error. It's not defined in my example as I want 'no type' to be outputted.
Where am I going wrong?
php php-7
add a comment |
I have a string with an operator in:
echo 'abc.' . $type ?? 'no type' . 'xyz';
If $type exists I want that to be the value, if it does not, I want 'no type' to be the value.
But with the above I just get a $type undefined error. It's not defined in my example as I want 'no type' to be outputted.
Where am I going wrong?
php php-7
1
We can start by asking which version of PHP you're using, since??
is a relatively new operator.
– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:25
I'm using PHP 7.
– panthro
Nov 19 '18 at 18:27
1
Please also update your question to mention that. No one should have to read a comment thread to answer your question =) Also, did you make sure to read through the operator precedence list? secure.php.net/manual/en/language.operators.precedence.php (specifically, you want to look at which of.
and??
gets to go first)
– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:29
add a comment |
I have a string with an operator in:
echo 'abc.' . $type ?? 'no type' . 'xyz';
If $type exists I want that to be the value, if it does not, I want 'no type' to be the value.
But with the above I just get a $type undefined error. It's not defined in my example as I want 'no type' to be outputted.
Where am I going wrong?
php php-7
I have a string with an operator in:
echo 'abc.' . $type ?? 'no type' . 'xyz';
If $type exists I want that to be the value, if it does not, I want 'no type' to be the value.
But with the above I just get a $type undefined error. It's not defined in my example as I want 'no type' to be outputted.
Where am I going wrong?
php php-7
php php-7
edited Nov 19 '18 at 18:44
miken32
23.7k84972
23.7k84972
asked Nov 19 '18 at 18:23
panthropanthro
7,04332100202
7,04332100202
1
We can start by asking which version of PHP you're using, since??
is a relatively new operator.
– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:25
I'm using PHP 7.
– panthro
Nov 19 '18 at 18:27
1
Please also update your question to mention that. No one should have to read a comment thread to answer your question =) Also, did you make sure to read through the operator precedence list? secure.php.net/manual/en/language.operators.precedence.php (specifically, you want to look at which of.
and??
gets to go first)
– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:29
add a comment |
1
We can start by asking which version of PHP you're using, since??
is a relatively new operator.
– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:25
I'm using PHP 7.
– panthro
Nov 19 '18 at 18:27
1
Please also update your question to mention that. No one should have to read a comment thread to answer your question =) Also, did you make sure to read through the operator precedence list? secure.php.net/manual/en/language.operators.precedence.php (specifically, you want to look at which of.
and??
gets to go first)
– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:29
1
1
We can start by asking which version of PHP you're using, since
??
is a relatively new operator.– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:25
We can start by asking which version of PHP you're using, since
??
is a relatively new operator.– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:25
I'm using PHP 7.
– panthro
Nov 19 '18 at 18:27
I'm using PHP 7.
– panthro
Nov 19 '18 at 18:27
1
1
Please also update your question to mention that. No one should have to read a comment thread to answer your question =) Also, did you make sure to read through the operator precedence list? secure.php.net/manual/en/language.operators.precedence.php (specifically, you want to look at which of
.
and ??
gets to go first)– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:29
Please also update your question to mention that. No one should have to read a comment thread to answer your question =) Also, did you make sure to read through the operator precedence list? secure.php.net/manual/en/language.operators.precedence.php (specifically, you want to look at which of
.
and ??
gets to go first)– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:29
add a comment |
3 Answers
3
active
oldest
votes
You need to add brackets round the ?? operator...
echo 'abc.' . ($type ?? 'no type') . 'xyz';
What it was doing was evaluating it like 'abc.' . $type
and then ??
.
This is all about operator precedence (thanks to @Blackhole for the prompt).
A link to the documentation.
– Blackhole
Nov 19 '18 at 18:28
Thank you very much. Will accept asap.
– panthro
Nov 19 '18 at 18:29
add a comment |
Null Coalescing Operator:
echo 'abc.' . ($type ?? 'no type') . 'xyz';
or this (ternary operator)
echo 'abc.' . (isset($type) ? $type : 'no type' ) . 'xyz';
That's not really a userful answer in this case, though. Using??
is fine, but.
has higher precedence, so the??
expression needs to be in parentheses.
– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:31
add a comment |
You don't need to use concatenation when using echo
. It can take multiple arguments, just separate them with a comma (as if it were an actual function):
echo 'abc.', $type ?? 'no type', 'xyz';
This is also (slightly) more efficient than the upvoted answer, especially with long strings (PHP doesn't have to construct/store the whole string in a buffer before displaying it).
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%2f53380574%2fhow-to-append-a-statement-using-the-null-coalesce-operator-to-a-string%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
You need to add brackets round the ?? operator...
echo 'abc.' . ($type ?? 'no type') . 'xyz';
What it was doing was evaluating it like 'abc.' . $type
and then ??
.
This is all about operator precedence (thanks to @Blackhole for the prompt).
A link to the documentation.
– Blackhole
Nov 19 '18 at 18:28
Thank you very much. Will accept asap.
– panthro
Nov 19 '18 at 18:29
add a comment |
You need to add brackets round the ?? operator...
echo 'abc.' . ($type ?? 'no type') . 'xyz';
What it was doing was evaluating it like 'abc.' . $type
and then ??
.
This is all about operator precedence (thanks to @Blackhole for the prompt).
A link to the documentation.
– Blackhole
Nov 19 '18 at 18:28
Thank you very much. Will accept asap.
– panthro
Nov 19 '18 at 18:29
add a comment |
You need to add brackets round the ?? operator...
echo 'abc.' . ($type ?? 'no type') . 'xyz';
What it was doing was evaluating it like 'abc.' . $type
and then ??
.
This is all about operator precedence (thanks to @Blackhole for the prompt).
You need to add brackets round the ?? operator...
echo 'abc.' . ($type ?? 'no type') . 'xyz';
What it was doing was evaluating it like 'abc.' . $type
and then ??
.
This is all about operator precedence (thanks to @Blackhole for the prompt).
edited Nov 19 '18 at 18:30
answered Nov 19 '18 at 18:27
Nigel RenNigel Ren
26.6k61833
26.6k61833
A link to the documentation.
– Blackhole
Nov 19 '18 at 18:28
Thank you very much. Will accept asap.
– panthro
Nov 19 '18 at 18:29
add a comment |
A link to the documentation.
– Blackhole
Nov 19 '18 at 18:28
Thank you very much. Will accept asap.
– panthro
Nov 19 '18 at 18:29
A link to the documentation.
– Blackhole
Nov 19 '18 at 18:28
A link to the documentation.
– Blackhole
Nov 19 '18 at 18:28
Thank you very much. Will accept asap.
– panthro
Nov 19 '18 at 18:29
Thank you very much. Will accept asap.
– panthro
Nov 19 '18 at 18:29
add a comment |
Null Coalescing Operator:
echo 'abc.' . ($type ?? 'no type') . 'xyz';
or this (ternary operator)
echo 'abc.' . (isset($type) ? $type : 'no type' ) . 'xyz';
That's not really a userful answer in this case, though. Using??
is fine, but.
has higher precedence, so the??
expression needs to be in parentheses.
– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:31
add a comment |
Null Coalescing Operator:
echo 'abc.' . ($type ?? 'no type') . 'xyz';
or this (ternary operator)
echo 'abc.' . (isset($type) ? $type : 'no type' ) . 'xyz';
That's not really a userful answer in this case, though. Using??
is fine, but.
has higher precedence, so the??
expression needs to be in parentheses.
– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:31
add a comment |
Null Coalescing Operator:
echo 'abc.' . ($type ?? 'no type') . 'xyz';
or this (ternary operator)
echo 'abc.' . (isset($type) ? $type : 'no type' ) . 'xyz';
Null Coalescing Operator:
echo 'abc.' . ($type ?? 'no type') . 'xyz';
or this (ternary operator)
echo 'abc.' . (isset($type) ? $type : 'no type' ) . 'xyz';
edited Nov 19 '18 at 18:40
Jay Blanchard
35.6k125596
35.6k125596
answered Nov 19 '18 at 18:25
Kamil KiełczewskiKamil Kiełczewski
10.1k86093
10.1k86093
That's not really a userful answer in this case, though. Using??
is fine, but.
has higher precedence, so the??
expression needs to be in parentheses.
– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:31
add a comment |
That's not really a userful answer in this case, though. Using??
is fine, but.
has higher precedence, so the??
expression needs to be in parentheses.
– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:31
That's not really a userful answer in this case, though. Using
??
is fine, but .
has higher precedence, so the ??
expression needs to be in parentheses.– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:31
That's not really a userful answer in this case, though. Using
??
is fine, but .
has higher precedence, so the ??
expression needs to be in parentheses.– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:31
add a comment |
You don't need to use concatenation when using echo
. It can take multiple arguments, just separate them with a comma (as if it were an actual function):
echo 'abc.', $type ?? 'no type', 'xyz';
This is also (slightly) more efficient than the upvoted answer, especially with long strings (PHP doesn't have to construct/store the whole string in a buffer before displaying it).
add a comment |
You don't need to use concatenation when using echo
. It can take multiple arguments, just separate them with a comma (as if it were an actual function):
echo 'abc.', $type ?? 'no type', 'xyz';
This is also (slightly) more efficient than the upvoted answer, especially with long strings (PHP doesn't have to construct/store the whole string in a buffer before displaying it).
add a comment |
You don't need to use concatenation when using echo
. It can take multiple arguments, just separate them with a comma (as if it were an actual function):
echo 'abc.', $type ?? 'no type', 'xyz';
This is also (slightly) more efficient than the upvoted answer, especially with long strings (PHP doesn't have to construct/store the whole string in a buffer before displaying it).
You don't need to use concatenation when using echo
. It can take multiple arguments, just separate them with a comma (as if it were an actual function):
echo 'abc.', $type ?? 'no type', 'xyz';
This is also (slightly) more efficient than the upvoted answer, especially with long strings (PHP doesn't have to construct/store the whole string in a buffer before displaying it).
edited Nov 19 '18 at 19:15
answered Nov 19 '18 at 18:54
JetoJeto
5,22521018
5,22521018
add a comment |
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%2f53380574%2fhow-to-append-a-statement-using-the-null-coalesce-operator-to-a-string%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
1
We can start by asking which version of PHP you're using, since
??
is a relatively new operator.– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:25
I'm using PHP 7.
– panthro
Nov 19 '18 at 18:27
1
Please also update your question to mention that. No one should have to read a comment thread to answer your question =) Also, did you make sure to read through the operator precedence list? secure.php.net/manual/en/language.operators.precedence.php (specifically, you want to look at which of
.
and??
gets to go first)– Mike 'Pomax' Kamermans
Nov 19 '18 at 18:29