Page 1 of 1
What does this do?
Posted: Tue Sep 09, 2008 11:51 pm
by treesa
mt_rand(10,12)?ucfirst($str[$i]):strtolower($str[$i])
any other way,the above statement can be written?
Re: What does this do?
Posted: Wed Sep 10, 2008 1:09 am
by Christopher
Yeah ...
ucfirst($str[$i])
because mt_rand(10,12) is always true!

Re: What does this do?
Posted: Wed Sep 10, 2008 1:49 am
by treesa
what is the result of
mt_rand(0,1)?ucfirst($str[$i]):strtolower($str[$i]) ?
Re: What does this do?
Posted: Wed Sep 10, 2008 2:49 am
by Christopher
Code: Select all
$foo = mt_rand(0,1)?ucfirst($str[$i]):strtolower($str[$i])
Is the same as:
Code: Select all
if (mt_rand(0,1) ) { // false when 0, true when not 0
$foo = ucfirst($str[$i]);
} else {
$foo = strtolower($str[$i]);
}
Re: What does this do?
Posted: Wed Sep 10, 2008 5:31 am
by treesa
thanks....
if (mt_rand(0,1) ) { // false when 0, true when not 0
$foo = ucfirst($str[$i]);
} else {
$foo = strtolower($str[$i]);
}
does that indicate if (mt_rand(0,1)) means the following if (mt_rand(0,1)==0)?
instead we could give
$str= mt_rand(0,1)
if ($str==0 ) { // false when 0, true when not 0
$foo = ucfirst($str[$i]);
} else {
$foo = strtolower($str[$i]);
}
Re: What does this do?
Posted: Wed Sep 10, 2008 12:52 pm
by Christopher
treesa wrote:does that indicate if (mt_rand(0,1)) means the following if (mt_rand(0,1)==0)?
No.
Code: Select all
if (mt_rand(0,1)) {
// is the same as
if (mt_rand(0,1) != 0) {
treesa wrote:instead we could give
$str= mt_rand(0,1)
if ($str==0 ) { // false when 0, true when not 0
$foo = ucfirst($str[$i]);
} else {
$foo = strtolower($str[$i]);
}
You are using $str for two different things there. The value returned from mt_rand() will be 0 or 1. No reason to convert those to upper or lower case!

Re: What does this do?
Posted: Wed Sep 10, 2008 10:16 pm
by treesa
sorry,i meant,
instead we could give
$str1= mt_rand(0,1)
if ($str1==0 ) { // false when 0, true when not 0
$foo = ucfirst($str[$i]);
} else {
$foo = strtolower($str[$i]);
}
Re: What does this do?
Posted: Wed Sep 10, 2008 11:53 pm
by Christopher
Yes, $str1 will hold the value from mt_rand(0,1) so it will contain 0 or 1.