Web lists-archives.com

Re: [PHP] Difficulty with semicolon




Unfortunately, empty statements are sometimes needed in real-world code.

    while (!keepTrying())
        /* keep trying... */ ;

I added the indentation for clarity. We enforce the use of braces even for
one-line blocks like this to help.

    while (!keepTrying()) {
        /* keep trying... */ ;
    }

That, however, wouldn't have solved your typo. Since we can't remove the
ability to have empty statements, one could argue for an option to enforce
braces for all blocks, but I expect the developers would deny such a
request with, "just don't make typos!" :) This is where a linter can help.
You can tell PhpStorm and other editors to disallow one-line blocks without
braces, and they would flag that line for you.

Cheers,
David




On Wed, Apr 18, 2018 at 1:28 PM, Jeffry Killen <jekillen@xxxxxxxxxxx> wrote:

> Hello;
> This is the second time I have had to deal with this issue.
> It is the result of my own typing errors. But I thought it would
> be worthwhile sharing the implicit problem.
>
> You may notice the line marked '//<<<'  There is a semicolon immediately
> following the closing ')'.
>
> This is part of a function to create a directory with a default index.php
> file
> This code would not run if the directory had not been created successfully.
>
> What happened was that the error implied by file_put_contents returning
> false
> was sent: EVEN though the file WAS actually created. It appears that the
> semi
> colon caused the false value to be converted to true and the error was
> sent.
> Or it just canceled the conditional test and the error block was run with
> out it.
> (I didn't think it was possible).
>
> So, the take away is that it would be worthwhile for a syntax error to the
> issued
> in this case. I can't think of any conditional or looping construct that
> would use
> a semicolon at this location usefully. And lord knows that if you leave
> one out
> where it needs to be, you hear about it.
>
> $_indTxt = "<?php\n/* code */;\n"."?".">";
>  if(file_put_contents($_tar."/index.php", $_indTxt) === false); // <<<
>    {
>      $_out['error'] = $_errHead." file named ".$_tarShrt."/index.php file
> not created";
>      return $_out;
>    }
>
> Thanks for time and attention.
> JK
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>