首頁 > 後端開發 > php教程 > 菜鸟问个简单的逻辑问题,求解答

菜鸟问个简单的逻辑问题,求解答

WBOY
發布: 2016-06-23 14:15:02
原創
1059 人瀏覽過

逻辑 菜鸟 简单的

我是想实现62进制的功能,可是下面这段代码只能echo出来,不能返回,不知道是什么原因,求高手解答

function dwz($id,$str=""){	$a=array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");	$zs=(int)($id/sizeof($a));	$xs=$id%sizeof($a);	if($zs>=sizeof($a)){		$str=$a[$xs].$str;		dwz($zs,$str);	}	else{		if($str==""){			return $a[$zs].$a[$xs];		}		else{			echo   $a[$zs].$str;//这里只能输出			return $a[$zs].$str;//返回没值,不知道什么原因		}	}}for($i=999990;$i<=1000000;$i++){	echo dwz($i);	echo "<br>";}
登入後複製


回复讨论(解决方案)

第7行 dwz($zs,$str);
没有承接返回
$str = dwz($zs,$str);

函数结束处还需要有 return $str;

    if($zs>=sizeof($a)){         $str=$a[$xs].$str;         dwz($zs,$str); //这里加入return: return dwz($zs, $str);    } 
登入後複製

第7行 dwz($zs,$str);
没有承接返回
$str = dwz($zs,$str);

函数结束处还需要有 return $str;

哈哈,可以啦,谢谢你的提醒和指导

这样写也可以,可逆的前不限长度

echo convert_62(999990); //4C8secho convert_62('4C8s', 1); //999990function convert_62($s, $mode=0) {  $d = str_split('0123456789ABCDEFGHIJKLMNOPQRSTUVEXYZabcdefghijklmnopqrstuvwxyz');  $r = '';  if($mode) {    $d = array_flip($d);    for($i=0; $i<strlen($s); $i++) $r = bcmul($r, '62') + $d[$s{$i}];  }else {    while($s) {      $r = $d[bcmod($s, '62')] . $r;      $s = bcdiv($s, '62');    }  }  return $r;}
登入後複製
登入後複製
登入後複製

这样写也可以,可逆的前不限长度

echo convert_62(999990); //4C8secho convert_62('4C8s', 1); //999990function convert_62($s, $mode=0) {  $d = str_split('0123456789ABCDEFGHIJKLMNOPQRSTUVEXYZabcdefghijklmnopqrstuvwxyz');  $r = '';  if($mode) {    $d = array_flip($d);    for($i=0; $i<strlen($s); $i++) $r = bcmul($r, '62') + $d[$s{$i}];  }else {    while($s) {      $r = $d[bcmod($s, '62')] . $r;      $s = bcdiv($s, '62');    }  }  return $r;}
登入後複製
登入後複製
登入後複製

受用了,谢谢你

这样写也可以,可逆的前不限长度

echo convert_62(999990); //4C8secho convert_62('4C8s', 1); //999990function convert_62($s, $mode=0) {  $d = str_split('0123456789ABCDEFGHIJKLMNOPQRSTUVEXYZabcdefghijklmnopqrstuvwxyz');  $r = '';  if($mode) {    $d = array_flip($d);    for($i=0; $i<strlen($s); $i++) $r = bcmul($r, '62') + $d[$s{$i}];  }else {    while($s) {      $r = $d[bcmod($s, '62')] . $r;      $s = bcdiv($s, '62');    }  }  return $r;}
登入後複製
登入後複製
登入後複製


不好意思,请问下为什么我把这个文件放在本地测试可以用,但是放在服务器上不能用,会提示找不到bcmod这个函数

嗯,这是 php_bc 扩展没加载的原因(php for win 是自动加载的)
你还可以检查一下 php_gmp 扩展是否已加载,用这个函数库也是一样的

嗯,这是 php_bc 扩展没加载的原因(php for win 是自动加载的)
你还可以检查一下 php_gmp 扩展是否已加载,用这个函数库也是一样的

真的没有加载,那怎么办啊?
我没服务器的权限

print_r(get_loaded_extensions());
看看都有些什么

print_r(get_loaded_extensions());
看看都有些什么

Array(    [0] => date    [1] => libxml    [2] => openssl    [3] => pcre    [4] => zlib    [5] => ctype    [6] => curl    [7] => dom    [8] => filter    [9] => ftp    [10] => gd    [11] => hash    [12] => iconv    [13] => json    [14] => mbstring    [15] => mcrypt    [16] => mhash    [17] => mysql    [18] => SimpleXML    [19] => SPL    [20] => PDO    [21] => posix    [22] => Reflection    [23] => session    [24] => pdo_sqlite    [25] => sockets    [26] => SQLite    [27] => standard    [28] => tokenizer    [29] => xml    [30] => xmlreader    [31] => xmlwriter    [32] => zip    [33] => apache2handler    [34] => memcache    [35] => mssql    [36] => soap    [37] => Zend Optimizer)
登入後複製

没权限就不好办了
就自己写吧,你不是也写了吗

没权限就不好办了
就自己写吧,你不是也写了吗

我写的那个有错,发现运算到后面就错了,然后就用了你的
还是没搞清楚我的那个错在哪里,为什么运算到后面就错了
大神,帮忙看下吧

function dwz($id,$str=""){$a=array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");	$zs=(int)($id/sizeof($a));	$xs=$id%sizeof($a);	if($zs>=sizeof($a)){		$str=$a[$xs].$str;		$str=dwz($zs,$str);	}	else{		if($str==""){return $a[$zs].$a[$xs];}		else{return $a[$zs].$str;}	}	return $str;}
登入後複製


这是我根据你的提示改的,可以显示出来,但是我之前的算法有错,帮忙看下

终于可以了,谢谢大神

function dwz($s) {  $d = str_split('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVEXYZ');  $r = '';  while($s) {	$r = $d[(int)($s%62)].$r;	$s = (int)($s/62);  }  return $r;}
登入後複製
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板