Reply to Re: [PHP] Merging Strings

Your name:

Reply:


Posted by Jochem Maas on 05/03/05 13:54

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
>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация