The difference between return and echo in php function
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=>'apple',1=>'banana',2=>'cat',3=>'dog',4=>'egg','5'=>'father'); //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 = '<ul>'; foreach ($arr as $key => $value) { //里面的for循环 是 循环出con内容 foreach ($con as $value2) { // .= 往后添加更多 连续定义变量 // 如果第一层数据循环出来的值,和第二层条件循环出现的值相同,添加到 变量里 //多个for循环来过滤数据也称为 递归 if ($value == $value2) { $code .= '<li>'.$value.'</li>'; } } } $code .= '</ul>'; //循环得到结果后 返回变量。所以,这个函数就是一个字符串 return $code; } //function 用法3 //在函数里 echo 和 return 有什么区别 看执行结果 function f_3($arr,$con){ //先实例一个变量 echo '<ul>'; foreach ($arr as $key => $value) { //里面的for循环 是 循环出con内容 foreach ($con as $value2) { // .= 往后添加更多 连续定义变量 // 如果第一层数据循环出来的值,和第二层条件循环出现的值相同,添加到 变量里 //多个for循环 去过滤数据也称为 递归 if ($value == $value2) { echo '<li>'.$value.'</li>'; } } } echo '</ul>'; } ?> f_1 output start<br/> <?php //因为 f_1 是一个数组,我们可以打印出来 print_r(f_1($array,'banana')); ?> <br/>f_1 output end <hr/><br/> f_2 output start<br/> <?php //f_2 是变量 $con = array('apple','father'); echo f_2($array,$con); ?> <br/>f_2 output end <hr/><br/> f_2 output start<br/> <?php //f_3 已经在函数里面echo 了,所以在函数执行时不用echo $con = array('apple','father'); f_3($array,$con); ?> <br/>f_2 output end
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!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

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

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.

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.

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

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" 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

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. 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 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
