Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 菜鸟问个简单的逻辑问题,求解答

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

Jun 23, 2016 pm 02:15 PM

逻辑 菜鸟 简单的

我是想实现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>";}
Copy after login


回复讨论(解决方案)

第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);    } 
Copy after login

第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;}
Copy after login
Copy after login
Copy after login

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

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;}
Copy after login
Copy after login
Copy after login

受用了,谢谢你

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

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;}
Copy after login
Copy after login
Copy after login


不好意思,请问下为什么我把这个文件放在本地测试可以用,但是放在服务器上不能用,会提示找不到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)
Copy after login

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

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

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

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;}
Copy after login


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

终于可以了,谢谢大神

function dwz($s) {  $d = str_split('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVEXYZ');  $r = '';  while($s) {	$r = $d[(int)($s%62)].$r;	$s = (int)($s/62);  }  return $r;}
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles