Break out of echo ..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Break out of echo ..

Post by bob_the _builder »

Hi,

Is there away to break out of an echo statment without having to close off ie:

Code: Select all

<td><input name="first" type="text" value="'.$first.'"> '.( if(!$first){  ' *'; } ).'</td>


Thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

What?
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

If you have something like:

echo '<td><input name="first" type="text" value="'.$first.'"></td>';

and you want to error check fields using:

if(!$first){ echo ' *'; }

is there a way to place the whole lot within:

echo ' ';

without having to use the likes of:

Code: Select all

echo '<td><input name="first" type="text" value="'.$first.'"></td>';
if(!$first){  echo ' *'; }

echo '<td><input name="last" type="text" value="'.$last.'"></td>';
if(!$last){  echo ' *'; }

Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Like:

Code: Select all

echo '<td><input name="first" type="text" value="'.($first ? $first : '*').'"></td>';
echo '<td><input name="last" type="text" value="'.($last ? $last : '*').'"></td>';
(#10850)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Ah, your looking for the ternary operator.

Edit | A bit late.. left window open and forgot about it ;)
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Perfect ..

Thanks
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Hi,

Im using:

Code: Select all

<td><input name="first" type="text" value="'.$first.'">'.($first ? '' : ' *').'</td>
To display * outside of the text field.


Thanks .. cleans my code up nicely
Post Reply