On Thu, 2003-09-25 at 12:47, 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 ":" 

Ternary operator, if the first expression evaluates to true then it
returns the result of the second expression, otherwise it returns the
result of the third expression. The ! is part of the first expression.

> 
> 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? 

This is a short form for testing if the strings are equal. Rather than
test if strcmp( x, y ) == 0 which requires a comparison, a single
boolean operator is applied so that if 0 is returns the ! makes it true.
This is generally a carry forward from C coding style, where I believe
the ! style runs through the processor faster than the == style.

HTH,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

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

Reply via email to