3

So I am writing out all my code and it is all the way It should be, but for some reason it doesn't seem to want to output anything on the screen like it should...

Can someone please help me out? Thanks

code:

$points_disp = $user_data['points'];
$oneFDigit = substr($points_disp, 0, 1);
$oneSDigit = substr($points_disp, 1, 1);
$oneRange = range(1000, 9999);

$tenFDigit = substr($points_disp, 0, 2);
$tenSDigit = substr($points_disp, 2, 1);
$tenRange = range(9999, 99999);

$hunFDigit = substr($points_disp, 0, 3);
$hunSDigit = substr($points_disp, 3, 1);
$hunRange = range(99999, 999999);

$oneMillionFD = substr($points_disp, 0, 1);
$oneMillionSD = substr($points_disp, 1, 1);
$oneMillionRange = range(999999, 9999999);

if ($points_disp < 1000){
    echo $points_disp;
} else if (in_array($points_disp, $oneRange)){
    echo $oneFDigit . "." . $oneSDigit . "k";
} else if (in_array($points_disp, $tenRange)){
    echo $tenFDigit . "." . $tenSDigit . "k";
} else if (in_array($points_disp, $hunRange)){
    echo $hunFDigit . "." . $hunSDigit . "k";
} else if (in_array($points_disp, $oneMillionRange)){
    echo $oneMillionFD . "." . $oneMillionSD . "m";
}
16
  • check your error log for errors, maybe there is an Fatal error: Allowed memory size Commented Sep 22, 2015 at 11:21
  • There are no errors, i have turned on error reporting and nothing is showing Commented Sep 22, 2015 at 11:22
  • 1
    put a simple else { echo 'Nada'; } maybe no condition is met... Commented Sep 22, 2015 at 11:23
  • 2
    add at last else{ echo "no output found"; } if none of your condition is true !! Commented Sep 22, 2015 at 11:24
  • 1
    For some reason when i get rid of the 4th code block ($oneMillionSD = e.t.c.) it all starts to work again.. :/ Commented Sep 22, 2015 at 11:26

3 Answers 3

5

If the condition is always false, use this:

...
} else {
    echo "some value";
}
Sign up to request clarification or add additional context in comments.

Comments

3

I tried this, just dumped your range() because I had a memory problem. I think range is the wrong function to use here, because you'll get some huge arrays and you just want to know if the number is between this values.

BEWARE: the new range array needs minValue and maxValue in this order!

$user_data['points'] = 9999;

$points_disp = $user_data['points'];
$oneFDigit = substr( $points_disp, 0, 1 );
$oneSDigit = substr( $points_disp, 1, 1 );
$oneRange = array( 1000, 9999 );

$tenFDigit = substr( $points_disp, 0, 2 );
$tenSDigit = substr( $points_disp, 2, 1 );
$tenRange = array( 9999, 99999 );
//
$hunFDigit = substr( $points_disp, 0, 3 );
$hunSDigit = substr( $points_disp, 3, 1 );
$hunRange = array( 99999, 999999 );
//
$oneMillionFD = substr( $points_disp, 0, 1 );
$oneMillionSD = substr( $points_disp, 1, 1 );
$oneMillionRange = array( 999999, 9999999 );


if ( $points_disp < 1000 ) {
    echo $points_disp;
} else if ( checkInRange( $points_disp, $oneRange ) ) {
    echo $oneFDigit . "." . $oneSDigit . "k";
} else if ( checkInRange( $points_disp, $tenRange ) ) {
    echo $tenFDigit . "." . $tenSDigit . "k";
} else if ( checkInRange( $points_disp, $hunRange ) ) {
    echo $hunFDigit . "." . $hunSDigit . "k";
} else if ( checkInRange( $points_disp, $oneMillionRange ) ) {
    echo $oneMillionFD . "." . $oneMillionSD . "m";
} else {
    echo "nothing found";
}

function checkInRange( $needle, $range ) {
    $min = $range[0];
    $max = $range[1];

    return ( $needle >= $min && $needle <= $max ) ? true : false;
}

3 Comments

if you try, don't forget to remove the $user_data['points'] = 9999; this was just for my test
yes! this worked! thankyou! may I ask? how did this fix it by adding the function?
well I cannot exactly say what was your problem, but as i tried it, i had a memory issue, because range returns an array with each value between the numbers. so it's better to really check the range and not make new arrays. You said you got an 500 error, if you look in your error_log, there must be the reason. I really think it was a memory error
0

The code seems a bit overkill for what you are trying to actually achieve.

$points = (string) 99999;

if($points < 1000) {
    echo $points;
}
elseif($points < 9999) {
    echo sprintf('%s.%sk', $points[0], $points[1]);
}
elseif($points < 99999) {
    echo sprintf('%s.%sk', $points[0], $points[1]);
}
elseif($points < 999999) {
    echo sprintf('%s.%sk', $points[0], $points[1]);
}
elseif($points < 9999999) {
    echo sprintf('%s.%sm', $points[0], $points[1]);
}

Result: 9.9k

2 Comments

trust me, you should've seen what I had before haha
If it is purely down to displaying a points value with a prefix then you can really cut this code down to 1/2 lines