doujiaozhao2489 2013-12-27 04:08
浏览 28
已采纳

PHP何时访问网站时运行?

I have a quick question about PHP, say I am creating a form and there is a code snippet as follows:

<?php if($_POST){ do something here }else{ do something else } ?>

When does this script run? I am more familiar with python and java, where code runs line by line, a brief explanation would be appreciated, Thanks!

  • 写回答

1条回答 默认 最新

  • dongzhijing8202 2013-12-27 04:11
    关注

    The script runs whenever the browser sends a request to the server with the URL scriptname.php. This is no different from if you use python or java to implement your server-side coding.

    If the user simply goes to the URL via a link or entering it into the browser's address bar, there will not be any POST parameters. $_POST will be empty, and the do something here block of code will be executed.

    But if the script is used as the action URL of an HTML form, the inputs will be in $_POST, and the do something else block will be executed.

    This allows the same script to be used to display a form and also process the form data when the user submits it. This is a pretty common idiom in PHP webpage design.

    One of the unique things about PHP is that it's embedded within a page of ordinary text (usually HTML, but it can actually be anything). When PHP is processing the file, anything that's outside <?php ... ?> is simply sent directly to the client. When it encounters <?php, it starts executing it as a script, until it reaches ?>. So if you have a PHP file like this:

    <html>
    <body>
    <?php echo "This is some text"; ?>
    </body>
    </html>
    

    it will put This is some text in the body of the resulting HTML document.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测