By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,321 Members | 1,909 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,321 IT Pros & Developers. It's quick & easy.

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

P: n/a
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->pathToNeededFiles.$dir;
// 11-10-03 - first we try to find the file using the prefix
$fileNameToTry = $prefix.$name;
$tryThisFile = $tryThisDir.$fileNameToTry;
$theFileIsLoaded = @include($tryThisFile);
if (!$theFileIsLoaded) {
$this->debugNotes("$i loop. In getArrangement(), in the class
McControllerForAll, we tried to include $tryThisFile but we did not
find it.");
}
// 11-10-03 - then we try it with ".php" at the end
if (!$theFileIsLoaded) {
$tryThisFile = $tryThisDir.$fileNameToTry.".php";
$theFileIsLoaded = @include($tryThisFile);
$this->debugNotes("$i loop. In getArrangement(), in the class
McControllerForAll, 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 (!$theFileIsLoaded) {
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include($tryThisFile);
$this->debugNotes("$i loop. In getArrangement(), in the class
McControllerForAll, we tried to include $tryThisFile but we did not
find it.");
}
if (!$theFileIsLoaded) {
$tryThisFile = $tryThisFile.".php";
$theFileIsLoaded = @include($tryThisFile);
$this->debugNotes("$i loop. In getArrangement(), in the class
McControllerForAll, we tried to include $tryThisFile but we did not
find it.");
}
if (!$theFileIsLoaded) {
$noFile = true;
$num = count($dirArray);
$this->debugNotes("$i loop. In getArrangement(), in the class
McControllerForAll, 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 .");
}
}
Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
lawrence wrote:
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?


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 =--
Jul 17 '05 #2

P: n/a
On 4 Mar 2004 11:46:06 -0800, lk******@geocities.com (lawrence) wrote:
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?


This is Expected Behaviour. See the manual:

http://www.php.net/manual/en/languag...string.casting

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

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
<http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>
Jul 17 '05 #3

P: n/a

"lawrence" <lk******@geocities.com> wrote in message
news:da**************************@posting.google.c om...
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?
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;

$theFileIsLoaded = @include($tryThisFile);

if (!$theFileIsLoaded)
$theFileIsLoaded = @include($someOtherFile);

if (!$theFileIsLoaded)
$theFileIsLoaded = @include($someThirdFile);

if (!$theFileIsLoaded)
$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

for ($i=0; $i < count($dirArray) && $noFile == true; $i++) {
$noFile = false;
$dir = $dirArray[$i];
$tryThisDir = $this->pathToNeededFiles.$dir;
// 11-10-03 - first we try to find the file using the prefix
$fileNameToTry = $prefix.$name;
$tryThisFile = $tryThisDir.$fileNameToTry;
$theFileIsLoaded = @include($tryThisFile);
if (!$theFileIsLoaded) {
$this->debugNotes("$i loop. In getArrangement(), in the class
McControllerForAll, we tried to include $tryThisFile but we did not
find it.");
}
// 11-10-03 - then we try it with ".php" at the end
if (!$theFileIsLoaded) {
$tryThisFile = $tryThisDir.$fileNameToTry.".php";
$theFileIsLoaded = @include($tryThisFile);
$this->debugNotes("$i loop. In getArrangement(), in the class
McControllerForAll, 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 (!$theFileIsLoaded) {
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include($tryThisFile);
$this->debugNotes("$i loop. In getArrangement(), in the class
McControllerForAll, we tried to include $tryThisFile but we did not
find it.");
}
if (!$theFileIsLoaded) {
$tryThisFile = $tryThisFile.".php";
$theFileIsLoaded = @include($tryThisFile);
$this->debugNotes("$i loop. In getArrangement(), in the class
McControllerForAll, we tried to include $tryThisFile but we did not
find it.");
}
if (!$theFileIsLoaded) {
$noFile = true;
$num = count($dirArray);
$this->debugNotes("$i loop. In getArrangement(), in the class
McControllerForAll, 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 .");
}
}

Jul 17 '05 #4

P: n/a
"Erik Johnson" <ej@just.post.here.com> wrote in message
You can't fathom why this loop wouldn't run a second time?
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.

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;

$theFileIsLoaded = @include($tryThisFile);

if (!$theFileIsLoaded)
$theFileIsLoaded = @include($someOtherFile);

if (!$theFileIsLoaded)
$theFileIsLoaded = @include($someThirdFile);

if (!$theFileIsLoaded)
$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

for ($i=0; $i < count($dirArray) && $noFile == true; $i++) {
$noFile = false;
$dir = $dirArray[$i];
$tryThisDir = $this->pathToNeededFiles.$dir;
// 11-10-03 - first we try to find the file using the prefix
$fileNameToTry = $prefix.$name;
$tryThisFile = $tryThisDir.$fileNameToTry;
$theFileIsLoaded = @include($tryThisFile);
if (!$theFileIsLoaded) {
$this->debugNotes("$i loop. In getArrangement(), in the class
McControllerForAll, we tried to include $tryThisFile but we did not
find it.");
}
// 11-10-03 - then we try it with ".php" at the end
if (!$theFileIsLoaded) {
$tryThisFile = $tryThisDir.$fileNameToTry.".php";
$theFileIsLoaded = @include($tryThisFile);
$this->debugNotes("$i loop. In getArrangement(), in the class
McControllerForAll, 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 (!$theFileIsLoaded) {
$tryThisFile = $tryThisDir.$name;
$theFileIsLoaded = @include($tryThisFile);
$this->debugNotes("$i loop. In getArrangement(), in the class
McControllerForAll, we tried to include $tryThisFile but we did not
find it.");
}
if (!$theFileIsLoaded) {
$tryThisFile = $tryThisFile.".php";
$theFileIsLoaded = @include($tryThisFile);
$this->debugNotes("$i loop. In getArrangement(), in the class
McControllerForAll, we tried to include $tryThisFile but we did not
find it.");
}
if (!$theFileIsLoaded) {
$noFile = true;
$num = count($dirArray);
$this->debugNotes("$i loop. In getArrangement(), in the class
McControllerForAll, 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 .");
}
}

Jul 17 '05 #5

P: n/a
Pedro Graca <he****@hotpop.com> wrote in message news:<c2*************@ID-203069.news.uni-berlin.de>...
<?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] []

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

string $theYear = string($theYearAsNumber);
???
Jul 17 '05 #6

P: n/a
lawrence wrote:
I take it there is nothing like:

string $theYear = string($theYearAsNumber);


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($boolean);
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #7

P: n/a
function setActive($state=5) {
if (is_bool($state)) {
$this->active = $state;
} else {
$this->resultsObject->addToErrorResults("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($level);

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?
Jul 17 '05 #8

P: n/a
Pedro Graca <he****@hotpop.com> wrote in message news:<c2*************@ID-203069.news.uni-berlin.de>...
lawrence wrote:
I take it there is nothing like:

string $theYear = string($theYearAsNumber);


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

false

All other 'things' do so more or less autogically.


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

$answer = "true";
$answer = settype($answer, "boolean");
Jul 17 '05 #9

P: n/a
Okay, I found this page, which explains casting in PHP.
http://us4.php.net/manual/en/languag...es.typecasting
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.