TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-11-2008, 09:43 PM   #1 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default Using ISSET()...How does this line of code work?

I got the code below from PHP: Hypertext Preprocessor, under the ISSET() topic. It was a "contribution" to the subject from a reader, and it did not have any accompanying comments.

It looks interesting, but can anyone explain to a person with little experience (such as I) how it works?


PHP Code:
$var = (isset($var) && $var) ? $var 'new value'
Thanks,
Dave
Dave is offline  
Reply With Quote
Old 06-11-2008, 09:47 PM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

There's actually a really good explanation of this where it was discussed recently in another thread, here:
Help with function stristr()

It's called the ternary operator, and aside from the explanation you'll read above, it's basically checking to see if $var is set, and if it has a value. If it does, it's reassigning the value back to itself ($var to $var), and if not, it's assigning a new value (the string 'new value') to $var.
-m
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
Dave (06-11-2008)
Old 06-11-2008, 10:06 PM   #3 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

...so basically, leaving the tech terms out, it works like an if-else statement (not only works, but it is exactly that). The following statement:

PHP Code:
if($a == 1)
{
    
$b++;
}
else
{
    
$c++;

can be translated using the ternary operator into:

PHP Code:
$a == $b++ : $c++; 
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
Dave (06-11-2008)
Old 06-12-2008, 12:52 AM   #4 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default Thanks for info on the ternary operator

Thanks, delayedInsanity & Xenon, for the pointers.

I studied the recommended thread, plus I read some other sources, so I think I have quite a good introduction to this structure. It may be awhile, however, before I start using it on a regular basis...

Dave
Dave is offline  
Reply With Quote
Old 06-12-2008, 01:38 AM   #5 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

It may not be, you'd be surprised... I find it extremely handy to have on hand, but then again I'm all about shorthand. A lot of my code will do things like;

PHP Code:
if ($bool === true) return true;
    return 
false
...instead of...

PHP Code:
if ($bool === true) {
    return 
true;
} else {
    return 
false;

...which would also be a situation where the ternary could be used...

PHP Code:
return ($bool === true) ? true false
-m
delayedinsanity is offline  
Reply With Quote
Old 06-12-2008, 02:08 AM   #6 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Addendum: I realize the above example could be shortened to

PHP Code:
return $bool
...but it was meant just as an example.
-m
delayedinsanity is offline  
Reply With Quote
Old 06-12-2008, 04:51 AM   #7 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default 2015 will be my PHP year to shine...

That's probably because you "read" PHP in almost the same way that you would read a page of text. That is, you really don't have to think about what the letters and words mean individually, they all meld together "automatically" into meaning.

Probably, you can "think" in PHP, much as you might think in a foreign language after becoming very proficient in day-to-day usage.

I'm a l-o-o-o-ng way from there. But one of these days, I'd say 2015 or so, I'll start seeing the pieces begin to fit together...

Dave
Dave is offline  
Reply With Quote
Old 06-13-2008, 02:48 PM   #8 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Dave View Post
That's probably because you "read" PHP in almost the same way that you would read a page of text. That is, you really don't have to think about what the letters and words mean individually, they all meld together "automatically" into meaning.

Probably, you can "think" in PHP, much as you might think in a foreign language after becoming very proficient in day-to-day usage.

I'm a l-o-o-o-ng way from there. But one of these days, I'd say 2015 or so, I'll start seeing the pieces begin to fit together...

Dave
Heh, not 2015.. Unless you're THAT busy on your hands, or just hardly want to learn it. I studied it every day, and I understood at least half of the language, then 2 years came by and I grown to be a lot better at the language, then it's 3 years from now since I learned php, and I am all into the OOP standards and stuff. It was also helpful that I had knowledge of C/C++
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 06-13-2008, 04:24 PM   #9 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default OOP burns my eyes...

I'm so far away from OOP that I can't even see it with binoculars
Dave is offline  
Reply With Quote
Old 06-13-2008, 05:27 PM   #10 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Quit thinking that way. I just started doing all this back in ... february? march... or something... whenever I joined here. I jumped right into OOP because I wanted to understand it, I'm not kidding you, because I liked the look of $this-> variables. It's only taken four months and I think I've gotten a fairly good grasp on the basics of things - if you want to learn something, just go for it, you'd be surprised what you're capable of!

*gets off the soapbox*
-m
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
Dave (06-15-2008)
Old 06-15-2008, 12:15 AM   #11 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

Thanks for the encouragement! Yeah, you're right. As they say, the race belongs not to the swift, but to those who keep on running. (Makes for a neat saying, anyway! )

Dave
Dave is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

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


All times are GMT. The time now is 07:06 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design