SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question mark in php

    I know it is an easy question but for a new bie it is not, I want to know the job of question mark here.
    $name=isset($_POST["name"]) ? $_POST['name'] : "";
    $area=isset($_POST["area"]) ? $_POST['area'] : "";

  2. #2
    SitePoint Zealot Cute Tink's Avatar
    Join Date
    Apr 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The function of the question mark is that of a ternary operator:
    PHP: Comparison Operators - Manual

    It is the same as writing the following:

    if ( isset( $_POST['name'] ) ) {
    $name = $_POST['name'];
    }
    else {
    $name = '';
    }

  3. #3
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    23,598
    Mentioned
    411 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by abeli View Post
    I know it is an easy question but for a new bie it is not, I want to know the job of question mark here.
    $name=isset($_POST["name"]) ? $_POST['name'] : "";
    $area=isset($_POST["area"]) ? $_POST['area'] : "";
    I'm a newbie too, but this is my understanding. The ? goes in tandem with the :

    The line is saying: let's assign a value to $name. The question mark is saying 'is "name" set? If so, assign $_POST['name'] to this variable'. The : is saying 'if not, then assign "" to it'.

    EDIT: got distracted by a phone call before clicking Post. Cute Tink's answer is better.


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
  •