Results 1 to 10 of 10

Thread: What does this php shortcut mean?

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default What does this php shortcut mean?

    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:

    Code:
    ( $rptvar ? 'y' : 'n' )
    
    $freq = ( $_POST['modate_freq'] ? $_POST['modate_freq'] : 1 );
    Thanks very much.

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    That "shortcut" is called the ternary operator. You can read about it here:

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

  3. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    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(isset

    if (!empty

    If($var != " ")

    Do they all do the same thing?

    Thanks.

  4. #4
    Join Date
    Jul 2007
    Location
    Azerbaijan, Baku
    Posts
    144
    Thanks
    11
    Thanked 27 Times in 25 Posts

    Default

    Quote Originally Posted by kuau View Post
    I have seen

    if(isset

    if (!empty

    If($var != " ")

    Do they all do the same thing?
    No, not all of them. I think:

    Code:
    if (!empty 
    
    If($var != " ")
    does the same thing.

    But, isset, checks if something is. Like:

    Code:
    $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:

    Code:
    $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

  5. The Following User Says Thank You to allahverdi For This Useful Post:

    kuau (08-04-2008)

  6. #5
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    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

  7. #6
    Join Date
    Jul 2007
    Location
    Azerbaijan, Baku
    Posts
    144
    Thanks
    11
    Thanked 27 Times in 25 Posts

    Default

    $default, is arbitrary variable. Just used it for example.

    Isset checks if something exist.

  8. #7
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    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

  9. #8
    Join Date
    Jul 2007
    Location
    Azerbaijan, Baku
    Posts
    144
    Thanks
    11
    Thanked 27 Times in 25 Posts

    Default

    Isset is not for that works. I think:

    Code:
    if($name == "checked"
    Is better for me. I use it.

  10. #9
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    So this is what you use?...
    Code:
    <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.

  11. #10
    Join Date
    Jul 2007
    Location
    Azerbaijan, Baku
    Posts
    144
    Thanks
    11
    Thanked 27 Times in 25 Posts

    Default

    like this:

    Code:
    if($_POST['rectype'] == '1'){
    }
    elseif($_POST['rectype'] == '2'){
    }
    I think

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •