Short hand question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Short hand question

Post by EricS »

I found the following code inside some classes I've been looking at and I'm not sure exactly what it means.

Code: Select all

($type == true)
    ? mysql_pconnect($host, $user, $pass)
    : mysql_connect($host, $user, $pass)
My guess is this is some kind of shorthand for an if statement. If type is true connect using persistent connections else connect without persistent connections. Is that how it works?

Thanks for everything.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It's called the ternary operator and is basically (as you worked out) a short-hand for simple if statements, there's a bit of info about it in here:
http://www.php.net/manual/en/language.o ... arison.php

Mac
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Thanks

Post by EricS »

Thanks twigletmac.... wonderful help as always. :D
Post Reply