How To Deal With This HTML Code Echoed With PHP Which As Both Single And Double Quotes?
I'm not sure if this is a cleaner way of writing this, but I think I don't have problems here:
Solution 1:
If you really want to output PHP code:
<?php switch ( $meta_box['type'] ) {
case 'textarea':
echo '<textarea name="<?=$meta_box[\'name\']?'.'>">
<?=htmlspecialchars( $data[ $meta_box[ \'name\' ] ] ) ?'.'>
</textarea>';
break;
default:
echo '<input type="text" name="<?=$meta_box[ \'name\' ] ?'.'>"
value="<?=htmlspecialchars( $data[ $meta_box[ \'name\' ] ] ) ?'.'>">';
?>
Otherwise this makes more sense to me:
<?php switch ( $meta_box['type'] ) {
case 'textarea':
echo '<textarea name="'.$meta_box['name'].'>">'.
htmlspecialchars( $data[ $meta_box['name'] ] ).
'</textarea>';
break;
default:
echo '<input type="text" name="'.$meta_box[ 'name' ].'" '.
'value="'.htmlspecialchars( $data[ $meta_box[ 'name' ] ] ).'">';
}
?>
But I guess that $data[$meta_box['name']]
array index isn't correct either.
Solution 2:
echo 'String with "double quotes" inside';
echo "String with \"double quotes\" inside";
echo 'String with \'single quotes\' inside';
echo "String with 'single quotes' inside";
echo 'String with \'single quotes\' and "double quotes" inside';
echo "String with 'single quotes' and \"double quotes\" inside";
Solution 3:
Why u dont follow KISS rule, try below if u have no more conditions
<?php if( $meta_box['type'] === 'textarea' ) { ?>
<textarea name="<?php echo $meta_box[ 'name' ];?>"><?php echo htmlspecialchars($data[$meta_box['name']]); ?></textarea>
<?php } else { ?>
<input type="text" name="<?php echo $meta_box['name']; ?>"
value="<?php echo htmlspecialchars( $data[$meta_box['name']]); ?>" /> <?php }?>
Happy to help :)
Solution 4:
I tend to use double quotes for the echo and single quotes within that
echo "Hello string test ' ";
or you can use the escape \
echo "hello string test \" ";
Also in your code you are doing an echo within an echo.
Solution 5:
You can escape the quotiation makrs like this:
echo "<input type=\"text\" name=\"test\" />;
But why dont you make it this way:
echo '<textarea name="' . $meta_box[ 'name' ] . '">
Post a Comment for "How To Deal With This HTML Code Echoed With PHP Which As Both Single And Double Quotes?"