Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #21 (permalink)  
Old November 19th, 2003, 06:11 PM
Authorized User
 
Join Date: Nov 2003
Location: Trowbridge, Wilts, United Kingdom.
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Rich / Nik,

Thanks for all your help over the last few days, I never been so stumped in my life!

Pretty much everything is a ok now, got a few issues with sendmail (or lack there of, os x 10.3 use postfix instead) and still haven't got the forums to work, though I less bothered about that as I won't be using them.

Thanks for the info about references etc, I understand the principal but will need to take some time for fix them. Would I be write in thinking that the trinary operator is for writing shorthand expressions ??? I've come accross them in flash before, though still find them a little hard to read :-)

I'll be re-writing all the html once I've got everything working so I'm not going to worry too much about that! Got a lot of extra classes to write, and a lot of extrax functionality as well. I won't to have multiple product types and multiple product groups and products will need to appear in more than one group as well! Also, I would like to have related products (like say Amazon) and that's not to mention all the other sections of the site which are in turn, split into sub-sections! What I'm trying to say is this, this ain't the last time you're gonna hear from me!!!

Anyway, I promise to let you both see the site once it's finished and again, thanks.

Jon
Reply With Quote
  #22 (permalink)  
Old November 19th, 2003, 06:38 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Glad to have been of help!
Here is the manual page on the ternary operator.. so yeah its a shorthand method of writing expressions. Its just stuck in on the page that discusses expressions in general.

Quote:
quote:from the manual
There is one more expression that may seem odd if you haven't seen it in other languages, the ternary conditional operator:

<?php
$first ? $second : $third
?>

If the value of the first subexpression is TRUE (non-zero), then the second subexpression is evaluated, and that is the result of the conditional expression. Otherwise, the third subexpression is evaluated, and that is the value.
http://www.php.net/manual/en/language.expressions.php

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #23 (permalink)  
Old November 19th, 2003, 07:05 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Keep in mind that it's an OPERATOR, which means that it requires OPERANDS and results in a value. So when you think about it, it's not a shorthand method of writing expressions, it's just a kind of expression you can write.

Saying that ?: is shorthand is like saying ++ is shorthand for "+1" -- you can think of it that way if you want, but that's not TECHNICALLY accurate.

Because these are operators that form their own expressions, you can include them within other expressions.

For example: 4 + 3 is an expression, the operator is +, the operands are 4 and 3, the result is 7. You can use that expression within other expressions: 5 * (4 + 3). Same thing goes with the ternary operator. Also, the $first of Rich's quote is really a placeholder for a BOOLEAN expression, you're not limited to using just a variable -- you can call functions that return boolean values, or create expressions of your own.

Here goes with something a little more complex:

$submitted_form = isset($_POST['username'])? (isset($_POST['password'])? TRUE : FALSE) : FALSE;


See, if 'username' was submitted, then we evaluate the second ternary expression. That one evaluates to true (don't say "returns true" because it's NOT a function!!) if 'password' was submitted and false if it wasn't. Otherwise (that is, if 'username' was not submitted), the expression evaluates to false.


That translates to this long-winded code:

if (isset($_POST['username']))
{
    if (isset($_POST['password']))
    {
        $submitted_form = true;
    }
    else
    {
        $submitted_form = false;
    }
}
else
{
    $submitted_form = false;
}



One thing to notice about this: $submitted_form is ALWAYS assigned a value. If you've got if/else chains or other conditional code where a variable is ALWAYS assigned something, chances are you can clean things up by assignign the result of a ternary operator expression.

Of course, the above can be rewriten without loss of functionality as:

if (isset($_POST['username']) && isset($_POST['password))
{
   $submitted_form = true;
}
else
{
   $submitted_form = false;
}

...but that is a translation of THIS ternary expression:

$submitted_form = (isset($_POST['username']) && isset($_POST['password']))? true : false;


This example is admittedly lame, because the value of the expression when it's evaluated IS EXACTLY the value of the conditional part. It's like:

$foo = true ? true : false; or $foo = false ? true : false;

One can cut out the ternary operation completely:
$foo = true; or $foo = false;

Relating it to our example:

$submitted_form = isset($_POST['username']) && isset($_POST['password']);



Okay, rambling over.

Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #24 (permalink)  
Old November 20th, 2003, 06:34 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Thanks again for clarifying that Nik.. so it is itself an expression. got it. I did try your example:

$bg = (!strcmp($bg, "FFFFFF"))? "F6F6F6" : "FFFFFF";

In my own code when I realized a color alternating mechanism of my own wasn't working due to an inconsistency with the numbering. Being unable to rely on the number being odd I thought that way would be better.

So I thought what the hell and threw in that code.. which works, mind you, but still came up with a notice of undefined variable due to the comparision.

Which is no matter.. I just threw in another line.

if (!isset($bg)) $bg = "FFFFFF";
$bg = (!strcmp($bg, "FFFFFF"))? "F6F6F6" : "FFFFFF";

So there it is.. orginal error is fixed and the logic clarified both!

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
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 On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows based Problem Design Solution Book ricpue BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 5 September 1st, 2006 07:47 AM
php-mysql website - problem design solution pannet1 Pro PHP 0 December 23rd, 2005 01:49 PM
php/mysql programming problem - design - solution jben.net Beginning PHP 8 November 14th, 2003 09:00 PM
PHP MySQL Website Programming P-D-S robmorrish All Other Wrox Books 1 August 5th, 2003 01:29 AM
Building a PHP Intranet Problem-Design-Solution kseba All Other Wrox Books 0 June 23rd, 2003 04:26 PM



All times are GMT -4. The time now is 07:41 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.