Welcome! Log In Create A New Profile

Advanced

[PHP] Difficulty with semicolon

Posted by Jeffry Killen 
Jeffry Killen
[PHP] Difficulty with semicolon
April 18, 2018 10:30PM
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
Ken Robinson
Re: [PHP] Difficulty with semicolon
April 18, 2018 10:50PM
It's not a syntax error, so no error message is needed. The semi-colon just
ends the statement. At that point the value of the if statement is not
used. The next statement is executed as though there was no if statement
there. Maybe a warning message could be issued in this case, but it
wouldn't stop the program from running.

Ken Robinson

On Wed, Apr 18, 2018 at 4:28 PM, Jeffry Killen <[email protected]> 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
>
>
David Harkness
Re: [PHP] Difficulty with semicolon
April 18, 2018 11:10PM
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 <[email protected]> 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
>
>
Jeffry Killen
Re: [PHP] Difficulty with semicolon
April 19, 2018 12:00AM
> On Apr 18, 2018, at 1:59 PM, David Harkness <[email protected]> wrote:
>
> 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
>
>

Thanks for the reply.
In an attempt to test an intercept a variable value early in a function execution for an async request,
I put in a call to file_put_contents and write a text file with the variable value so I could open the file and
see what its status and value was. Then there re is a return statement so the rest of the code would not run.
I got a warning: code following return is unreachable.

Also your first example might look in code like

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

Yes the semicolon occurs after the closing parenthesis but it is also after the the last closing parenthesis in the
construct: something like

do(s)
{// code; }
while(x);

but if you had
do(s);
{ // code}
while(x)

I presume the code after do(s); would run once: would this cause a parse error for trying to reconcile the while statement
because the do statement needs an associated while statement?
I would think so. I have never used do/while and never had success using while statement, accept where it is already part
of code I have copied into my scripts and published as freely usable.

But, nonetheless, it is my task to avoid typos.

JK
>
>
> On Wed, Apr 18, 2018 at 1:28 PM, Jeffry Killen <[email protected]> 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
>
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jeffry Killen
Re: [PHP] Difficulty with semicolon
April 19, 2018 12:10AM
> On Apr 18, 2018, at 2:49 PM, Jeffry Killen <[email protected]> wrote:
>
>
>> On Apr 18, 2018, at 1:59 PM, David Harkness <[email protected]> wrote:
>>
>> 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
>>
>>
>
> Thanks for the reply.
> In an attempt to test an intercept a variable value early in a function execution for an async request,
> I put in a call to file_put_contents and write a text file with the variable value so I could open the file and
> see what its status and value was. Then there re is a return statement so the rest of the code would not run.
> I got a warning: code following return is unreachable.
>
> Also your first example might look in code like
>
>> while (!keepTrying()) /* keep trying... */ ;
>
> Yes the semicolon occurs after the closing parenthesis but it is also after the the last closing parenthesis in the
> construct: something like
>
> do(s)
> {// code; }
> while(x);
>
> but if you had
> do(s);
> { // code}
> while(x)

OOPS; I don't think I should have had do()
So it would be
> do
> { // code}
> while(x)
and
> do;
> { // code}
> while(x)
JK

>
> I presume the code after do(s); would run once: would this cause a parse error for trying to reconcile the while statement
> because the do statement needs an associated while statement?
> I would think so. I have never used do/while and never had success using while statement, accept where it is already part
> of code I have copied into my scripts and published as freely usable.
>
> But, nonetheless, it is my task to avoid typos.
>
> JK
>>
>>
>> On Wed, Apr 18, 2018 at 1:28 PM, Jeffry Killen <[email protected]> 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
>>
>>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
David Harkness
Re: [PHP] Difficulty with semicolon
April 19, 2018 12:30AM
Correct, it would be harder to have a typo in the do..while() because it
would complain about a missing while().

do;
{ ... } // <-- syntax error, expected while
while (!doSomething());

If you wanted an empty in there, you'd just use while() by itself.

do
/* keep trying... */ ;
while (!doSomething());

And this, ladies and gentlemen, is why putting the opening brace on the
line above is the *correct* way to write these constructs! ;) It makes
these typos less likely because they stand out as obviously broken:

if (...) {; // <-- tho this is syntactically correct and will cause
no problems when run; it's just an empty statement
...
}

and

if (...); { // <-- the situation you found yourself in, but probably
less likely to occur this way
...
}

Cheers,
David


On Wed, Apr 18, 2018 at 3:06 PM, Jeffry Killen <[email protected]> wrote:

>
> > On Apr 18, 2018, at 2:49 PM, Jeffry Killen <[email protected]> wrote:
> >
> >
> >> On Apr 18, 2018, at 1:59 PM, David Harkness <[email protected]>
> wrote:
> >>
> >> 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
> >>
> >>
> >
> > Thanks for the reply.
> > In an attempt to test an intercept a variable value early in a function
> execution for an async request,
> > I put in a call to file_put_contents and write a text file with the
> variable value so I could open the file and
> > see what its status and value was. Then there re is a return statement
> so the rest of the code would not run.
> > I got a warning: code following return is unreachable.
> >
> > Also your first example might look in code like
> >
> >> while (!keepTrying()) /* keep trying... */ ;
> >
> > Yes the semicolon occurs after the closing parenthesis but it is also
> after the the last closing parenthesis in the
> > construct: something like
> >
> > do(s)
> > {// code; }
> > while(x);
> >
> > but if you had
> > do(s);
> > { // code}
> > while(x)
>
> OOPS; I don't think I should have had do()
> So it would be
> > do
> > { // code}
> > while(x)
> and
> > do;
> > { // code}
> > while(x)
> JK
>
> >
> > I presume the code after do(s); would run once: would this cause a parse
> error for trying to reconcile the while statement
> > because the do statement needs an associated while statement?
> > I would think so. I have never used do/while and never had success using
> while statement, accept where it is already part
> > of code I have copied into my scripts and published as freely usable.
> >
> > But, nonetheless, it is my task to avoid typos.
> >
> > JK
> >>
> >>
> >> On Wed, Apr 18, 2018 at 1:28 PM, Jeffry Killen <[email protected]>
> 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
> >>
> >>
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Sorry, only registered users may post in this forum.

Click here to login