search
HomeBackend DevelopmentPHP Tutorial请问关于php代码运行顺序问题

麻烦会的朋友帮忙回答下 以下的问题, 谢谢啦。
在网上下载了套代码。 如下 admin.php 文件:
对于以下这段代码, 究竟代码是怎么跑的呢?
也就是比如说 以下的代码,什么时候 才会跑   if ($device == 'ipad')  这句代码呢?

即 想表达以下 这种意思:
 在php的代码中  有时候看到  以下这种情况:
那么, 什么时候 就会运行到 if  (xxw  ) xxxcc;呢? 
如果 不没有运行 function  xxxc ()这个函数, 会运行到  if  (xxw  ) xxxcc;吗?  (不知道说得清楚了没)

if (xxx)xxx;

function  xxxa () {
 
}

function  xxxc () {
 
}

if  (xxw  ) xxxcc;

?>  


//-----------------------------------------------

require 'Service/Init.php';

$act = Get('act');

if (!isset($act{0})){
$act = 'pda';

。。。
。。。
function HavePermissions($pid){
    。。。。。
if ($GLOBALS['user']->HavePermissions($pid))
return true;
SetLocation(-1, '你的权限不足');
}


if ($device == 'ipad')
SetMe('abc', 'width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no');

SetTitle('后台管理');

?>


回复讨论(解决方案)

代码运行顺序 从上至下啊

代码运行顺序 从上至下啊



谢谢     microlab2009    回复
麻烦再帮忙回复下。
其实我想说的是:
function HavePermissions($pid){
     。。。。。
if ($GLOBALS['user']->HavePermissions($pid))
 return true;
 SetLocation(-1, '你的权限不足');
 }


如果 以上这个函数 不运行的话, 是不是以下这段代码  就不会 跑了呢?
if ($device == 'ipad')
 SetMe('abc', 'width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no');

 SetTitle('后台管理');

function HavePermissions($pid){
是函数定义,与运行顺序无关

function HavePermissions($pid){
是函数定义,与运行顺序无关



谢谢  xuzuning    版主的回复

我还想确认下,按照您的意思, 我能这样理解以下这段代码吗?

以下的代码 运行顺序结果是:

$act = Get('act');  --》  if (!isset($act{0})){}   -->   if ($device == 'ipad')   -> SetTitle('后台管理');

也就是   没有跑 function HavePermissions() 这个函数 而直接跳到 运行    if ($device == 'ipad')   这句代码


require 'Service/Init.php';

$act = Get('act');

if (!isset($act{0})){
$act = 'pda';

function HavePermissions($pid){
    。。。。。
if ($GLOBALS['user']->HavePermissions($pid))
return true;
SetLocation(-1, '你的权限不足');
}

if ($device == 'ipad')
SetMe('abc', 'width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no');

SetTitle('后台管理');

?>

不是没有跑 function HavePermissions()
而是 function HavePermissions()本身不影响执行的顺序

如果是没有跑 function HavePermissions()
那么如果后面(或前面)有 HavePermissions() 那不就一团糟了吗

不是没有跑 function HavePermissions()
而是 function HavePermissions()本身不影响执行的顺序

如果是没有跑 function HavePermissions()
那么如果后面(或前面)有 HavePermissions() 那不就一团糟了吗



谢谢   xuzuning  版主  回复

我还有点不太明白 ,还想问下。

比如说  定义了 一个函数 abc,而这个函数abc在这个 admin.php这个页面上是没有被调用的。或者说  只是单单定义了,而没有
使用到。 

那么以下代码的 运行顺序  能这么理解吗?

 $act = Get('act');   if (!isset($act{0})){  }  --》     if ($device == 'ipad')


也就是说 运行完这个if (!isset($act{0})){  }   就直接跳到   if ($device == 'ipad')。而 没有运行 这个函数function abc()
能这么理解吗?

 require 'Service/Init.php';

 $act = Get('act');

 if (!isset($act{0})){
 $act = 'pda';

function abc($pid){
     。。。。。

 }

if ($device == 'ipad')
 SetMe('abc', 'width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no');

 SetTitle('后台管理');

 ?> 

定义是定义,执行是执行。这是两个不同的概念
不要自己把自己的思路搅乱了

虽然 php 允许你书写调用在前,定义在后的代码
但这是 php 在内部保证了 先定义,后使用 的原则
也就是说, 在执行期间,所有定义都已经执行过了

定义是定义,执行是执行。这是两个不同的概念
不要自己把自己的思路搅乱了

虽然 php 允许你书写调用在前,定义在后的代码
但这是 php 在内部保证了 先定义,后使用 的原则
也就是说, 在执行期间,所有定义都已经执行过了



谢谢  xuzuning  版主的回复

还想最后问一个问题,麻烦再回复下,  刚接触php,很多不解。先谢啦

$act = Get('act');

if (!isset(  $act{0} )){
 $act = 'pda'; }

if 语句中 isset($act{0})  这句话应该怎么理解呢? 特别是 $act{0} 这个0 代码的是什么意思呢?
isset($act{0})   是表示 act()中第0位置的变量是否定义和存在。好想这样说不通。



isset($act{0})   是表示 $act 的第0位置是否定义和存在

$a = '';var_dump(isset($a{0}));
bool(false)

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
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram APIIntroduction to the Instagram APIMar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool