afridy
July 21, 2019, 7:41am
1
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
afridy
July 22, 2019, 7:11am
3
afridy:
$temp[1]==1)
Really? sure ill check in a moment and feedback.
afridy
July 22, 2019, 1:00pm
4
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
afridy:
if(strlen($temp[1]==1))
gandalf’s trying to point you here.
Where should that first )
ACTUALLY go?
2 Likes
afridy:
if(strlen($temp[1]==1))
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
afridy
July 23, 2019, 7:59am
8
hahahah , let me go to my optician…
afridy
July 23, 2019, 8:00am
9
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
afridy
July 24, 2019, 4:55am
11
ofcause. i even thought that i might have ended up discovering a php starlen bug (for a moment)
system
Closed
October 23, 2019, 11:56am
12
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.