What does this do?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
treesa
Forum Commoner
Posts: 29
Joined: Sun Aug 31, 2008 10:19 pm

What does this do?

Post by treesa »

mt_rand(10,12)?ucfirst($str[$i]):strtolower($str[$i])

any other way,the above statement can be written?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: What does this do?

Post by Christopher »

Yeah ...

ucfirst($str[$i])

because mt_rand(10,12) is always true! ;)
(#10850)
treesa
Forum Commoner
Posts: 29
Joined: Sun Aug 31, 2008 10:19 pm

Re: What does this do?

Post by treesa »

what is the result of
mt_rand(0,1)?ucfirst($str[$i]):strtolower($str[$i]) ?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: What does this do?

Post 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]);
}
(#10850)
treesa
Forum Commoner
Posts: 29
Joined: Sun Aug 31, 2008 10:19 pm

Re: What does this do?

Post 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]);
}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: What does this do?

Post 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! ;)
(#10850)
treesa
Forum Commoner
Posts: 29
Joined: Sun Aug 31, 2008 10:19 pm

Re: What does this do?

Post 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]);
}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: What does this do?

Post by Christopher »

Yes, $str1 will hold the value from mt_rand(0,1) so it will contain 0 or 1.
(#10850)
Post Reply