tags:

views:

203

answers:

5

I'm trying to check whether a $_POST exists and if it does, print it inside another string, if not, don't print at all.

something like this:

$fromPerson = '+from%3A'.$_POST['fromPerson'];

function fromPerson() {
    if !($_POST['fromPerson']) {
        print ''
    } else {
        print $fromPerson
    };
}

$newString = fromPerson();

Any help would be great!

+7  A: 

isset($_POST['fromPerson'])

h3xStream
Eh WTF ?! 7 ups for this.. (please stop)
h3xStream
+1  A: 

Try isset($_POST['fromPerson'])?

strager
A: 
if (is_array($_POST) && in_array('fromPerson', $_POST)) {
    echo 'blah' . $_POST['fromPerson'];
}
jezmck
A: 
if( isset($_POST['fromPerson']) )
{
     $fromPerson = '+from%3A'.$_POST['fromPerson'];
     echo $fromPerson;
}
ehmad11
thanks! this worked.
eliwedel
my pleasure :D mark it as answer thn ;)
ehmad11
this was the final code:function fromPerson() { if( ($_POST['fromPerson']!='') ) { return '+from%3A'.$_POST['fromPerson']; }};$fromPerson1 = fromPerson();
eliwedel
+1  A: 

Everyone is saying to use isset() - which will probably work for you.

However, it's important that you understand the difference between

$_POST['x'] = NULL; and $_POST['x'] = '';

isset($_POST['x']) will return false on the first example, but will return true on the second one even though if you tried to print either one, both would return a blank value.

If your $_POST is coming from a user-inputted field/form and is left blank, I BELIEVE (I am not 100% certain on this though) that the value will be "" but NOT NULL.

Even if that assumption is incorrect (someone please correct me if I'm wrong!) the above is still good to know for future use.

Rafael
this also helped! thanks
eliwedel