Bluehost.com Web Hosting $6.95

Help understanding code...

This is a discussion on Help understanding code... within the PHP General forums, part of the PHP Programming Forums category; I've just picked up a more advanced book on PHP and it has a lot of example code in ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-25-2003
Jeff McKeon
 
Posts: n/a
Default Help understanding code...

I've just picked up a more advanced book on PHP and it has a lot of
example code in it. I understand most of it but some things I'm seeing I
don't understand. Like the following...

code:
------------------------------------------------------------------------
--------

$couponcode = (! empty($_REQUEST['couponcode'])) ?
$_REQUEST['couponcode'] : NULL;
------------------------------------------------------------------------
--------

I think this is saying:

If the global variable couponcode is not empty, then the variable
'$couponcode' is equal to "$_REQUEST['couponcode']" otherwise it gets a
"NULL" value.

What's throwing me is the use of the "!" and "?" and ":"

If What I suspect is correct, I've never seen an if-then statement like
this. If it is a replacement for an IF-Then statement then it's much
cleaner and I'd like to use it.

another one is:


code:
------------------------------------------------------------------------
--------
IF (!strcmp($operator, '+')) {
$result = $num1 + $num2
}
------------------------------------------------------------------------
--------

I've looked up strcmp() and know it's used to compair two strings. The
$operator variable in the script that this was taken from is set to
either "-", "+", "*" or "/". What I don't understand here is what the
"!" in front of strcmp() means.

Can anyone break down the code for me and explain the parts?

thanks,

Jeff
Reply With Quote
  #2 (permalink)  
Old 09-25-2003
Brent Baisley
 
Posts: n/a
Default Re: [PHP] Help understanding code...

Your assumptions are correct. It's called a ternary operator and it is
a substitution or the if-else statements. I try not to overuse it since
I don't think it's as readable as the block if statement. But for
things not assigning a value to a variable, I think it's better than
the block statement since you're more interested in the variable
declaration than the condition of it's value.


On Thursday, September 25, 2003, at 12:47 PM, Jeff McKeon wrote:

> I've just picked up a more advanced book on PHP and it has a lot of
> example code in it. I understand most of it but some things I'm seeing
> I
> don't understand. Like the following...
>
> code:
> -----------------------------------------------------------------------
> -
> --------
>
> $couponcode = (! empty($_REQUEST['couponcode'])) ?
> $_REQUEST['couponcode'] : NULL;
> -----------------------------------------------------------------------
> -
> --------
>
> I think this is saying:
>
> If the global variable couponcode is not empty, then the variable
> '$couponcode' is equal to "$_REQUEST['couponcode']" otherwise it gets a
> "NULL" value.
>
> What's throwing me is the use of the "!" and "?" and ":"
>
> If What I suspect is correct, I've never seen an if-then statement like
> this. If it is a replacement for an IF-Then statement then it's much
> cleaner and I'd like to use it.
>
> another one is:
>
>
> code:
> -----------------------------------------------------------------------
> -
> --------
> IF (!strcmp($operator, '+')) {
> $result = $num1 + $num2
> }
> -----------------------------------------------------------------------
> -
> --------
>
> I've looked up strcmp() and know it's used to compair two strings. The
> $operator variable in the script that this was taken from is set to
> either "-", "+", "*" or "/". What I don't understand here is what the
> "!" in front of strcmp() means.
>
> Can anyone break down the code for me and explain the parts?
>
> thanks,
>
> Jeff
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>

--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search & Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577
Reply With Quote
  #3 (permalink)  
Old 09-26-2003
Baroiller Pierre-Emmanuel
 
Posts: n/a
Default Re: Help understanding code...

Hi,
It's a "C Like Syntax"

(condition)?true:false;

is like :

if (condition) true
else false

regards.
P.E. Baroiller

"Jeff McKeon" <jmckeon@telaurus.com> a écrit dans le message de
news:B3F5C378E4B6034CA27B15C99E9133CE63EAD4@exch01 tc07927.telaurus.net...
I've just picked up a more advanced book on PHP and it has a lot of
example code in it. I understand most of it but some things I'm seeing I
don't understand. Like the following...

code:
------------------------------------------------------------------------
--------

$couponcode = (! empty($_REQUEST['couponcode'])) ?
$_REQUEST['couponcode'] : NULL;
------------------------------------------------------------------------
--------

I think this is saying:

If the global variable couponcode is not empty, then the variable
'$couponcode' is equal to "$_REQUEST['couponcode']" otherwise it gets a
"NULL" value.

What's throwing me is the use of the "!" and "?" and ":"

If What I suspect is correct, I've never seen an if-then statement like
this. If it is a replacement for an IF-Then statement then it's much
cleaner and I'd like to use it.

another one is:


code:
------------------------------------------------------------------------
--------
IF (!strcmp($operator, '+')) {
$result = $num1 + $num2
}
------------------------------------------------------------------------
--------

I've looked up strcmp() and know it's used to compair two strings. The
$operator variable in the script that this was taken from is set to
either "-", "+", "*" or "/". What I don't understand here is what the
"!" in front of strcmp() means.

Can anyone break down the code for me and explain the parts?

thanks,

Jeff
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 02:36 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0