glasspill
glasspill

Reputation: 1300

php printf adds number to output

I wanted to format some output using printf, but it outputs a number after each item for some reason. Any ideas as to why this is and how it could be fixed?

$array = array("Mo" => "09:30-19:00",  
          "Di" => "09:30-19:00", 
          "So" => "geschlossen");

foreach( $array as $key => $value ){
     echo printf("%3s:%15s", $key, $value);
}

output

Mo: 09:30-19:0019 Di: 09:30-19:0019 So: geschlossen19

Thank you

Upvotes: 3

Views: 584

Answers (2)

MetalFrog
MetalFrog

Reputation: 10523

Isn't it just because you're echoing a printf?

http://codepad.org/1KtxR9JF

<?php

$array = array("Mo" => "09:30-19:00",  
          "Di" => "09:30-19:00", 
          "So" => "geschlossen");

foreach( $array as $key => $value ){
     printf("%3s:%15s", $key, $value);
}

 Mo:    09:30-19:00 Di:    09:30-19:00 So:    geschlossen

Upvotes: 5

TheBestBigAl
TheBestBigAl

Reputation: 1230

Is there any particular reason you are using echo printf(...) rather than just echo?

If not, then using this returns correctly (without the 19 being appended):

$array = array("Mo" => "09:30-19:00",  
          "Di" => "09:30-19:00", 
          "So" => "geschlossen");

foreach( $array as $key => $value ){
     echo "$key:$value";
}

Upvotes: 0

Related Questions