How to append a statement using the null coalesce operator to a string?












2















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?










share|improve this question




















  • 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


















2















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?










share|improve this question




















  • 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
















2












2








2


0






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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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














3 Answers
3






active

oldest

votes


















8














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).






share|improve this answer


























  • 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



















1














Null Coalescing Operator:



echo 'abc.' . ($type ?? 'no type') . 'xyz';


or this (ternary operator)



echo 'abc.' . (isset($type) ? $type : 'no type' ) . 'xyz';





share|improve this answer


























  • 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





















0














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).






share|improve this answer

























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    8














    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).






    share|improve this answer


























    • 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
















    8














    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).






    share|improve this answer


























    • 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














    8












    8








    8







    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).






    share|improve this answer















    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).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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



















    • 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













    1














    Null Coalescing Operator:



    echo 'abc.' . ($type ?? 'no type') . 'xyz';


    or this (ternary operator)



    echo 'abc.' . (isset($type) ? $type : 'no type' ) . 'xyz';





    share|improve this answer


























    • 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


















    1














    Null Coalescing Operator:



    echo 'abc.' . ($type ?? 'no type') . 'xyz';


    or this (ternary operator)



    echo 'abc.' . (isset($type) ? $type : 'no type' ) . 'xyz';





    share|improve this answer


























    • 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
















    1












    1








    1







    Null Coalescing Operator:



    echo 'abc.' . ($type ?? 'no type') . 'xyz';


    or this (ternary operator)



    echo 'abc.' . (isset($type) ? $type : 'no type' ) . 'xyz';





    share|improve this answer















    Null Coalescing Operator:



    echo 'abc.' . ($type ?? 'no type') . 'xyz';


    or this (ternary operator)



    echo 'abc.' . (isset($type) ? $type : 'no type' ) . 'xyz';






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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





















    • 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













    0














    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).






    share|improve this answer






























      0














      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).






      share|improve this answer




























        0












        0








        0







        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).






        share|improve this answer















        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).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 '18 at 19:15

























        answered Nov 19 '18 at 18:54









        JetoJeto

        5,22521018




        5,22521018






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?