(int) - Problem

Sasser

Erfahrenes Mitglied
Moin Leute!

Ich verstehe etwas nicht; (int) macht doch aus einer Zahl eine Ganzzahl oder? Das funktioniert auch ohne Probleme, aber sobald ich eine Zahl habe die in die Milliarden geht, komm plötzlich eine total dumme Zahl heraus!

Habe mal zum Test folgendes gemacht:

PHP:
$money = "5000000000";

echo "Vorher: ".$money;

$money = (int) $money;

echo "<br>";

echo "Danach: ".$money;

Da kommt raus:

Vorher: 5000000000
Nachher: 2147483647

Kann mir jemand den Sinn davon erklären ...
 
Das hat wahrscheinlich was mit dem Wertebereich von Integer zu tun. Der liegt glaub' ich bei irgendwas von 4 Milliarden. Schau mal auf der PHP-Seite wie groß der Bereich bei int ist.
 
Damit liegt C-H genau richtig, probier es mal mit float, anders geht es nicht. Denn der Integer geht nur bis 2147483648
 
Zitat PHP Handbuch
Integers can be specified in decimal (base 10), hexadecimal (base 16), or octal (base 8) notation, optionally preceded by a sign (- or +).

Da hast du deine maximale int-Zahl.

Für Größere Werte musst du mit Floates rechnen. Für Ganzzahlen dann z.B. eben das so machen:
PHP:
echo floor(5000000000.23);
 
Was Michael damit sagen will ist glaube ich, dass es keine genau Grenze für Integer in PHP gibt. Diese ist nämlich Platform abhängig, siehe PHP-Manual.
PHP-Manual hat gesagt.:
The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.
Mit float bist du also richtig bedient.
MfG, Andy
 
Zurück