0

Hey guys, can anyone point out where I'm going wrong with this? It counts the sessions fine, but the IF statement doesn't seem to be working as it should.

<?php 
session_start(); 
($_SESSION['count']) ? $_SESSION['count']++ : $_SESSION['count'] = 1;

if (!(count) % 5)
{ 
        echo "test";
}
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
"http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
    <meta http-equiv="content-type" content="text/html;charset=ISO-8859-1"> 
    <title>//</title>        
</head> 

<body> 
    <h2>You have been here <?php echo( $_SESSION['count'] ); ?> times in this session.</h2>    
</body> 
</html>
3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by ello
0

so what is your question?

Just asking if anyone can point out why the if statements not working...

0

If something is not working then you should be able to say what is not working and how do you think it was supposed to work. Saying just it doesn't work is meaningless

0

If something is not working then you should be able to say what is not working and how do you think it was supposed to work. Saying just it doesn't work is meaningless

The code is meant to echo 'test' everytime the session number reaches a multiple of 5.

But as of now it's not working...

I'm sure it's something stupid I'm doing wrong, just wondered if someone could spot what was wrong with it?

Edited by ello: n/a

1

!(count) isn't a valid php variable, php would look at that like a function... unless it is a function defined somewhere else. Anyway, you'd probably want to use the $_SESSION itself. Also, $_SESSION, to my knowledge, won't return a bool for your ternary statement.

% = modulus, meaning it'll divide and return any remainders. With that in mind, if it returns 0, it means it's divisible by the number given.

[Links]
http://us.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
http://us.php.net/manual/en/language.operators.arithmetic.php

Edited by Nyight: n/a

0

!(count) isn't a valid php variable, php would look at that like a function... unless it is a function defined somewhere else. Anyway, you'd probably want to use the $_SESSION itself. Also, $_SESSION, to my knowledge, won't return a bool for your ternary statement.

% = modulus, meaning it'll divide and return any remainders. With that in mind, if it returns 0, it means it's divisible by the number given.

[Links]
http://us.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
http://us.php.net/manual/en/language.operators.arithmetic.php

ahhh I see, thanks alot for that. I'd never come across modulus before so wasn't really sure how to use it. But thanks, appreciate it :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.