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?
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";
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.
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.
$foo = 5;
$foo = (string)$foo;
Innym sposobem jest ujęcie w cudzysłów:
$foo = 5;
$foo = "$foo"
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żę.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";
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');
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
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;
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.
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:)
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 .
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
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);
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."";
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