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>

Recommended Answers

All 6 Replies

so what is your question?

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

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

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?

!(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

!(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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.