Home Backend Development PHP Tutorial The difference between return and echo in php function

The difference between return and echo in php function

Jul 06, 2017 am 10:07 AM
echo function return

This article mainly introduces how to use php function recursively and the difference between return and echo. Friends who need it can refer to it

The code is as follows:

<?php 
//模拟sql数据 
$array = array(0=>&#39;apple&#39;,1=>&#39;banana&#39;,2=>&#39;cat&#39;,3=>&#39;dog&#39;,4=>&#39;egg&#39;,&#39;5&#39;=>&#39;father&#39;); 

//function 用法1 
//arr 是传入的数据 $con 是条件 
function f_1($arr,$con){ 
//这里的 array 是这个函数内私有的,不会和出面的array冲突 
//所以,外地面的 array不里直接在内面用,里面的array也不能直接外面用 
//先实例一个array 
$array = array(); 
//for foreach while 用法都类似,具体baidu 
foreach ($arr as $key => $value) { 
//如果循环出来的 value 等于 con 的话,就把他加入到数组 
if ($value == $con) { 
//数组和变量的区别在于加了个 [] 
$array[] = array($key => $value); 
} 
} 
//循环得到结果后 返回数组。所以,这个函数就是一个数组 
return $array; 
//return 执行后就终断了,无论后面还有什么代码 都不会被执行 
//return可以看做是一个函数结束的地方 
} 


//function 用法2 
//$con 可以是数组 
function f_2($arr,$con){ 
//先实例一个变量 
$code = &#39;<ul>&#39;; 
foreach ($arr as $key => $value) { 
//里面的for循环 是 循环出con内容 
foreach ($con as $value2) { 
// .= 往后添加更多 连续定义变量 
// 如果第一层数据循环出来的值,和第二层条件循环出现的值相同,添加到 变量里 
//多个for循环来过滤数据也称为 递归 
if ($value == $value2) { 
$code .= &#39;<li>&#39;.$value.&#39;</li>&#39;; 
} 
} 
} 
$code .= &#39;</ul>&#39;; 
//循环得到结果后 返回变量。所以,这个函数就是一个字符串 
return $code; 
} 

//function 用法3 
//在函数里 echo 和 return 有什么区别 看执行结果 
function f_3($arr,$con){ 
//先实例一个变量 
echo &#39;<ul>&#39;; 
foreach ($arr as $key => $value) { 
//里面的for循环 是 循环出con内容 
foreach ($con as $value2) { 
// .= 往后添加更多 连续定义变量 
// 如果第一层数据循环出来的值,和第二层条件循环出现的值相同,添加到 变量里 
//多个for循环 去过滤数据也称为 递归 
if ($value == $value2) { 
echo &#39;<li>&#39;.$value.&#39;</li>&#39;; 
} 
} 
} 
echo &#39;</ul>&#39;; 
} 
?> 

f_1 output start<br/> 
<?php 
//因为 f_1 是一个数组,我们可以打印出来 
print_r(f_1($array,&#39;banana&#39;)); 
?> 
<br/>f_1 output end 
<hr/><br/> 
f_2 output start<br/> 
<?php 
//f_2 是变量 
$con = array(&#39;apple&#39;,&#39;father&#39;); 
echo f_2($array,$con); 
?> 
<br/>f_2 output end 
<hr/><br/> 
f_2 output start<br/> 
<?php 
//f_3 已经在函数里面echo 了,所以在函数执行时不用echo 
$con = array(&#39;apple&#39;,&#39;father&#39;); 
f_3($array,$con); 
?> 
<br/>f_2 output end
Copy after login


The above is the detailed content of The difference between return and echo in php function. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

What is the execution order of return and finally statements in Java? What is the execution order of return and finally statements in Java? Apr 25, 2023 pm 07:55 PM

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

In today's era of rapid technological development, programming languages ​​are springing up like mushrooms after a rain. One of the languages ​​that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

Go language development essentials: 5 popular framework recommendations Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

&quot;Go Language Development Essentials: 5 Popular Framework Recommendations&quot; As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

Implementing distributed task scheduling using Golang's web framework Echo framework Implementing distributed task scheduling using Golang's web framework Echo framework Jun 24, 2023 am 11:49 AM

With the development of the Internet and the advancement of information technology, the era of big data has arrived, and fields such as data analysis and machine learning have also been widely used. In these fields, task scheduling is an inevitable problem. How to achieve efficient task scheduling is crucial to improving efficiency. In this article, we will introduce how to use Golang's web framework Echo framework to implement distributed task scheduling. 1. Introduction to the Echo framework Echo is a high-performance, scalable, lightweight GoWeb framework. It is based on HTTP

Detailed explanation of the role and function of the MySQL.proc table Detailed explanation of the role and function of the MySQL.proc table Mar 16, 2024 am 09:03 AM

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Jun 13, 2023 pm 05:01 PM

Laravel is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

See all articles