Return true php

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
1












A friend got this question during an interview:



What should be the value of x so that the following function returns true.



<?php
function returnTrue( $x )
$x[$x] = $x;
return $x != true;

$res = returnTrue(YOUR_ANSWER);
var_dump($res);
?>


The answer should be 3 characters max










share|improve this question

























    up vote
    6
    down vote

    favorite
    1












    A friend got this question during an interview:



    What should be the value of x so that the following function returns true.



    <?php
    function returnTrue( $x )
    $x[$x] = $x;
    return $x != true;

    $res = returnTrue(YOUR_ANSWER);
    var_dump($res);
    ?>


    The answer should be 3 characters max










    share|improve this question























      up vote
      6
      down vote

      favorite
      1









      up vote
      6
      down vote

      favorite
      1






      1





      A friend got this question during an interview:



      What should be the value of x so that the following function returns true.



      <?php
      function returnTrue( $x )
      $x[$x] = $x;
      return $x != true;

      $res = returnTrue(YOUR_ANSWER);
      var_dump($res);
      ?>


      The answer should be 3 characters max










      share|improve this question













      A friend got this question during an interview:



      What should be the value of x so that the following function returns true.



      <?php
      function returnTrue( $x )
      $x[$x] = $x;
      return $x != true;

      $res = returnTrue(YOUR_ANSWER);
      var_dump($res);
      ?>


      The answer should be 3 characters max







      php






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 3 hours ago









      TSR

      1,04711029




      1,04711029






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          That was interesting, I gave this test a quick try and the answer is :



          $res = returnTrue();





          share|improve this answer




















          • did you notice that your code will trigger an illegal offset warning?I think that a question designed for an interview can't lead to this type of error.But it is just an opinion....
            – Elementary
            2 hours ago

















          up vote
          1
          down vote













          of course this



          returntrue();


          works
          but not completely fine as you would receive the message




          Warning: Illegal offset type in ....




          You must keep in my in mind that string type also allows arrayAccess style so



          the right answer is



          $res=returntrue('0');
          var_dump($res)// print true


          When you give as argument the string '0' this code $x[$x]=$x give again the same string '0'
          wich will not produce any warning and will absolutely return true as the string '0' will always be evaluated as false and false!=true return true






          share|improve this answer






















          • A better (and probably the correct) solution in my opinion.
            – Nick
            15 mins ago


















          up vote
          0
          down vote













          And also for the other post now deleted, I also managed to solve it.



          function returnTrue( $x )

          if( !is_array( $x ) )
          return false;

          foreach( $x as $x )
          $x = $x;

          return $x;


          var_dump(returnTrue([!0]));





          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',
            convertImagesToLinks: true,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            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%2f52904507%2freturn-true-php%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            That was interesting, I gave this test a quick try and the answer is :



            $res = returnTrue();





            share|improve this answer




















            • did you notice that your code will trigger an illegal offset warning?I think that a question designed for an interview can't lead to this type of error.But it is just an opinion....
              – Elementary
              2 hours ago














            up vote
            2
            down vote



            accepted










            That was interesting, I gave this test a quick try and the answer is :



            $res = returnTrue();





            share|improve this answer




















            • did you notice that your code will trigger an illegal offset warning?I think that a question designed for an interview can't lead to this type of error.But it is just an opinion....
              – Elementary
              2 hours ago












            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            That was interesting, I gave this test a quick try and the answer is :



            $res = returnTrue();





            share|improve this answer












            That was interesting, I gave this test a quick try and the answer is :



            $res = returnTrue();






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 2 hours ago









            Rémi Bosgaerd

            9710




            9710











            • did you notice that your code will trigger an illegal offset warning?I think that a question designed for an interview can't lead to this type of error.But it is just an opinion....
              – Elementary
              2 hours ago
















            • did you notice that your code will trigger an illegal offset warning?I think that a question designed for an interview can't lead to this type of error.But it is just an opinion....
              – Elementary
              2 hours ago















            did you notice that your code will trigger an illegal offset warning?I think that a question designed for an interview can't lead to this type of error.But it is just an opinion....
            – Elementary
            2 hours ago




            did you notice that your code will trigger an illegal offset warning?I think that a question designed for an interview can't lead to this type of error.But it is just an opinion....
            – Elementary
            2 hours ago












            up vote
            1
            down vote













            of course this



            returntrue();


            works
            but not completely fine as you would receive the message




            Warning: Illegal offset type in ....




            You must keep in my in mind that string type also allows arrayAccess style so



            the right answer is



            $res=returntrue('0');
            var_dump($res)// print true


            When you give as argument the string '0' this code $x[$x]=$x give again the same string '0'
            wich will not produce any warning and will absolutely return true as the string '0' will always be evaluated as false and false!=true return true






            share|improve this answer






















            • A better (and probably the correct) solution in my opinion.
              – Nick
              15 mins ago















            up vote
            1
            down vote













            of course this



            returntrue();


            works
            but not completely fine as you would receive the message




            Warning: Illegal offset type in ....




            You must keep in my in mind that string type also allows arrayAccess style so



            the right answer is



            $res=returntrue('0');
            var_dump($res)// print true


            When you give as argument the string '0' this code $x[$x]=$x give again the same string '0'
            wich will not produce any warning and will absolutely return true as the string '0' will always be evaluated as false and false!=true return true






            share|improve this answer






















            • A better (and probably the correct) solution in my opinion.
              – Nick
              15 mins ago













            up vote
            1
            down vote










            up vote
            1
            down vote









            of course this



            returntrue();


            works
            but not completely fine as you would receive the message




            Warning: Illegal offset type in ....




            You must keep in my in mind that string type also allows arrayAccess style so



            the right answer is



            $res=returntrue('0');
            var_dump($res)// print true


            When you give as argument the string '0' this code $x[$x]=$x give again the same string '0'
            wich will not produce any warning and will absolutely return true as the string '0' will always be evaluated as false and false!=true return true






            share|improve this answer














            of course this



            returntrue();


            works
            but not completely fine as you would receive the message




            Warning: Illegal offset type in ....




            You must keep in my in mind that string type also allows arrayAccess style so



            the right answer is



            $res=returntrue('0');
            var_dump($res)// print true


            When you give as argument the string '0' this code $x[$x]=$x give again the same string '0'
            wich will not produce any warning and will absolutely return true as the string '0' will always be evaluated as false and false!=true return true







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 2 hours ago

























            answered 2 hours ago









            Elementary

            1,2481215




            1,2481215











            • A better (and probably the correct) solution in my opinion.
              – Nick
              15 mins ago

















            • A better (and probably the correct) solution in my opinion.
              – Nick
              15 mins ago
















            A better (and probably the correct) solution in my opinion.
            – Nick
            15 mins ago





            A better (and probably the correct) solution in my opinion.
            – Nick
            15 mins ago











            up vote
            0
            down vote













            And also for the other post now deleted, I also managed to solve it.



            function returnTrue( $x )

            if( !is_array( $x ) )
            return false;

            foreach( $x as $x )
            $x = $x;

            return $x;


            var_dump(returnTrue([!0]));





            share|improve this answer
























              up vote
              0
              down vote













              And also for the other post now deleted, I also managed to solve it.



              function returnTrue( $x )

              if( !is_array( $x ) )
              return false;

              foreach( $x as $x )
              $x = $x;

              return $x;


              var_dump(returnTrue([!0]));





              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                And also for the other post now deleted, I also managed to solve it.



                function returnTrue( $x )

                if( !is_array( $x ) )
                return false;

                foreach( $x as $x )
                $x = $x;

                return $x;


                var_dump(returnTrue([!0]));





                share|improve this answer












                And also for the other post now deleted, I also managed to solve it.



                function returnTrue( $x )

                if( !is_array( $x ) )
                return false;

                foreach( $x as $x )
                $x = $x;

                return $x;


                var_dump(returnTrue([!0]));






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 hours ago









                Rémi Bosgaerd

                9710




                9710



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52904507%2freturn-true-php%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery