search
Homephp教程php手册PHP return语句的另一个作用,return语句

PHP return语句的另一个作用,return语句

一直以为,return只能出现在函数中,直到看了bbPress的代码:

<&#63;php
require_once('./bb-load.php');

bb_repermalink(); // The magic happens here.

if ( $self ) {
 if ( strpos($self, '.php') !== false ) {
  require($self);
 } else {
  require( BB_PATH . 'profile-base.php' );
 }
 return;
}

难道 return 还能出现在函数之外?这在C语言是无法想象的。

查了一下 PHP 手册:如果在一个函数中调用 return 语句,将立即结束此函数的执行并将它的参数作为函数的值返回。如果在全局范围中调用,则当前脚本文件中止运行。

唉,受C语言的毒害太深了。

PHP中return的用法与实际作用?

return 的用法其实不难了,它的意思就是返回;

如果执行了return,则return 语句后面的内容将不会被执行了;
return 可以是函数返回值,也可以返回一个空值,这要看你的具体用法了,例如:

function test($a){
if($a>10){
return "a>10";
}else{
return "a}
$b=45;
$c=$b-$a;
echo $c;
}

在这个例子当中,当你调用这个函数的时候,然后给一个任何的数字,它都会返回一个字符串,而代码:$b=45;
$c=$b-$a;
echo $c;
将永远不会被执行
 

PHP中使用return语句,遇到的问题

return 是返回啊 。
echo是输出。也可以print
return不是输出,
 

Statement
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

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools