TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Using ISSET()...How does this line of code work? (http://www.talkphp.com/absolute-beginners/2948-using-isset-how-does-line-code-work.html)

Dave 06-11-2008 09:43 PM

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

delayedinsanity 06-11-2008 09:47 PM

There's actually a really good explanation of this where it was discussed recently in another thread, here:
http://www.talkphp.com/general/2943-...html#post15515

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

xenon 06-11-2008 10:06 PM

...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++; 


Dave 06-12-2008 12:52 AM

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

delayedinsanity 06-12-2008 01:38 AM

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 06-12-2008 02:08 AM

Addendum: I realize the above example could be shortened to

PHP Code:

return $bool

...but it was meant just as an example. ;-)
-m

Dave 06-12-2008 04:51 AM

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...8-)

Dave

Orc 06-13-2008 02:48 PM

Quote:

Originally Posted by Dave (Post 15567)
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...8-)

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

Dave 06-13-2008 04:24 PM

OOP burns my eyes...
 
I'm so far away from OOP that I can't even see it with binoculars *!*

delayedinsanity 06-13-2008 05:27 PM

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

Dave 06-15-2008 12:15 AM

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


All times are GMT. The time now is 11:41 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0