Re: Merging Strings

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux