


Unexpected output of PHP recursive function: Why does a simple addition function print '85' instead of '8'?
Apr 01, 2025 am 06:12 AMDetailed explanation of PHP recursive function: analyzing the unexpected output of the addition function
This article analyzes an example of a PHP recursive function to explain why its output results do not match expectations. The code is as follows:
<?php function sd($a=3,$b=2){ $c = $a $b; if($c < 6){ sd($a,$c); } echo $c; } sd(); ?>
The function sd()
accepts two parameters a
and b
, and the default values ??are 3 and 2 respectively. The function calculates the sum of a
and b
and assigns the value to c
. If c
is less than 6, then recursively call itself and pass a
and the new c
values ??as parameters. Finally, the function prints the value of c
.
After executing sd()
, the output result is "85", rather than the expected "8". This is because there is a bias in understanding the order of recursive calls and echo
statement execution.
The program execution process is as follows:
- The first call to
sd()
is called,a=3
,b=2
,c = 3 2 = 5
. Sincec , the function calls itself recursively and the parameters become <code>sd(3, 5)
. - In recursive calls,
a=3
,b=5
,c = 3 5 = 8
. At this timec >= 6
, the recursion ends.echo $c;
print 8. - The program returns to the location where the first call
sd()
is called.echo $c;
prints thec
value at the first call, which is 5.
Therefore, the final output is "85". It is not that the variable c
is not overwritten, but that the echo
statement is at the end of the function and is executed every time the recursive ends, resulting in two prints.
To get the result "8", you need to modify the function logic, such as placing the echo
statement in if
conditional statement, or printing the final result only at the end of recursion. The modified code can be as follows:
<?php function sd($a=3,$b=2){ $c = $a $b; if($c < 6){ return sd($a,$c); } return $c; } echo sd(); ?>
This modified version uses the return
statement to return the value of c
, ensuring that the final result is printed only after the recursion is over.
The above is the detailed content of Unexpected output of PHP recursive function: Why does a simple addition function print '85' instead of '8'?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In PHP, HTML entities can be decoded efficiently using the html_entity_decode() function. 1) Use the basic syntax $decodedString=html_entity_decode($encodedString); 2) Specify character encoding, such as $decodedString=html_entity_decode($encodedString, ENT_QUOTES,'UTF-8'); 3) Pay attention to character encoding, security and performance issues to ensure decoding effect and data security.

Today, we will reveal a hidden treasure for you - a platform that provides a free comics app entrance, allowing you to easily enjoy the ocean of comics and enjoy the fun of reading. This platform is not just a simple entrance, but more like a caring guide. It brings together various types of comics APPs. Whether you are a loyal fan of Hot-blooded Boys, a fan of romantic girl comics, or a fan of suspense and mystery comics, you can find an app that meets your needs here. More importantly, these apps promise to provide a free reading experience

There are three ways to enter the MySQL database: 1. Log in through the command line, enter "mysql-u username-p" and enter the password as prompted; 2. Use MySQLWorkbench to create a new connection and enter relevant information; 3. Log in through the Python programming language, and use the mysql.connector library to connect to the database.

VSCode was chosen to develop SpringBoot projects because of its lightweight, flexibility and powerful expansion capabilities. Specifically, 1) Ensure the environment is configured correctly, including the installation of JavaJDK and Maven; 2) Use SpringBootExtensionPack to simplify the development process; 3) Manually configure SpringBoot dependencies and configuration files, which requires a deep understanding of SpringBoot; 4) Use VSCode's debugging and performance analysis tools to improve development efficiency. Although manual configuration is required, VSCode provides a high level of custom space and flexibility.

The reason why the editor crashes after the VSCode plugin is updated is that there is compatibility issues with the plugin with existing versions of VSCode or other plugins. Solutions include: 1. Disable the plug-in to troubleshoot problems one by one; 2. Downgrade the problem plug-in to the previous version; 3. Find alternative plug-ins; 4. Keep VSCode and plug-in updated and conduct sufficient testing; 5. Set up automatic backup function to prevent data loss.

Two methods and precautions for downloading Binance on Android phones: 1. Download the APK file through the official website: visit Binance official website www.binance.com, click "Android APK Download", and enable the installation permission of the "Unknown Source" of your phone before completing the installation; 2. Download through a third-party application store: select a trusted store to search for "Binance", confirm the developer information and download and install it. Be sure to get the app from official channels, enable two-factor verification, regularly change passwords and be alert to phishing websites to ensure your account security.

The official entrance website for Lanhaiss Book Search is www.lanhaiss.com. The steps to search for books using Blue Ocean include: 1. Visit the official website; 2. Enter book information in the search box and search; 3. Select the appropriate e-book version; 4. Click the download link and download; 5. Read and manage using an e-book reader or application. The unique functions of Blue Ocean Book Search include: 1. Rich resource library; 2. Efficient search function; 3. Multi-format support; 4. User-friendly interface; 5. Regular update of resource library.

As XRP price trends continue to attract market attention, observers have also turned their attention to emerging crypto projects such as Jetbolt (JBOLT). Although most analysts focus on the latest XRP price forecasts, many people are attracted by Jetbolt (JBOLT)'s outstanding performance in the pre-sale stage. Its pre-sales are progressing rapidly, and the latest 357 million tokens sold is a strong proof. Jetbolt has a series of cutting-edge features, such as zero-gas trading technology. Can this help it soar? At the same time, will the SEC follow-up handling of the Ripple case drive the XRP price to rise? Here is the latest analysis of Jetbolt pre-sales and XRP price trends. XRP Price Outlook: S
