0

I have a script which is like a whois database. This function returns site views and I want to echo between value.

How can I echo and return one result? If the number is say 4000, it should return only 1k-10k

Rows are like

1550330

1000000

The code:

$siteTotalViews=1000000;
if($siteTotalViews <= 100){
    echo '0-100';
}
if($siteTotalViews <= 1000){
    echo '100-1k';
}
if($siteTotalViews <= 10000){
    echo '1k-10k';
}
if($siteTotalViews <= 100000){
    echo '10k-100k';
}
if($siteTotalViews <= 1000000){
    echo '100k-1 mil';
}
if($siteTotalViews <= 2000000){
    echo '1 mil-2 mil';
}
if($siteTotalViews <= 5000000){
    echo '2 mil-5 mil';
}
if($siteTotalViews <= 10000000){
    echo '5 mil-10 mil';
}
if($siteTotalViews >= 10000000){
    echo '10 mil +';
}
2
  • 3
    So, your question is...? Commented Feb 11, 2012 at 17:56
  • Sorry, why can't you use elseif? Commented Feb 11, 2012 at 18:14

4 Answers 4

2

Quick fix:

$siteTotalViews=1000000;
if($siteTotalViews <= 100){
    echo '0-100';
}
//next else is new
else if($siteTotalViews <= 1000){
    echo '100-1k';
}
//next else is new
else if($siteTotalViews <= 10000){
    echo '1k-10k';
}
//next else is new
else if($siteTotalViews <= 100000){
    echo '10k-100k';
}

Better fix:

$names=array(
  100 => '0-100',
  1000 => '100-1k',
  10000 => '1k-10k',
  ...
}

foreach ($names as $count=>$name)
  if ($siteTotalViews<$count) break;

echo $name;
Sign up to request clarification or add additional context in comments.

1 Comment

@Kaii So what's the problem? It will end up with $name having the last name in the array, which is exactly what we want. Unable to see a "wrong way round" here!
2

You could create a function that return the interval. When the function hit the return statement it stops executing, so you will only get one value back. Then you can call the function and echo the result:

function getInterval($siteTotalViews) {

  if($siteTotalViews <= 100){
      return '0-100';
  }
  if($siteTotalViews <= 1000){
      return '100-1k';
  }

  ...

}

echo getInterval(1000);

1 Comment

@TheBlackBenzKid No you shouldn't need to use else if, since if the first if-statement isn't valid it will just try the next until it reaches the one that is true.
2

You could put all the limits and their corresponding text into an array and then loop over the reverse array to find the appropriate output. (breaking the loop when a limit has been hit)

$siteTotalViews=1000000;
$outputs = array(
  0 => '0-100',
  100 => '100-1k',
  1000 => '1k-10k',
  10000 => '10k-100k',
  100000 => '100k-1 mil',
  1000000 => '1 mil-2 mil',
  2000000 => '2 mil-5 mil',
  5000000 => '5 mil-10 mil',
  10000000 => '10 mil +' );
$outputs = array_reverse($outputs);

foreach ($outputs as $limit => $text) {
  if ($siteTotalViews >= $limit) {
    echo $text;
    break;
  }
}

3 Comments

What about speed of this compared to using an if and else if statement
almost the same. you should really not care about micro-optimization here. in fact, the count of ifs executed here is the same as it would be without the loop. the only overhead you have is the array and the loop - don't care about.
@TheBlackBenzKid: Doesn't matter. It's a much better solution.
0
$siteTotalViews=1000000;
 if($siteTotalViews >= 0 && $siteTotalViews <=100 ){    
echo '0-100'; } 
if($siteTotalViews >=101 && $siteTotalViews <= 1000){    
 echo '100-1k'; } 
.....
   if($siteTotalViews >= 10000000){     
echo '10 mil +'; } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.