Konwersja liczby całkowitej na ciąg znaków w PHP

Czy istnieje sposób na konwersję liczby całkowitej na ciąg znaków w PHP?

Author: YanDatsiuk, 2009-06-23

14 answers

Możesz użyć strval() funkcja do konwersji liczby na ciąg znaków.

Z punktu widzenia utrzymania jest oczywiste, co próbujesz zrobić, a nie niektóre inne bardziej Ezoteryczne odpowiedzi. Oczywiście, to zależy od kontekstu.

$var = 5;

// Inline variable parsing
echo "I'd like {$var} waffles"; // = I'd like 5 waffles

// String concatenation 
echo "I'd like ".$var." waffles"; // I'd like 5 waffles

// The two examples above have the same end value...
// ... And so do the two below

// Explicit cast 
$items = (string)$var; // $items === "5";

// Function call
$items = strval($var); // $items === "5";
 825
Author: Chris Thompson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-26 15:52:10

Można to zrobić na wiele sposobów.

Dwa przykłady:

 $str = (string) $int;
 $str = "$int";     

Zobacz Podręcznik PHP na Types Juggling Po Więcej.

 96
Author: Sanjay Sheth,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-06-23 22:42:57
$foo = 5;

$foo = $foo . "";

Teraz $foo jest ciągiem znaków.

Ale możesz się przyzwyczaić do rzucania. Ponieważ casting jest właściwym sposobem na osiągnięcie czegoś takiego:
$foo = 5;
$foo = (string)$foo;

Innym sposobem jest ujęcie w cudzysłów:

$foo = 5;
$foo = "$foo"
 47
Author: Sev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-12 22:17:38

Istnieje wiele sposobów na "konwersję" liczby całkowitej na ciąg znaków w PHP.

Tradycyjnym sposobem informatyki byłoby oddanie zmiennej jako ciągu znaków:

$int = 5;
$int_as_string = (string) $int;
echo $int . ' is a '. gettype($int) . "\n";
echo $int_as_string . ' is a ' . gettype($int_as_string) . "\n";

Możesz również skorzystać z niejawnej konwersji typu PHP i interpolacji ciągu znaków:

$int = 5;
echo $int . ' is a '. gettype($int) . "\n";

$int_as_string = "$int";
echo $int_as_string . ' is a ' . gettype($int_as_string) . "\n";

$string_int = $int.'';
echo $int_as_string . ' is a ' . gettype($int_as_string) . "\n";

Wreszcie, podobnie jak powyżej, każda funkcja, która akceptuje i zwraca łańcuch, może być użyta do konwersji i liczby całkowitej. Rozważmy następujące:

$int = 5;
echo $int . ' is a '. gettype($int) . "\n";

$int_as_string = trim($int);
echo $int_as_string . ' is a ' . gettype($int_as_string) . "\n";
Nie polecam ostatecznej opcji, ale Widziałem kod w dziczy, który opierał się na tym zachowaniu, więc pomyślałem, że go przekażę.
 11
Author: Alan Storm,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-12 22:21:44

Wszystkie te odpowiedzi są świetne, ale wszystkie zwracają pusty łańcuch, jeśli wartość jest równa zero.

Spróbuj:

    $v = 0;

    $s = (string)$v ? (string)$v : "0";
 10
Author: Microprocessor Cat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-11-20 14:58:19

Użycie:

$intValue = 1;
$string = sprintf('%d', $intValue);

Albo może być:

$string = (string)$intValue;

Lub:

settype(&$intValue, 'string');
 7
Author: gewel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-12 22:22:40

Istnieje wiele możliwych sposobów konwersji:

$input => 123
sprintf('%d',$input) => 123
(string)$input => 123
strval($input) => 123
settype($input, "string") => 123
 4
Author: UserBSS1,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-22 08:09:55

Możesz użyć operatora period I połączyć z nim łańcuch znaków (i będzie on typowany na łańcuch):

$integer = 93;
$stringedInt = $integer . "";

Lub, bardziej poprawnie, możesz po prostu wpisać cast integer do ciągu znaków:

$integer = 93;
$stringedInt = (string) $integer;
 4
Author: Andrew Dunkman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-12 22:18:05

Jak ładnie pokazują odpowiedzi tutaj, tak, istnieje kilka sposobów. Jednak w PHP rzadko trzeba to robić. "Dogmatycznym sposobem" pisania PHP jest poleganie na luźnym systemie pisania języka, który w razie potrzeby będzie transparentnie wymuszał ten typ. Dla wartości całkowitych jest to zwykle bez problemów. Należy jednak bardzo uważać na wartości zmiennoprzecinkowe.

 1
Author: troelskn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-25 09:08:02

Powiedziałbym, że to zależy od kontekstu. Można użyć strval () lub operatora odlewu (string). Jednak w większości przypadków PHP zdecyduje, co jest dla ciebie dobre, jeśli na przykład użyjesz go z echo lub printf...

Jedna mała uwaga: die() potrzebuje string i nie wyświetla żadnego int:)

 0
Author: merkuro,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-12 22:19:40
$amount = 2351.25;
$str_amount = "2351.25";

$strCorrectAmount = "$amount";
echo gettype($strCorrectAmount);    //string

Więc echo będzie return string .

 0
Author: Kaushik shrimali,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-13 05:23:06
$num = 10;
"'".$num."'"

Spróbuj tego

 0
Author: paradox,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-07-15 09:31:02

Możesz po prostu użyć:

$intVal = 5;
$strVal = trim($intVal);
 -1
Author: Imam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-18 22:02:10
$integer = 93;
$stringedInt = $integer.'';

Jest szybszy niż

$integer = 93;
$stringedInt = $integer."";
 -4
Author: Arthur Kushman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-25 09:09:17