dtds8802 2015-02-01 09:32
浏览 45

PHP - 用fopen()理解“if循环”的表达式

I have a little problem of understanding with one of the exercise of my PHP learning book (Wrox, beginning with PHP 5.3).

This code is the part when we check is the files "count" exists. If it does not, we need to create it. Really not a high level of complexity, but I do have a problem with it. Here is how I would write it:

  1. Check if the files exists with !files_exist()
  2. If not, create it and initialise it to 0 with fopen() and fwrite()
  3. When it is not possible to create it, then (else) display a error message

Here is how they wrote it and I saw this structure a couple of time. When something is to be executed it comes in the "else" expression, at the end. In my logic, I would place to code to be executed in the "if" expression.

$counterFile = “./count.dat”;

if ( !file_exists( $counterFile ) ) {
   if ( !( $handle = fopen( $counterFile, “w” ) ) ) {
   die( “Cannot create the counter file.” );
   } else {
           fwrite( $handle, 0 ); 
           fclose( $handle );
   }
}

My questions:

1) Are there rules how to structure loop and why is a method better?

2) Is the code, inside the expression, executed or just checked if true?

$counterFile = “./count.dat”;

if ( !file_exists( $counterFile ) ) {
   if ( !( $handle = fopen( $counterFile, “w” ) ) ) {
   die( “Cannot create the counter file.” );
   } else {
           fopen( $counterFile, “w” ) // I thought this code is not executed in the if expression? This is why I would declare it right here.
           fwrite( $handle, 0 ); 
           fclose( $handle );
   }
}

Thank you for your help. have a nice week.

  • 写回答

3条回答 默认 最新

  • doudou5023 2015-02-01 09:56
    关注

    1)

    Whatever makes most sense to you and will (hopefully) be easiest for others reading your code to understand.
    So if it makes sense to test for the negative case and then create a file. You might not need any else part of your if statement.
    In your example above, you could write your second if statement without the else part and get the same result:

    if ( !file_exists( $counterFile ) ) {
        if ( !( $handle = fopen( $counterFile, “w” ) ) ) {
            die( “Cannot create the counter file.” );
        } 
        // if the file couldn't be opened above, the program won't get to here, because of the _die()_
        // otherwise the file is open, no need to open again 
        //   fopen( $counterFile, “w” ) 
        fwrite( $handle, 0 ); 
        fclose( $handle );
    }
    

    Or you might consider it easier to read if you have the normal case first and then deal with the error in the else block, this avoids the ! (not) which can get confusing in a complicated expression.

    if ( !file_exists( $counterFile ) ) {
        if ( $handle = fopen( $counterFile, “w” ) ) { 
            fwrite( $handle, 0 ); 
            fclose( $handle );
        }
        else{//$handle not 'true'
            die( “Cannot create the counter file.” );
        } 
    
    }
    

    2)

    In order to decide if the condition for the if statement is true or not, the expression in the condition needs to be evaluated. So if the expression involves running a function, (in your case file_exists and fopen()) that function will be run to get a result.
    Once the condition is evaluated to either true or false, either the then block or the else block is executed next, but never both.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题