Skip to content Skip to sidebar Skip to footer

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:

<?phpswitch ( $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:

<?phpswitch ( $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

<?phpif( $meta_box['type'] === 'textarea' ) { ?><textareaname="<?phpecho$meta_box[ 'name' ];?>"><?phpecho htmlspecialchars($data[$meta_box['name']]); ?></textarea><?php } else { ?><inputtype="text"name="<?phpecho$meta_box['name']; ?>"value="<?phpecho 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?"