Re: [PHP] UNLESS Statement Equivalent

2013-03-11 Thread Larry Garfield

On 3/11/13 6:25 PM, Angela Barone wrote:

On Mar 11, 2013, at 4:10 PM, Jonathan Sundquist wrote:

the variable $current_page does not exist.


That was my problem. :(  I've been staring at this for too long.  Too 
bad there's not a 'use strict' pragma.


There is.  Always set your development environment to E_ALL | E_STRICT, 
and it will yell at you about every little thing, including undefined 
variables.


http://php.net/manual/en/function.error-reporting.php


I would also suggest keeping with your original statement to return early and 
return often. Its best to exit out of your functions sooner than later.  
Specially if its a large function.


O.K.  I just thought there might be a more elegant way of doing it.  I 
at least got rid of the else statement like you mentioned.

Thanks for your help,
Angela


If you find yourself with a function that's too long and complex from 
the if-statements, your first step is to break it up into utility functions.


if (...) {
  // Something Long
}
else {
  // Something Else Long
}

Becomes:

if (...) {
  something_long();
}
else {
  something_else_long();
}

function something_long() {

}

function something_else_long() {

}

That helps both readability and testability.

Also, on your original boolean question, note that negation is 
distributive.  That is:


!($a && $b && $c)

is the same as:

!$a || !$b || $!c

Which means that if your checks are all equality checks, as in your 
case, you can simply do:


if ($a != 'a' || $b != 'b' || $c >= 'c') {

}

Which may be easier to read.  If those checks are not trivial then 
there's also a micro-performance benefit there, as the first statement 
that evaluates to true will cause the whole thing to return true, so the 
second and third options don't need to be evaluated.


--Larry Garfield

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] UNLESS Statement Equivalent

2013-03-11 Thread Angela Barone
On Mar 11, 2013, at 4:10 PM, Jonathan Sundquist wrote:
> the variable $current_page does not exist. 

That was my problem. :(  I've been staring at this for too long.  Too 
bad there's not a 'use strict' pragma.

> I would also suggest keeping with your original statement to return early and 
> return often. Its best to exit out of your functions sooner than later.  
> Specially if its a large function.

O.K.  I just thought there might be a more elegant way of doing it.  I 
at least got rid of the else statement like you mentioned.

Thanks for your help,
Angela

Re: [PHP] UNLESS Statement Equivalent

2013-03-11 Thread Jonathan Sundquist
Angela,

the variable $current_page does not exist. so $curent_page does not equal
$saved_page.  Also the ! in front of the entire statement means that all of
this is false.  Since one items is true and not true = false "Don't save"
is echoed out.

If you are looking to save the results based on the above sample it would
look like


if(($page === $saved_page) && ($current_ip === $saved_ip) && ($current_dt <
($saved_dt +3600)) {
 //save the results
} else {
//don't save
}

I would also suggest keeping with your original statement to return early
and return often. Its best to exit out of your functions sooner than later.
 Specially if its a large function.


On Mon, Mar 11, 2013 at 6:03 PM, Angela Barone
wrote:

> On Mar 11, 2013, at 3:47 PM, Ashley Sheridan wrote:
> > if ( !( ($current_page == $saved_page) and ($current_ip == $saved_ip)
> and ($current_dt < ($saved_dt + 3600)) ) )
>
> Hello Ash,
>
> This makes sense to me, but I can't get it to work, so I'm either
> not understanding it or I'm asking the wrong question.  Here's a complete
> scriptlet:
>
>  $saved_page = 'ddd';
> $page   = 'ddd';
> $saved_ip   = '1.1.1.1';
> $ip = '1.1.1.1';
> $saved_dt   = '2013-03-11 11:11:11';
> $current_dt = '2013-03-11 11:22:11';
>
> if ( !( ($current_page == $saved_page) and ($current_ip == $saved_ip) and
> ($current_dt < ($saved_dt + 3600)) ) ) {
> echo 'Save results.';
> } else {
> echo "Don't save.";
> }
> ?>
>
> Using the supplied data, the result should be "Don't save."  Can
> you see what's wrong?
>
> Angela
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


Re: [PHP] UNLESS Statement Equivalent

2013-03-11 Thread Angela Barone
On Mar 11, 2013, at 3:47 PM, Ashley Sheridan wrote:
> if ( !( ($current_page == $saved_page) and ($current_ip == $saved_ip) and 
> ($current_dt < ($saved_dt + 3600)) ) )

Hello Ash,

This makes sense to me, but I can't get it to work, so I'm either not 
understanding it or I'm asking the wrong question.  Here's a complete scriptlet:



Using the supplied data, the result should be "Don't save."  Can you 
see what's wrong?

Angela
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] UNLESS Statement Equivalent

2013-03-11 Thread Jonathan Sundquist
What you have

if ( ($current_page == $saved_page) and ($current_ip == $saved_ip) and
($current_dt < ($saved_dt + 3600)) ) {
return;
} else {
$query  = "UPDATE `table` SET `hits` = '$count', `agent` =
'$agent', `ts` = '$date_time'  WHERE `page` = '$page'";
$result = mysql_query($query) or die ('Error! -- ' . mysql_error());
}

is the same as writing it as

if ( ($current_page == $saved_page) && ($current_ip == $saved_ip) &&
($current_dt < ($saved_dt + 3600)) ) {
return;
}

$query  = "UPDATE `table` SET `hits` = '$count', `agent` = '$agent', `ts` =
'$date_time'  WHERE `page` = '$page'";
$result = mysql_query($query) or die ('Error! -- ' . mysql_error());



On Mon, Mar 11, 2013 at 5:48 PM, Angela Barone
wrote:

> On Mar 11, 2013, at 2:38 PM, Jonathan Sundquist wrote:
>
> Since you already have the return statement with the if statement the else
> isn't required. If those three statements are true you would exit the call
> any ways
>
>
> I don't follow.  The else contains the meat of the statement.
>
> Angela
>


Re: [PHP] UNLESS Statement Equivalent

2013-03-11 Thread Angela Barone
On Mar 11, 2013, at 2:38 PM, Jonathan Sundquist wrote:

> Since you already have the return statement with the if statement the else 
> isn't required. If those three statements are true you would exit the call 
> any ways

I don't follow.  The else contains the meat of the statement. 

Angela

Re: [PHP] UNLESS Statement Equivalent

2013-03-11 Thread Ashley Sheridan
On Mon, 2013-03-11 at 16:38 -0500, Jonathan Sundquist wrote:

> Since you already have the return statement with the if statement the else
> isn't required. If those three statements are true you would exit the call
> any ways
> On Mar 11, 2013 4:33 PM, "Angela Barone" 
> wrote:
> 
> > I'm looking for an 'unless' statement, but as far as I can tell,
> > PHP doesn't have one.  Hopefully someone can help me rewrite my statement.
> >
> > In English, I want to say: "always do something UNLESS these 3
> > conditions are met".
> >
> > The best I've been able to come up with in PHP is this:
> >
> > if ( ($current_page == $saved_page) and ($current_ip == $saved_ip) and
> > ($current_dt < ($saved_dt + 3600)) ) {
> > return;
> > } else {
> > $query  = "UPDATE `table` SET `hits` = '$count', `agent` =
> > '$agent', `ts` = '$date_time'  WHERE `page` = '$page'";
> > $result = mysql_query($query) or die ('Error! -- ' .
> > mysql_error());
> > }
> >
> > However, I've read where this is not really acceptable.  Can
> > someone help me eliminate the 'else' portion of this if statement?
> >
> > Thank you,
> > Angela
> >
> > P.S.  I realize the above isn't complete code but it should still be
> > clear.  If not, let me know.
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >


What about this:

if ( !( ($current_page == $saved_page) and ($current_ip == $saved_ip)
and ($current_dt < ($saved_dt + 3600)) ) )

The 3 sub-expressions are grouped with an extra set of brackets and
the ! inverts the whole thing, so no need for an else clause.

Thanks,
Ash
http://www.ashleysheridan.co.uk




Re: [PHP] UNLESS Statement Equivalent

2013-03-11 Thread Jonathan Sundquist
Since you already have the return statement with the if statement the else
isn't required. If those three statements are true you would exit the call
any ways
On Mar 11, 2013 4:33 PM, "Angela Barone" 
wrote:

> I'm looking for an 'unless' statement, but as far as I can tell,
> PHP doesn't have one.  Hopefully someone can help me rewrite my statement.
>
> In English, I want to say: "always do something UNLESS these 3
> conditions are met".
>
> The best I've been able to come up with in PHP is this:
>
> if ( ($current_page == $saved_page) and ($current_ip == $saved_ip) and
> ($current_dt < ($saved_dt + 3600)) ) {
> return;
> } else {
> $query  = "UPDATE `table` SET `hits` = '$count', `agent` =
> '$agent', `ts` = '$date_time'  WHERE `page` = '$page'";
> $result = mysql_query($query) or die ('Error! -- ' .
> mysql_error());
> }
>
> However, I've read where this is not really acceptable.  Can
> someone help me eliminate the 'else' portion of this if statement?
>
> Thank you,
> Angela
>
> P.S.  I realize the above isn't complete code but it should still be
> clear.  If not, let me know.
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


[PHP] UNLESS Statement Equivalent

2013-03-11 Thread Angela Barone
I'm looking for an 'unless' statement, but as far as I can tell, PHP 
doesn't have one.  Hopefully someone can help me rewrite my statement.

In English, I want to say: "always do something UNLESS these 3 
conditions are met".

The best I've been able to come up with in PHP is this:

if ( ($current_page == $saved_page) and ($current_ip == $saved_ip) and 
($current_dt < ($saved_dt + 3600)) ) {
return;
} else {
$query  = "UPDATE `table` SET `hits` = '$count', `agent` = '$agent', 
`ts` = '$date_time'  WHERE `page` = '$page'";
$result = mysql_query($query) or die ('Error! -- ' . mysql_error());
}

However, I've read where this is not really acceptable.  Can someone 
help me eliminate the 'else' portion of this if statement?

Thank you,
Angela

P.S.  I realize the above isn't complete code but it should still be clear.  If 
not, let me know.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php