您的位置 首页 php

PHP语法:goto 语句

问题

当 PHP 在执行代码过程,在某一时刻我们希望它能跳转到某一特定位置继续执行代码,该怎么做呢?

回答

在 PHP 中,我们可以使用 goto 操作符来使 PHP 代码执行器跳转到程序中某一特定位置。goto 的使用有一定限制,如:无法跳出一个函数或类,无法从外部跳入到一个函数,无法跳入到任何循环或者 switch 结构中。但可以跳出循环或者 switch,通常的用法是用 goto 代替 switch 中嵌套多层的 break。

语法

goto 会使 PHP 直接跳转到指定标志位置。

goto 标志;代码块标志:代码块

示例

例 1 —— 尝试跳入循环

<?phpgoto loop;for($i=0; $i<3; $i++) {    while($i++) {        loop:    }}echo "End";

运行结果:

Fatal error: 'goto' into loop or switch statement is disallowed in F:\index.php on line 3

从运行结果可以看出,goto 不能从外部直接跳入循环中。

例 2 —— 一个简单的跳转

<?phpgoto loop;echo '这是第一个句子。';loop:echo '这是第二个句子。';

运行结果:

这是第二个句子。

例 3 —— 跳出循环

<?phpfor($i=0; $i<10; $i++) {    while($i++) {        if($i==5) goto end;    }}echo '此时 $i=10';end:echo '此时 $i=' . $i;

运行结果:

此时 $i=5

从例 3 运行结果可以看出,在我们需要从循环中跳出的时候,使用 goto 非常方便。

提示

虽然 goto 使用起来非常方便,但是随意使用 goto 语句极易造成代码逻辑混乱,所以应当谨慎使用。

以上就是PHP语法:goto 语句的详细内容,更多请关注求知技术网其它相关文章!

文章来源:智云一二三科技

文章标题:PHP语法:goto 语句

文章地址:https://www.zhihuclub.com/3847.shtml

关于作者: 智云科技

热门文章

发表回复

您的电子邮箱地址不会被公开。

232条评论

  1. What’s Happening i’m new to this, I stumbled upon this I’ve found It absolutely helpful and it has helped me out loads.
    I’m hoping to contribute & help other customers like
    its helped me. Great job.

  2. My brother suggested I might like this website. He was entirely right.

    This post truly made my day. You can not imagine simply how much time I had spent for this information! Thanks!

  3. Somebody essentially lend a hand to make significantly articles I’d state.
    That is the very first time I frequented your web page and up to now?
    I amazed with the analysis you made to create this particular put up incredible.
    Magnificent job!

  4. I love reading through a post that can make people think.

    Also, many thanks for permitting me to comment!

  5. Hi my family member! I want to say that this article is awesome, great
    written and come with approximately all important infos.
    I’d like to peer extra posts like this .

  6. Thanks to my father who stated to me concerning this web site, this website
    is actually awesome.

  7. It’s very simple to find out any topic on web as compared to books, as I
    found this paragraph at this web page.

  8. It’s very simple to find out any topic on web as compared
    to books, as I found this paragraph at this web page.

  9. great points altogether, you just won a new reader. What might
    you suggest about your publish that you simply made some days in the
    past? Any sure?

  10. Thank you, I’ve recently been searching for information approximately this
    topic for a while and yours is the greatest I have
    found out so far. However, what in regards to the conclusion? Are
    you sure about the source?

  11. Its such as you read my mind! You seem to know a lot about
    this, such as you wrote the guide in it or something.
    I think that you simply can do with some percent to force the message house a bit,
    but other than that, this is fantastic blog. A great read.

    I’ll definitely be back.

  12. You made some really good points there. I looked on the internet
    to find out more about the issue and found most people will go along
    with your views on this site.

  13. It’s difficult to find experienced people in this particular topic, but you sound like you know
    what you’re talking about! Thanks

  14. [url=https://nolvadex.store/]nolvadex for sale online[/url] [url=https://avodart365.com/]online avodart without prescription[/url] [url=https://clonidine.boutique/]can i buy clonidine over the counter uk[/url] [url=https://buyclonidine.shop/]clonidine 0.1 mg online[/url] [url=https://albendazole.shop/]albenza prescription drug[/url] [url=https://buyzoloft.store/]zoloft 200 mg pill[/url] [url=https://diflucan.shop/]can you buy diflucan over the counter in canada[/url] [url=https://retinoa.shop/]retino 0.025 cream[/url]

网站地图