Search code examples
phphtmloutputquotes

PHP - printf() prints part of my code


When running this (simple) code:

<?php

$array = array("Blue", "Green", "Yellow", "Pink", "");

foreach ($array as $arrayElement) {
    printf("<div class = \"colorSubArea %s \" > <p> 1 </p> </div> " , $arrayElement);
}

https://jsfiddle.net/xbkky7jx/3/

the last part of my code is geting printed out. Also the loop doesn't continue after the first iteration (possibly the same cause).I am new in PHP and this is very confusing to me.


Solution

  • Your php code is ok, the reason you don't see the output is HTML, see the output in viewsource mode.

    Your output is:

    <div class = "colorSubArea Blue " > <p> 1 </p> </div> 
    <div class = "colorSubArea Green " > <p> 1 </p> </div> 
    <div class = "colorSubArea Yellow " > <p> 1 </p> </div> 
    <div class = "colorSubArea Pink " > <p> 1 </p> </div> 
    <div class = "colorSubArea  " > <p> 1 </p> </div>   
    

    see this: https://eval.in/568890

              $array = array("Blue", "Green", "Yellow", "Pink", "");
    
              foreach($array as $arrayElement){  
                printf("%s " , $arrayElement);
              }