0
if($koltukk%4 == 2){
    if($koltukk > 1000){
    $koltukH = "B";
    (int)$koltukR = ($koltukk / 4) + 1;//Doesnt work (int)
            }
    else{
    $koltukH = "E";
    (int)$koltukR = ($koltukk / 4) + 1;//Doesnt work (int)

        }


    }

$koltukR = ($koltukk / 4) + 1; 

I want to get the $koltukR variable as an integer but i couldn't do it (int) did not work

9
  • 2
    Get rid of the (int)'s. They aren't valid in PHP the way you are using them. Commented Jan 7, 2014 at 21:31
  • Try $koltukR = (int)(($koltukk / 4) + 1); Commented Jan 7, 2014 at 21:32
  • I am curious, @John Conde, why do you put your answer in the comments instead of writing an answer? I have seen that before but I don't understand... Commented Jan 7, 2014 at 21:33
  • @DamienPirsy he is not, you are getting confused with the variable names. It is $koltukH and $koltukk Commented Jan 7, 2014 at 21:34
  • 1
    @FabienWarniez Because John is posting a comment and not an answer. Commented Jan 7, 2014 at 21:36

5 Answers 5

1

You need to use the (int) casting on the other side of the assignment operator:

$koltukR = (int)(($koltukk / 4) + 1);

Or, use intval() like this:

$kolturR = intval(($koltukk / 4) + 1);
Sign up to request clarification or add additional context in comments.

Comments

0
$koltukR = intval(($koltukk / 4) + 1);

You should use better variable names, move the math out of the if/else since both are the same, and you shouldn't even need to cast this as an int manually.

Comments

0

PHP has an intval() method that turns a variable into an integer. Pass in your variable as a parameter.

intval()

Comments

0
<?php

if($koltukk%4 == 2)
{
    if($koltukk > 1000)
    {
        $koltukH = "B";
        $koltukR = (int)(($koltukk / 4) + 1);
    } else{
        $koltukH = "E";
        $koltukR = (int)(($koltukk / 4) + 1);    
    }
}

echo $koltukR;

?>

Comments

0

One important note here: intval() is NOT round(). intval() is similar to floor().

I think what you really want is round():

Here's the real answer: K.I.S.S.

if($k%4 == 2){
    if($k > 1000){
        $H = "B"    
    }
    else{
        $H = "E";
    }    
}
$R = round($k / 4) + 1;

Stealing an example from https://www.php.net/intval to illustrate:

echo number_format(8.20*100, 20), "<br />";
echo intval(8.20*100), "<br />";
echo floor(8.20*100), "<br />";
echo round(8.20*100), "<br />";

819.99999999999988631316
819
819
820

1 Comment

Good to point out that this can definitely be refactored, though not actually answering the original question about integer type casting.

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.