Re: Merging Strings
| From: | Jochem Maas | Date: | Tue, 03 May 2005 10:54:13 +0000 |
| Subject: | Re: Merging Strings | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-214372@lists.php.net to get a copy of this message | ||
Rolf van de Krol wrote:
Hi all, why do result the following two examples in two different value for $month? (1)$current_date = getdate(time());
$month = strval($current_date['year']);
$month .= (strlen(strval($current_date['mon'])) == 2)
? $current_date['mon']
: ("0".$current_date['mon']);
$current_date = getdate(time());
$month = strval($current_date['year']) . (strlen(strval($current_date['mon'])) == 2) ? $current_date['mon']
: ("0".$current_date['mon']);
in the second version:
strval($current_date['year']) . (strlen(strval($current_date['mon'])) == 2)
is equal to TRUE, therefore $current_date['mon'] is returned, you are missing
parentheses around the tertiary expression (?:).... so:
$current_date = getdate(time());
$month = strval($current_date['year']) . ((strlen(strval($current_date['mon'])) == 2) ? $current_date['mon'] : "0".$current_date['mon']);btw, have a look at str_pad: $current_date = getdate();
$month = str_pad($current_date['mon'], 2, "0", STR_PAD_LEFT);echo $month;
The first example sets $month to 200505 and the second sets it to 5. Why is that. In my opinion they both should set it to 200505. Is this a weird bug, or did i something wrong? I'm using PHP 4.3.10 with Apache 1.3.33 on W2K Pro, but the server of my hosting provider does the same (php 4.3.10 + apache (don't know the version, i guess 1.3.33) on linux (i'm not sure, but i thought it was redhat)). Rolf van de Krol