How does one cast a bool to a string? As in true to "true"?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lawrence

    How does one cast a bool to a string? As in true to "true"?

    In the following loop, at the end, the method debugNotes() is printing
    out some notes for me to read, so I can figure out what is going on in
    my script. Where I try to print out the value of the variable $noFile,
    it is print "1" instead of "true". Why does it do this?

    The loop is failing to run a second time, for reasons I can not
    fathom. debugNotes tells me that the count of $dirArray is 10, and the
    file hasn't been found yet, so I would expect the loop to keep
    looping.

    Why?



    for ($i=0; $i < count($dirArray ) && $noFile == true; $i++) {
    $noFile = false;
    $dir = $dirArray[$i];
    $tryThisDir = $this->pathToNeededFi les.$dir;
    // 11-10-03 - first we try to find the file using the prefix
    $fileNameToTry = $prefix.$name;
    $tryThisFile = $tryThisDir.$fi leNameToTry;
    $theFileIsLoade d = @include($tryTh isFile);
    if (!$theFileIsLoa ded) {
    $this->debugNotes(" $i loop. In getArrangement( ), in the class
    McControllerFor All, we tried to include $tryThisFile but we did not
    find it.");
    }
    // 11-10-03 - then we try it with ".php" at the end
    if (!$theFileIsLoa ded) {
    $tryThisFile = $tryThisDir.$fi leNameToTry.".p hp";
    $theFileIsLoade d = @include($tryTh isFile);
    $this->debugNotes(" $i loop. In getArrangement( ), in the class
    McControllerFor All, we tried to include $tryThisFile but we did not
    find it.");
    }
    // 11-10-03 - if that fails, then we try to find the file without
    using the prefix.
    if (!$theFileIsLoa ded) {
    $tryThisFile = $tryThisDir.$na me;
    $theFileIsLoade d = @include($tryTh isFile);
    $this->debugNotes(" $i loop. In getArrangement( ), in the class
    McControllerFor All, we tried to include $tryThisFile but we did not
    find it.");
    }
    if (!$theFileIsLoa ded) {
    $tryThisFile = $tryThisFile.". php";
    $theFileIsLoade d = @include($tryTh isFile);
    $this->debugNotes(" $i loop. In getArrangement( ), in the class
    McControllerFor All, we tried to include $tryThisFile but we did not
    find it.");
    }
    if (!$theFileIsLoa ded) {
    $noFile = true;
    $num = count($dirArray );
    $this->debugNotes(" $i loop. In getArrangement( ), in the class
    McControllerFor All, we end $i loop without finding the file we wanted.
    We expect to do a total of $num loops, maximum. Our variable noFile
    should be registered 'true' at this point. It's value is: $noFile .");
    }
    }
  • Pedro Graca

    #2
    Re: How does one cast a bool to a string? -- ANSWER HERE -- You have to convert from '' (boolean false) to 'false' and from 1 (boolean true) to 'true'. One way to do that is: &lt;?php echo $boolean_variab le : 'true' ? 'false'; ?&gt;

    lawrence wrote:[color=blue]
    > In the following loop, at the end, the method debugNotes() is printing
    > out some notes for me to read, so I can figure out what is going on in
    > my script. Where I try to print out the value of the variable $noFile,
    > it is print "1" instead of "true". Why does it do this?
    >
    > The loop is failing to run a second time, for reasons I can not
    > fathom. debugNotes tells me that the count of $dirArray is 10, and the
    > file hasn't been found yet, so I would expect the loop to keep
    > looping.
    >
    > Why?[/color]

    Isn't any of those files you include returning false (or 0, or '')?

    <?php
    $a = include 'nofile.php';
    $b = include 'yesfile.php';
    $c = include 'falsefile.php' ;

    echo "[$a] [$b] [$c]\n";
    ?>

    where nofile.php does not exist, yesfile.php is
    <?php echo 'YES'; ?> and falsefile.php is
    <?php echo 'false'; return 0; ?>


    The result is:
    YESfalse[] [1] []
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Andy Hassall

      #3
      Re: How does one cast a bool to a string? As in true to &quot;true&quot ;?

      On 4 Mar 2004 11:46:06 -0800, lkrubner@geocit ies.com (lawrence) wrote:
      [color=blue]
      >In the following loop, at the end, the method debugNotes() is printing
      >out some notes for me to read, so I can figure out what is going on in
      >my script. Where I try to print out the value of the variable $noFile,
      >it is print "1" instead of "true". Why does it do this?[/color]

      This is Expected Behaviour. See the manual:



      If you're printing debug statements, var_dump is often a good choice. It would
      produce 'bool(true)'.

      --
      Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
      <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

      Comment

      • Erik Johnson

        #4
        Re: How does one cast a bool to a string? As in true to &quot;true&quot ;?


        "lawrence" <lkrubner@geoci ties.com> wrote in message
        news:da7e68e8.0 403041146.1f806 bf6@posting.goo gle.com...
        [color=blue]
        > In the following loop, at the end, the method debugNotes() is printing
        > out some notes for me to read, so I can figure out what is going on in
        > my script. Where I try to print out the value of the variable $noFile,
        > it is print "1" instead of "true". Why does it do this?
        >
        > The loop is failing to run a second time, for reasons I can not
        > fathom. debugNotes tells me that the count of $dirArray is 10, and the
        > file hasn't been found yet, so I would expect the loop to keep
        > looping.
        >
        > Why?[/color]

        You can't fathom why this loop wouldn't run a second time?
        Let's cut out the fluff and pare your code down to the functional part:

        for ($i=0; $i < count($dirArray ) && $noFile == true; $i++)
        {
        $noFile = false;

        $theFileIsLoade d = @include($tryTh isFile);

        if (!$theFileIsLoa ded)
        $theFileIsLoade d = @include($someO therFile);

        if (!$theFileIsLoa ded)
        $theFileIsLoade d = @include($someT hirdFile);

        if (!$theFileIsLoa ded)
        $noFile = true;
        }

        Hopefully it is obvious now?

        As for your first question, that's just one of PHP little quirks: false is
        interpolated into strings as the empty string, true is interpolated as '1'.
        Here's a quick little way to convert it to the form you like:

        $x = true; # or false, take your pick
        print "x = " . ($x ? "true" : "false") . "\n";

        HTH,
        -ej



        [color=blue]
        > for ($i=0; $i < count($dirArray ) && $noFile == true; $i++) {
        > $noFile = false;
        > $dir = $dirArray[$i];
        > $tryThisDir = $this->pathToNeededFi les.$dir;
        > // 11-10-03 - first we try to find the file using the prefix
        > $fileNameToTry = $prefix.$name;
        > $tryThisFile = $tryThisDir.$fi leNameToTry;
        > $theFileIsLoade d = @include($tryTh isFile);
        > if (!$theFileIsLoa ded) {
        > $this->debugNotes(" $i loop. In getArrangement( ), in the class
        > McControllerFor All, we tried to include $tryThisFile but we did not
        > find it.");
        > }
        > // 11-10-03 - then we try it with ".php" at the end
        > if (!$theFileIsLoa ded) {
        > $tryThisFile = $tryThisDir.$fi leNameToTry.".p hp";
        > $theFileIsLoade d = @include($tryTh isFile);
        > $this->debugNotes(" $i loop. In getArrangement( ), in the class
        > McControllerFor All, we tried to include $tryThisFile but we did not
        > find it.");
        > }
        > // 11-10-03 - if that fails, then we try to find the file without
        > using the prefix.
        > if (!$theFileIsLoa ded) {
        > $tryThisFile = $tryThisDir.$na me;
        > $theFileIsLoade d = @include($tryTh isFile);
        > $this->debugNotes(" $i loop. In getArrangement( ), in the class
        > McControllerFor All, we tried to include $tryThisFile but we did not
        > find it.");
        > }
        > if (!$theFileIsLoa ded) {
        > $tryThisFile = $tryThisFile.". php";
        > $theFileIsLoade d = @include($tryTh isFile);
        > $this->debugNotes(" $i loop. In getArrangement( ), in the class
        > McControllerFor All, we tried to include $tryThisFile but we did not
        > find it.");
        > }
        > if (!$theFileIsLoa ded) {
        > $noFile = true;
        > $num = count($dirArray );
        > $this->debugNotes(" $i loop. In getArrangement( ), in the class
        > McControllerFor All, we end $i loop without finding the file we wanted.
        > We expect to do a total of $num loops, maximum. Our variable noFile
        > should be registered 'true' at this point. It's value is: $noFile .");
        > }
        > }[/color]


        Comment

        • lawrence

          #5
          Re: How does one cast a bool to a string? As in true to &quot;true&quot ;?

          "Erik Johnson" <ej@just.post.h ere.com> wrote in message[color=blue]
          > You can't fathom why this loop wouldn't run a second time?[/color]

          I think you misread the code. After all, you're looking at a loop that
          has worked correctly for months. When I found the problem, it was not
          related to the loop.

          As for the example you give, I've looked at it several times and I'm
          still not sure what point you're trying to make. The loop that you
          depict should run fine. And, in fact, it is running fine.

          But I do thank you for trying to help.





          [color=blue]
          > Let's cut out the fluff and pare your code down to the functional part:
          >
          > for ($i=0; $i < count($dirArray ) && $noFile == true; $i++)
          > {
          > $noFile = false;
          >
          > $theFileIsLoade d = @include($tryTh isFile);
          >
          > if (!$theFileIsLoa ded)
          > $theFileIsLoade d = @include($someO therFile);
          >
          > if (!$theFileIsLoa ded)
          > $theFileIsLoade d = @include($someT hirdFile);
          >
          > if (!$theFileIsLoa ded)
          > $noFile = true;
          > }
          >
          > Hopefully it is obvious now?
          >
          > As for your first question, that's just one of PHP little quirks: false is
          > interpolated into strings as the empty string, true is interpolated as '1'.
          > Here's a quick little way to convert it to the form you like:
          >
          > $x = true; # or false, take your pick
          > print "x = " . ($x ? "true" : "false") . "\n";
          >
          > HTH,
          > -ej
          >
          >
          >
          >[color=green]
          > > for ($i=0; $i < count($dirArray ) && $noFile == true; $i++) {
          > > $noFile = false;
          > > $dir = $dirArray[$i];
          > > $tryThisDir = $this->pathToNeededFi les.$dir;
          > > // 11-10-03 - first we try to find the file using the prefix
          > > $fileNameToTry = $prefix.$name;
          > > $tryThisFile = $tryThisDir.$fi leNameToTry;
          > > $theFileIsLoade d = @include($tryTh isFile);
          > > if (!$theFileIsLoa ded) {
          > > $this->debugNotes(" $i loop. In getArrangement( ), in the class
          > > McControllerFor All, we tried to include $tryThisFile but we did not
          > > find it.");
          > > }
          > > // 11-10-03 - then we try it with ".php" at the end
          > > if (!$theFileIsLoa ded) {
          > > $tryThisFile = $tryThisDir.$fi leNameToTry.".p hp";
          > > $theFileIsLoade d = @include($tryTh isFile);
          > > $this->debugNotes(" $i loop. In getArrangement( ), in the class
          > > McControllerFor All, we tried to include $tryThisFile but we did not
          > > find it.");
          > > }
          > > // 11-10-03 - if that fails, then we try to find the file without
          > > using the prefix.
          > > if (!$theFileIsLoa ded) {
          > > $tryThisFile = $tryThisDir.$na me;
          > > $theFileIsLoade d = @include($tryTh isFile);
          > > $this->debugNotes(" $i loop. In getArrangement( ), in the class
          > > McControllerFor All, we tried to include $tryThisFile but we did not
          > > find it.");
          > > }
          > > if (!$theFileIsLoa ded) {
          > > $tryThisFile = $tryThisFile.". php";
          > > $theFileIsLoade d = @include($tryTh isFile);
          > > $this->debugNotes(" $i loop. In getArrangement( ), in the class
          > > McControllerFor All, we tried to include $tryThisFile but we did not
          > > find it.");
          > > }
          > > if (!$theFileIsLoa ded) {
          > > $noFile = true;
          > > $num = count($dirArray );
          > > $this->debugNotes(" $i loop. In getArrangement( ), in the class
          > > McControllerFor All, we end $i loop without finding the file we wanted.
          > > We expect to do a total of $num loops, maximum. Our variable noFile
          > > should be registered 'true' at this point. It's value is: $noFile .");
          > > }
          > > }[/color][/color]

          Comment

          • lawrence

            #6
            Re: How does one cast a bool to a string? -- ANSWER HERE -- You have to convert from '' (boolean false) to 'false' and from 1 (boolean true) to 'true'. One way to do that is: &lt;?php echo $boolean_variab le : 'true' ? 'false'; ?&gt;

            Pedro Graca <hexkid@hotpop. com> wrote in message news:<c2828v$1q 6vga$1@ID-203069.news.uni-berlin.de>...[color=blue]
            > <?php
            > $a = include 'nofile.php';
            > $b = include 'yesfile.php';
            > $c = include 'falsefile.php' ;
            >
            > echo "[$a] [$b] [$c]\n";
            > ?>
            >
            > where nofile.php does not exist, yesfile.php is
            > <?php echo 'YES'; ?> and falsefile.php is
            > <?php echo 'false'; return 0; ?>
            >
            >
            > The result is:
            > YESfalse[] [1] [][/color]


            Thanks much for this answer. I take it there is nothing like:

            string $theYear = string($theYear AsNumber);


            ???

            Comment

            • Pedro Graca

              #7
              Re: How does one cast a bool to a string? -- ANSWER HERE -- You have to convert from '' (boolean false) to 'false' and from 1 (boolean true) to 'true'. One way to do that is: &lt;?php echo $boolean_variab le : 'true' ? 'false'; ?&gt;

              lawrence wrote:[color=blue]
              > I take it there is nothing like:
              >
              > string $theYear = string($theYear AsNumber);[/color]

              The only problematic 'thing' to transform to string is the value

              false

              All other 'things' do so more or less autogically.
              If you want to print a boolean value try this function:

              <?php
              function bool2string($x) {
              return ($x) ? 'true': 'false';
              }
              ?>

              then instead of
              echo $boolean;

              use the function
              echo bool2string($bo olean);
              --
              --= my mail box only accepts =--
              --= Content-Type: text/plain =--
              --= Size below 10001 bytes =--

              Comment

              • lawrence

                #8
                Re: How does one cast a bool to a string? As in true to &quot;true&quot ;?

                function setActive($stat e=5) {
                if (is_bool($state )) {
                $this->active = $state;
                } else {
                $this->resultsObjec t->addToErrorResu lts("In setActive(), in the
                class McFilterData, the programmer tried to set the active level for
                the filter object that protects everyone's privacy. This method
                expects to be handed a boolean, that is, true or false, but instead we
                got this: $state");
                }
                }


                $f->setActive(); will trip the error message, yes? I hope so.


                What about this:

                $level = "true";
                $f->setActive($lev el);

                Does PHP convert the string to a bool automatically? Can I stop PHP
                from doing these kinds of conversions, when I need for it to stop?

                Comment

                • lawrence

                  #9
                  Re: How does one cast a bool to a string? -- ANSWER HERE -- You have to convert from '' (boolean false) to 'false' and from 1 (boolean true) to 'true'. One way to do that is: &lt;?php echo $boolean_variab le : 'true' ? 'false'; ?&gt;

                  Pedro Graca <hexkid@hotpop. com> wrote in message news:<c2dl1g$1r td7i$1@ID-203069.news.uni-berlin.de>...[color=blue]
                  > lawrence wrote:[color=green]
                  > > I take it there is nothing like:
                  > >
                  > > string $theYear = string($theYear AsNumber);[/color]
                  >
                  > The only problematic 'thing' to transform to string is the value
                  >
                  > false
                  >
                  > All other 'things' do so more or less autogically.[/color]

                  Thanks. I realize now what I wanted was settype().

                  $answer = "true";
                  $answer = settype($answer , "boolean");

                  Comment

                  • lawrence

                    #10
                    Re: How does one cast a bool to a string? As in true to &quot;true&quot ;?

                    Okay, I found this page, which explains casting in PHP.



                    Comment

                    Working...