Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP整数变量_Php_Variables_Integer - Fatal编程技术网

PHP整数变量

PHP整数变量,php,variables,integer,Php,Variables,Integer,我想知道是否有一种方法,wee可以首先分离一个整数变量的值,然后加上或减去各个值 例如: $a=24 2+4=6你的意思是这样的 $a = 24; var_dump(SumEachDigits($a)); // 6 function SumEachDigits(int $int) { $intDigitsArr = str_split(strval($int), 1); return array_reduce( $intDigitsArr,

我想知道是否有一种方法,wee可以首先分离一个整数变量的值,然后加上或减去各个值

例如:

$a=24


2+4=6

你的意思是这样的

$a = 24;
var_dump(SumEachDigits($a)); // 6

function SumEachDigits(int $int) {
    $intDigitsArr = str_split(strval($int), 1);

    return array_reduce(
        $intDigitsArr, 
        function($acc, $val) { return $acc + intval($val); },
        0
    );
}

您可以使用
str\u split()


这里已经回答了您的问题:这是否回答了您的问题?
<?php

$a = 24;
$array = str_split($a); // return Array ( [0] => 2 [1] => 4 )
$sum = $array[0] + $array[1];

echo $sum; //return 6