1

I have a form review page that works great calling:

echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>";
echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Email:</span>{$_REQUEST['CustomerEmail']}</div>";
echo "<div class='reviewItem'><span class='reviewTitle'>Customer Phone:</span>{$_REQUEST['CustomerPhone']}</div>";
echo "<div class='reviewItem'><span class='reviewTitle'>Customer Address:</span>{$_REQUEST['CustomerAddress']}</div>";

The problem is that I have a ton of fields that don't get filled out. I want to put an "if" statement before the echo to check if it has data. if not, nothing gets displayed. I am very new to PHP, so I'm not sure if I can call something like:

if (length > 0)echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Email:</span>{$_REQUEST['CustomerEmail']}</div>";

Thanks in advance for any help. Cheers.

1
  • echo (condition) ? "<div ...></div>" : false; Commented May 3, 2013 at 18:27

6 Answers 6

1
echo (!empty($_REQUEST['CustomerName'])) ? "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>  {$_REQUEST['CustomerName']}</div>" : "";
echo (!empty($_REQUEST['CustomerEmail'])) ? "<div class='reviewItem'><span class='reviewTitle'>Cusomer Email:</span>{$_REQUEST['CustomerEmail']}</div>" : "";
echo (!empty($_REQUEST['CustomerPhone'])) ? "<div class='reviewItem'><span class='reviewTitle'>Customer Phone:</span>{$_REQUEST['CustomerPhone']}</div>" :"";
echo (!empty($_REQUEST['CustomerAddress'])) ? "<div class='reviewItem'><span class='reviewTitle'>Customer Address:</span>{$_REQUEST['CustomerAddress']}</div>" : "";

Try that.

Sign up to request clarification or add additional context in comments.

Comments

0

Try

if (isset($_REQUEST['CustomerName']))
    echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>";

or

if (isset($_REQUEST['CustomerName']) && $_REQUEST['CustomerName'])
    echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>";

Comments

0

You've just about got it. Try this:

if(isset($_REQUEST['CustomerName']}))
     echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>";

PHP's isset checks to see if the referenced variable has a value assigned - if it does, echo it.

Comments

0

I usually write it like this, which tests for the var being set and not blank:

<?php
if(isset($_REQUEST['CustomerName']) && $_REQUEST['CustomerName']!=''){
 echo "<div class='reviewItem'><span class='reviewTitle'>Customer Name:</span>{$_REQUEST['CustomerName']}</div>";
}
?>

1 Comment

You don't need to check two conditions if you want to prevent empty strings: if(!empty($_REQUEST['CustomerName')){...}
0

You mean this?

if($_REQUEST['CustomerName']):
    echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>";
endif;
if($_REQUEST['CustomerEmail']):
    echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Email:</span>{$_REQUEST['CustomerEmail']}</div>";
endif;
if($_REQUEST['CustomerPhone']):
    echo "<div class='reviewItem'><span class='reviewTitle'>Customer Phone:</span>{$_REQUEST['CustomerPhone']}</div>";
endif;
if($_REQUEST['CustomerAddress']): 
    echo "<div class='reviewItem'><span class='reviewTitle'>Customer Address:</span>{$_REQUEST['CustomerAddress']}</div>";
endif;

6 Comments

I don't understand why people still use if: endif;, especially in a case like this. It's just noise
its easier to read if you have other stuff like html etc.. you would want to know what that stray } mean, right? unless you want to trace it back above..
@reikyoushin: proper code formating and avoiding of mixing php+html make miracles
i know MVC is a good approach but there are situations when you need to add stuff together..
@Mr. Alien - You call } => stray? if the code is too long, especially on nested curly braces, you could confuse it with something else like a closing character on for(){, try{, etc.. if you get what i mean..
|
0

You can always use the short way:

echo (isset($_REQUEST['CustomerName'])) ? "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>" : false;

Logic:

echo (condition) ? TRUE : FALSE;

1 Comment

Will it work? Yes. But something about echo false rubs me the wrong way. And how is this "the short way"? It's more characters than using a simple if statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.