Strange strlen issue

Hi folks,

$to_date=$_POST['to']; // assume date entered like this 2018-3-26
$temp=explode('-',$to_date);
echo strlen($temp[1]); // shows 1
if(strlen($temp[1]==1)){ // but this if fails. why ???
    echo "yes";
    $to_date=$temp[0] . "-0" . $temp[1] . "-" . $temp[2];
    echo $to_date;
}else{
   echo "no"; // out put prints no
}

You have a close bracket in the wrong place!

5 Likes

Really? sure ill check in a moment and feedback. :smiley:

i could not exactly spot where that close bracket is in wrong place. then i adjusted to code to avoid too many close brackets and now it works

$from_date=$_POST['from'];
$temp=explode('-',$from_date);
$count=strlen($temp[1]); 
if($count<2){ 
    $from_date=$temp[0] . "-0" . $temp[1] . "-" . $temp[2];
}

You were close to finding it in your previous post #3

1 Like

gandalf’s trying to point you here.
Where should that first ) ACTUALLY go?

2 Likes

PHP will do the following:

  • check if $temp[1] equals 1 (it is “3” so the result is the boolean value false)
  • get the length of false
  • convert the length to boolean and use that for the if statement

Clearly that is wrong. What you want to do is:

  • get the length of $temp[1]
  • check if the length equals 1
  • use that boolean value in the if statement

So the parameter for strlen should be just $temp[1] but you had $temp[1]==1 for the strlen parameter.

1 Like

hahahah , let me go to my optician…

Thank you all for helping me resolving this issue !!!

1 Like

It happens to all of us. We expect a problem to be more complicated and overlook the simple cause. We need problems like this to teach us to look more closely at things like parentheses.

1 Like

ofcause. i even thought that i might have ended up discovering a php starlen bug (for a moment) :smiley:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.