 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
06-11-2008, 09:43 PM
|
#1 (permalink)
|
|
The Acquainted
Join Date: Apr 2008
Posts: 110
Thanks: 97
|
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
|
|
|
|
06-11-2008, 09:47 PM
|
#2 (permalink)
|
|
is cute and cuddly
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
|
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
|
|
|
|
|
The Following User Says Thank You to delayedinsanity For This Useful Post:
|
|
06-11-2008, 10:06 PM
|
#3 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
...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 == 1 ? $b++ : $c++;
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
|
|
|
|
|
The Following User Says Thank You to xenon For This Useful Post:
|
|
06-12-2008, 12:52 AM
|
#4 (permalink)
|
|
The Acquainted
Join Date: Apr 2008
Posts: 110
Thanks: 97
|
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
|
|
|
|
06-12-2008, 01:38 AM
|
#5 (permalink)
|
|
is cute and cuddly
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
|
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
|
|
|
|
06-12-2008, 02:08 AM
|
#6 (permalink)
|
|
is cute and cuddly
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
|
Addendum: I realize the above example could be shortened to
...but it was meant just as an example. 
-m
|
|
|
|
06-12-2008, 04:51 AM
|
#7 (permalink)
|
|
The Acquainted
Join Date: Apr 2008
Posts: 110
Thanks: 97
|
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
|
|
|
|
06-13-2008, 02:48 PM
|
#8 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Dave
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
|
|
|
|
06-13-2008, 04:24 PM
|
#9 (permalink)
|
|
The Acquainted
Join Date: Apr 2008
Posts: 110
Thanks: 97
|
OOP burns my eyes...
I'm so far away from OOP that I can't even see it with binoculars 
|
|
|
|
06-13-2008, 05:27 PM
|
#10 (permalink)
|
|
is cute and cuddly
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
|
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
|
|
|
|
|
The Following User Says Thank You to delayedinsanity For This Useful Post:
|
|
06-15-2008, 12:15 AM
|
#11 (permalink)
|
|
The Acquainted
Join Date: Apr 2008
Posts: 110
Thanks: 97
|
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
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|