hello
I want to make a counter so when someone visit the page it increases by 1
then when the number can be divided into 5 i want the page to redirect to another page
how can i do that
thanks
adel
Printable View
hello
I want to make a counter so when someone visit the page it increases by 1
then when the number can be divided into 5 i want the page to redirect to another page
how can i do that
thanks
adel
Well any number can be divided by 5. You want to check if it is a multiple of 5?
5, 10, 15, 20, 25...
yah exactly
ok, well here is something that I came up with...
basically there is no built in function, so it will check to see if there is a decimal in the number, and if there is no decimal, that means it went into it evenly.PHP Code://Get current page view count and add one
$count++;
//Check to see if count is a decimal
$number = $count / 5;
$str = strstr($number, '.');
//if it isnt, then it must be a multiple of 5
if ($str == false) {
//Redirect...
header("Location: http://www.mysite.com");
}
thanks i will try it
by the way isnt there a "%" operator that can check the remainder if the division i dunno i am still a noob in PHP
thanks again i will try it and respond whether it worked or not
when it reaches where it should redirect it tells me "Warning: Cannot modify header information, headers already sent"
ah. header needs to come before any outputted information. So it should be at the top of your page.
Basic mathematics:
String conversion is so inefficient: even if it was a string to begin with, then strpos() would be more efficient than strstr(), which returns the substring rather than the index at which it is found.PHP Code:if ($num % 5 == 0)
# $num is a whole multiple of 5.
Oh, and:
That will never execute, since you're assigning false to $str. You need === there.Quote:
Originally Posted by dclamp
haha. oops. thanks pena, i corrected that.
More acceptable, but still not quite correct. strstr() returns either a string or boolean false. The correct operator to use when checking for an explicit false value is ===, not ==, since there are some other values which can be evaluated to false if type checking is not applied. I don't know off-hand if there are any string values which do, but it's still not a good idea to use == where === is more appropriate.
As mentioned, though, if you just want to check whether a substring is present within a string then use strpos() instead:
In this case, the == operator is not just less appropriate, but flat out incorrect: if $mystring begins with 'substring', then strpos() will return zero, which evaluates to false. This is obviously an undesirable effect.PHP Code:if (strpos($mystring, 'substring') === 0)
# substring not found in $mystring