intval

(PHP 4, PHP 5, PHP 7, PHP 8)

intvalBir değişkenin tamsayı değerini döndürür

Açıklama

function intval(mixed $değişken, int $taban = 10): int

Belirtilen değişkenin taban tabanında tamsayı değerini döndürür (10 tabanı öntanımlıdır). İşlev nesnelerle kullanılamaz, aksi takdirde E_WARNING seviyesinde bir hata çıktılanır ve işlev 1 döndürür.

Bağımsız Değişkenler

değişken

Bir tamsayıya dönüştürülecek bir sayıl değer.

taban

Dönüşülecek sayı tabanı.

Bilginize:

taban 0 ise, taban değerin biçemine göre saptanır:

  • Dizge bir "0x" (veya "0X") öneki içeriyorsa, taban 16'dır; aksi takdirde,

  • dizge "0b" (veya "0B") öneki içeriyorsa, taban 2'dir (ikli); aksi takdirde,

  • dizge "0" ile başlıyorsa, taban 8'dir; aksi takdirde,

  • taban 10 'dur.

Dönen Değerler

Başarı durumunda belirtilen değerin tamsayı değeriyle, başarısızlık durumunda 0 döner. Boş diziler ve nesneler 0 döndürür. Boş olmayan diziler ise 1 döndürür.

Azami değer sisteme bağlıdır. 32 bitlik sistemlerde işaretli tamsayılar için azami değerler -2147483648 ve 2147483647'dir. Bu bakımdan, böyle sistemlerde örneğin intval('1000000000000') değeri 2147483647 döndürür. 64 bitlik sistemlerde işaretli tamsayılar için azami değer 9223372036854775807'dir.

Dizgeler için dönecek değer genellikle en soldaki karaktere bağlıysa da çoğunlukla 0 döner. Genel kurallar için integer türüne dönüşüm bölümüne bakınız.

Sürüm Bilgisi

Sürüm: Açıklama
8.0.0 Nesneden dönüşümde hata düzeyi E_NOTICE iken E_WARNING oldu.

Örnekler

Örnek 1 - intval() örnekleri

Aşağıdaki örnekler 64 bitlik sistemler içindir.

<?php
echo intval(42);                      // 42
echo intval(4.2);                     // 4
echo intval('42');                    // 42
echo intval('+42');                   // 42
echo intval('-42');                   // -42
echo intval(042);                     // 34
echo intval('042');                   // 42
echo intval(1e10);                    // 10000000000
echo intval('1e10');                  // 10000000000
echo intval(0x1A);                    // 26
echo intval('0x1A');                  // 0
echo intval('0x1A', 0);               // 26
echo intval(42000000);                // 42000000
echo intval(420000000000000000000);   // -4275113695319687168
echo intval('420000000000000000000'); // 9223372036854775807
echo intval(42, 8);                   // 42
echo intval('42', 8);                 // 34
echo intval(array());                 // 0
echo intval(array('foo', 'bar'));     // 1
echo intval(false);                   // 0
echo intval(true);                    // 1
?>

Notlar

Bilginize:

Belirtilen değişken bir dizge olmadıkça taban bağımsız değişkeninin bir etkisi yoktur.

Ayrıca Bakınız

add a note

User Contributed Notes 17 notes

up
375
Ken
14 years ago
Not mentioned elsewhere: intval(NULL) also returns 0.
up
80
leon at leonidasjp dot nl
9 years ago
It seems intval is interpreting valid numeric strings differently between PHP 5.6 and 7.0 on one hand, and PHP 7.1 on the other hand.

<?php
echo intval('1e5');
?>

will return 1 on PHP 5.6 and PHP 7.0,
but it will return 100000 on PHP 7.1.
up
160
winbill at hotmail dot com
15 years ago
Be careful :

<?php
$n="19.99";
print intval($n*100); // prints 1998
print intval(strval($n*100)); // prints 1999
?>
up
50
zak at php dot net
26 years ago
intval converts doubles to integers by truncating the fractional component of the number.

When dealing with some values, this can give odd results.  Consider the following:

print intval ((0.1 + 0.7) * 10);

This will most likely print out 7, instead of the expected value of 8.

For more information, see the section on floating point numbers in the PHP manual (http://www.php.net/manual/language.types.double.php)

Also note that if you try to convert a string to an integer, the result is often 0.

However, if the leftmost character of a string looks like a valid numeric value, then PHP will keep reading the string until a character that is not valid in a number is encountered.

For example:

"101 Dalmations" will convert to 101

"$1,000,000" will convert to 0 (the 1st character is not a valid start for a number

"80,000 leagues ..." will convert to 80

"1.4e98 microLenats were generated when..." will convert to 1.4e98

Also note that only decimal base numbers are recognized in strings.

"099" will convert to 99, while "0x99" will convert to 0.

One additional note on the behavior of intval.  If you specify the base argument, the var argument should be a string - otherwise the base will not be applied.

For Example:

print intval (77, 8);   // Prints 77
print intval ('77', 8); // Prints 63
up
38
Ben Laurienti
20 years ago
You guys are going to love this.  I found something that I found quite disturbing.

$test1 = intVal(1999);

$amount = 19.99 * 100;
$test2 = intVal($amount);
$test3 = intVal("$amount");

echo $test1 . "<br />\n";
echo $test2 . "<br />\n";
echo $test3 . "<br />\n";

expected output:
1999
1999
1999

actual output
1999
1998
1999

Appears to be a floating point issue, but the number 1999 is the only number that I was able to get to do this.  19.99 is the price of many things, and for our purpose we must pass it as 1999 instead of 19.99.
up
35
spoon_reloaded at gmail dot com
17 years ago
Here is a really useful undocumented feature:

You can have it automatically deduce the base of the number from the prefix of the string using the same syntax as integer literals in PHP ("0x" for hexadecimal, "0" for octal, non-"0" for decimal) by passing a base of 0 to intval():

<?php
echo intval("0x1a", 0), "\n"; // hex; prints "26"
echo intval