Expand on isset

Hi,

I am a total noob to PHP. I’m trying to build a listing form for accommodation establishments.

Let me give an example.

On the input side, I have a form, and here’s one of the fields in that form:

Now, the mobile number is not a required field.

But I don’t want the HTML to output blank tags. I’d like the tags to be part of the output, so that if the mobile number is not submitted, it doesn’t display the HTML either.

Here’s what I have on the output side:
if(isset($_POST[‘mobileNumber’])) {
$mobileNumber = $_POST[‘mobileNumber’];
}

I kind of understand what’s going on there. It’s saying that, if a number is received, show a number, but only if a number is received.

I could output the variable such as follows:

Phone number: <?php print $mobileNumber; ?>. (I am trying to use schema.org in my listing.)

But I would like that whole HTML string to be part of the ‘isset’ function.

I hope I’m making myself clear enough.

Could you perhaps help me with this?

Thanks,

I got some sort of thang going, so it’s sorted.

I create a variable: $mobileNumber = $_POST[‘mobileNumber’];

Then I use “not empty”, like so:
if(!empty($mobileNumber)) {
print ‘

Phone number: ‘.$mobileNumber.’.
’;
}
else {
echo “”;
}

That works!

A couple comments…

You use “print” AND “echo”. Stick to one or the other. Echo is one less character so less keystrokes over time.

If you open your echo string with double quotes you wont have to escape your variables, but you will have to escape any other double quotes. One thing to keep in mind, the newline character (\n) will not work in single quoted strings. Why you would want to use \n is to have line breaks when you view the page source in the browser which is something you will do often in development. It makes the output code much more readable.

Example:
[php]echo “

Phone number: <span itemprop=“telephone”>$mobileNumber
\n”;[/php]

Hi Kevin,

Thanks so much for the reply.

Really appreciate the input.

Sponsor our Newsletter | Privacy Policy | Terms of Service