Tuesday 15 April 2014

Are there any restrictions on when you can mix HTML and PHP? -



Are there any restrictions on when you can mix HTML and PHP? -

i surprised find can break out of php function raw html , back. knew sort of thing loops , conditionals, surprise me. accident or well-defined behavior? (i couldn't find explicit give-and-take of function case in manual.)

[note: next code doesn't give illustration of when utilize behavior, kept simple demonstration purposes.]

<?php $i = 0; while($i++ < 3) { ?><p>i in while loop.</p><?php } // part surprised me function actskeptical($adjective) { ?><p>is <?= $adjective ?> works?.</p><?php } actskeptical("weird"); ?>

output:

i in while loop. in while loop. in while loop. weird works?

i know people absolutely hate mixing php , html this, can't oop/templating (for reasons won't go here) , seeing much raw html possible.

also, don't quite understand semantics of how short open/close tag above (outputting $adjective) works in conjunction surrounding code. php treat raw html echo statement? , <?= $adjective ?> including variable within string?

i can't seem find documentation relating exiting of php tags within blocks. however, there's few places escaping html work.

normal usage

<?php php_related_code(); ?> //html/css/js/etc

within blocks, such while, for, functions, etc

<?php ($i = 0; $i < 5; $i++) { ?> hello world <?php } $i = 5; while ($i-- > 0) { ?> hello there <?php } function myfunc() { ?> hello universe <?php } myfunc();

you can think of ?>stuff<?php similar echo or print command found in php, because can escape html in same places can echo. can echo within main script, in loops, , can echo in functions. can't echo in array, example:

<?php $array = array(echo "here"); //not allowed $array = array(?>here<?php); //also not allowed

so can think of escaping same echoing in can tell where can utilize it, can't same thing when you're thinking what does.

they deed differently , processed php differently well. question asking restrictions won't go differences between ?><?php , echo.

i forgot mention, <?=$variable?> short tag <?php echo $variable; ?> if have feature enabled.

php html mixing

No comments:

Post a Comment