Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Does a multiline IF() statement NEED curly braces??

    I came across this code, and I'm wondering whether the {} (or lack there of) are optional?
    Is this clip (a direct copy&paste)
    PHP Code:
    $fp fopen('records_all_fields_truncated.csv''r');
        if (!
    fp)
        
        
    $i=0;
        while ((
    $data fgetcsv($fp1000",")) != FALSE
        {
            
    $data00 str_replace("'","",$data[1]);
            
    $data11 str_replace("'","",$data[2]);
            
    $data33 str_replace("'","",$data[4]);
            
    $data55 str_replace("'","",$data[6]);
            ... 
    equivalent to
    PHP Code:
    $fp fopen('records_all_fields_truncated.csv''r');
        if (!
    fp)
        {    
    // ## Curly-brace added here  ##
            
    $i=0;
            while ((
    $data fgetcsv($fp1000",")) != FALSE
            {
                
    $data00 str_replace("'","",$data[1]);
                
    $data11 str_replace("'","",$data[2]);
                ...
        }    
    //  ## Curly-brace added here  ## 
    I thought it would need them if it went into multiple lines.
    Yes? No? Maybe So?
    ~ Mo
    Last edited by mOrloff; 02-04-2010 at 10:27 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Curly braces are required for multiple line processing.
    The blocks are not equivilent; the first example is an error.
    Hmm, under the assumption that whitespace is ignored, the first block will only set $i = 0 if it could not open the file. Both are incorrect though, as this is specifically indicating an if block for if the file could not be opened yet uses the $fp as a valid file pointer.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Thanks.

    My initial suspicion was that the developer must have changed his mind on how he wanted to do things, and left an artifact.
    But then I started second-guessing myself.

    Based on how I understand what you're saying, I should have gone with my gut.

    ~ Mo
    Last edited by mOrloff; 02-04-2010 at 11:12 PM.

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    That's it's braced equivalent.

    PHP Code:
    $fp fopen('records_all_fields_truncated.csv''r');
        if (!
    fp)
        {
            
    $i=0;
        }

        while ((
    $data fgetcsv($fp1000",")) != FALSE
        {
            
    $data00 str_replace("'","",$data[1]);
            
    $data11 str_replace("'","",$data[2]);
            
    $data33 str_replace("'","",$data[4]);
            
    $data55 str_replace("'","",$data[6]);
            ... 

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Still no validation, this would be probably what is intended:
    PHP Code:
    if ($fp fopen('records_all_fields_truncated.csv''r'))
    {
        
    $i 0;
        while ((
    $data fgetcsv($fp1000",")) != FALSE)
        ....

    $fp needs to be a file pointer, or the fgetcsv could fail.

    But yes, according to the initial block, MattF's code is what it should be. Thats why I was saying that both are improper.

    Lol, just noticed that we're treating the $fp as a FILE *, so it should be if (!$fp) instead of if (!fp).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    btw,
    Code:
    if(some-condition)
    while(condition){
    ..........
    ..........
    
    }
    is equivalent to

    Code:
    if(some-condition){
    	while(condition){
    	..........
    	..........
    
    	}
    }
    . However, putting the curly braces after if is a good practice in similar situations.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •