|
Posted by ColdShine on 03/18/06 04:48
frizzle in news:1142644593.183520.156040@g10g2000cwb.googlegroups.com wrote:
> ColdShine wrote:
>
>> frizzle in news:1142640199.152903.92170@p10g2000cwp.googlegroups.com
>> wrote:
>>
>>> $_GET['artile'] looks like this: /2006/03/lorem_ipsum
>>>
>>> $locationinfo = explode('/', trim( $_GET['article'] , '/') );
>>>
>>> list( $article_year, $article_month, $article_url ) = $locationinfo;
>>>
>>> echo '<br>year: '.(($article_year === NULL)? 'null': $article_year);
>>> echo '<br>month: '.(($article_month === NULL)? 'null': $article_month);
>>> echo '<br>article: '.(($article_url === NULL)? 'null': $article_url);
>>>
>>> The weird thing is, if $_GET['article'] is empty, $article_year isn't
>>> NULL, but the rest is. This doesn't make any sense to me ...
>>
>> When there's no match for the specified delimiter, explode() returns an
>> array with 1 item (the entire source string). So, assigning this to
>> list(...) results in the first list() argument being assigned the value
>> of the 1st (and only) item in the array, and the remaining list()
>> arguments are set to null (or simply still unset? I don't know).
>>
>> So, as in your case, you get trim($_GET['article'], '/') == '';
>> explode('/', '') returns array(''), and list($a_y, $a_m, $a_u)
>> translates into $a_y = ''; $a_m = null, $a_u = null.
>
> So validating it as follows would be just as safe?
>
> echo '<br>year: '.(($article_year === '' )? 'null': $article_year);
> echo '<br>month: '.(($article_month === NULL)? 'null': $article_month);
> echo '<br>article: '.(($article_url === NULL)? 'null': $article_url);
You may choose between several options... here I list two of them:
if (count($locationinfo) == 3)
{
list($...) = $locationinfo;
echo $...; // Assume valid values.
}
else
{
echo ...; // Assume null values.
}
Or:
echo '<br>year: '.($article_year? $article_year: 'null');
echo '<br>month: '.($article_month? $article_month: 'null');
echo '<br>article: '.($article_url? $article_url: 'null');
Since 0 is not a valid value for an year nor it is for a month and a '' URL
is as useless as 'null', and (bool)'' equals false as (bool)null does, this
code is conceptually the same as you originally posted.
--
ColdShine
"Experience is a hard teacher: she gives the test first, the lesson
afterwards." - Vernon Sanders law
[Back to original message]
|