View Full Version : What does this php shortcut mean?

08-04-2008, 02:53 AM
I remember seeing a post by Nile a while back that explained about a shortcut I have seen used in code in various places. I have looked and looked for Nile's post, but don't know what it is called so I can't even google it. Could someone please explain again what the shortcut is short for? Here are 2 examples:

( $rptvar ? 'y' : 'n' )

$freq = ( $_POST['modate_freq'] ? $_POST['modate_freq'] : 1 );

Thanks very much.

08-04-2008, 03:05 AM
That "shortcut" is called the ternary operator. You can read about it here:

http://php.net/language.operators.comparison/ (scroll down a bit)

08-04-2008, 06:08 AM
Thanks! Just what I needed. In there were some other things I have always wondered about. For example:

What does this mean: <-

and this: => eg. 0=>array I get the feeling it is different than >=

And what is the best way to check if something is selected? I have seen


if (!empty

If($var != " ")

Do they all do the same thing?

Thanks. :)

08-04-2008, 06:17 AM
I have seen


if (!empty

If($var != " ")

Do they all do the same thing?

No, not all of them. I think:

if (!empty

If($var != " ")

does the same thing.

But, isset, checks if something is. Like:

$name = (isset($_GET['name'])) ? $_GET['name'] : $default;

This will check if there is "name". This won't check if it is empty or not. But:

$name = (!empty($_GET['name'])) ? $_GET['name'] : $default;

this will check if it is not empty, set the $name = $_GET['name'], if not make default.

I hope you understood me, and i could help you :)

08-04-2008, 06:34 AM
So isset checks if something exists as a variable, whether or not it has a value?

I think I understood, except not sure if $default is an arbitrary variable or a special usage of the word default. Does default mean a special value like NULL or something that a variable might have by default if it is not set?

Thanks :)

08-04-2008, 06:37 AM
$default, is arbitrary variable. Just used it for example.

Isset checks if something exist.

08-04-2008, 06:50 AM
OK, great. Thanks for explaining.

So what would be the best method to use if you are checking to see if a user checked a certain checkbox in a form? Say they have a choice between 4 radio buttons. What is the best way to determine what they checked, and then act accordingly? What if they checked something and then changed their mind and checked something else? Would that affect if something isset or not? Is it better to ask if the "name" of the button == the value of that button? Or do you say:

if($name == "checked" or something
if($name == 1) {do something

08-04-2008, 06:52 AM
Isset is not for that works. I think:

if($name == "checked"

Is better for me. I use it.

08-04-2008, 07:09 AM
So this is what you use?...

<input name="rectype" type="radio" value="1">
<input name="rectype" type="radio" value="2">
if($rectype == 2) {

What if there is no exact value, like a textbox that they fill in? How do you check if somethings in it? != " " ? Thanks.

08-04-2008, 07:34 AM
like this:

if($_POST['rectype'] == '1'){
elseif($_POST['rectype'] == '2'){

I think