[英]How do i print separate digits from a 3 digit php variable?
就像我試圖通過使用 SQL 查詢來使用數據一樣。 之后, PHP變量包含123 。 現在我想在 html 頁面上分別打印1、2和3 。
字符串基本上是一個字符數組,因此您可以通過 position 輕松訪問每個字符。 您需要確保您的值是一個字符串 - 如果它是一個數字,您需要強制轉換 它。
<?php
$value = 123;
// Cast our value to a string
$stringValue = (string) $value;
$firstChar = $stringValue[0];
$secondChar = $stringValue[1];
$thirdChar = $stringValue[2];
echo $firstChar.PHP_EOL;
echo $secondChar.PHP_EOL;
echo $thirdChar.PHP_EOL;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.