jswinkelman Posted March 16, 2007 Share Posted March 16, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/42980-solved-correct-syntax-for-if-statement-inside-variable-definition/ Share on other sites More sharing options...
Orio Posted March 16, 2007 Share Posted March 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/42980-solved-correct-syntax-for-if-statement-inside-variable-definition/#findComment-208743 Share on other sites More sharing options...
per1os Posted March 16, 2007 Share Posted March 16, 2007 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/42980-solved-correct-syntax-for-if-statement-inside-variable-definition/#findComment-208809 Share on other sites More sharing options...
jswinkelman Posted March 17, 2007 Author Share Posted March 17, 2007 I knew you'd know! Worked like a charm. Thanks for the tips! Quote Link to comment https://forums.phpfreaks.com/topic/42980-solved-correct-syntax-for-if-statement-inside-variable-definition/#findComment-209632 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.