loop not processing first element

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
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

loop not processing first element

Post by rhecker »

My code is intended to display selected days of the week in a variety of languages, with the week starting on Monday. Because November 2014 starts on a Monday, I use it to define the days, so 246 parsed should be Tuesday Thursday Saturday. The problem is that my code is dropping the first day of the selection. I have set the variable $days to 257, which should return "Tuesday, Friday, Sunday" but instead returns just "Friday, Sunday." Code follows:

Code: Select all

date_default_timezone_set('America/Los_Angeles');
$formatter = new IntlDateFormatter(en, IntlDateFormatter::LONG, NULL, 'America/Los_Angeles', NULL, "EEEE");
$days='257';
if ($days > 0) {
$datefordays= new DateTime("2014-09-01");
$dayformat=$formatter->format($datefordays);
for ($i=1; $i < 8;$i++){
$dayformat=$formatter->format($datefordays);
$dayformat = ($language_selected='en')? $dayformat."s $i " : $dayformat;
$dayz.= strpos(strval($days), strval($i))? " $dayformat," : NULL;

$datefordays->modify('+ 1 day');
}
$day = substr_replace($dayz,'',-1);
}
echo $day;
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: loop not processing first element

Post by requinix »

Code: Select all

strpos(strval($days), strval($i))? " $dayformat," : NULL;
The documentation for strpos() has a pretty big warning:
Warning
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
Oh yeah, and you don't have enough =s in

Code: Select all

($language_selected='en')? $dayformat."s $i " : $dayformat;
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: loop not processing first element

Post by rhecker »

Thank you for catching the single equality. I have only tested with English so far, so that issue had not shown up.

Because I am treating a sequence of numbers as a string, using strict equality actually never returns a positive, but I was unaware of this point regarding strpos(), so thank you for that.

It turns out that using strstr() instead of strpos does produce the result I need, so the problem is solved.

The whole block of code is weird, I realize, but it's the only way I know of that will return the spelled out days in any language.

Thanks for the help.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: loop not processing first element

Post by requinix »

rhecker wrote:It turns out that using strstr() instead of strpos does produce the result I need, so the problem is solved.
Funny, because the documentation for strstr() has something to say about that too:
Note:
If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead.
Really, all you need to do is put a "!== false" in the original code and the problem is fixed.
Post Reply