Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 请问关于php代码运行顺序问题

请问关于php代码运行顺序问题

Jun 23, 2016 pm 01:31 PM

麻烦会的朋友帮忙回答下 以下的问题, 谢谢啦。
在网上下载了套代码。 如下 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}));
Copy after login
bool(false)

Statement of this Website
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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1242
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

Explain the difference between self::, parent::, and static:: in PHP OOP. Explain the difference between self::, parent::, and static:: in PHP OOP. Apr 09, 2025 am 12:04 AM

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles