Jump to content

[SOLVED] correct syntax for if statement inside variable definition


jswinkelman

Recommended Posts

What is the correct syntax for the "if statement" below? I want to concatonate it with the information on either side. Without the information, the page shows Member_First Member_Last, as it should. With the "if statement," it shows a blank page. I know the syntax is incorrect. What should it be?

 

<?php $thisPage=($row_rs_Member['Member_First'].' '.<?php if ($row_rs_Member['Member_Middle'] <> NULL) echo $row_rs_Member['Member_Middle'].' '; ?>.$row_rs_Member['Member_Last']); ?>

 

Thanks!

You can't run php inside php (unless you are using eval()) and you can't use the regular if form inside a variable (although you can use the ternary operator)...

Do something like this:

 

<?php

$thisPage = $row_rs_Member['Member_First'].' ';
if ($row_rs_Member['Member_Middle'] <> NULL)
$thisPage .= $row_rs_Member['Member_Middle'].' ';
$thisPage .= $row_rs_Member['Member_Last'];

?>

 

 

Orio.

<?php

$thisPage = $row_rs_Member['Member_First'].' ';
if (!is_null($row_rs_Member['Member_Middle']))
$thisPage .= $row_rs_Member['Member_Middle'].' ';
$thisPage .= $row_rs_Member['Member_Last'];

?>

 

I believe if you are checking for null, the is_null() function php provides does wonders?

http://us3.php.net/manual/en/function.is-null.php

 

Also remember if you want to do more than 1 line of processing it should look like this:

 

<?php

$thisPage = $row_rs_Member['Member_First'].' ';
if (!is_null($row_rs_Member['Member_Middle'])) {
        // more than one line of processing here
$thisPage .= $row_rs_Member['Member_Middle'].' ';
}
$thisPage .= $row_rs_Member['Member_Last'];

?>

 

with the curly braces.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.