Jump to content

[SOLVED] correct syntax for if statement inside variable definition


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.