formatting the display of a variable

The typical constructs used for sending stuff to the browser are print and echo. The standard way to output the value of a variable is to include it in a doubly quoted string (which will interpolate its value) and then and place it after print or echo in a statement.

If you want your output to have a particular format, use printf() and sprintf(). The only difference between printf() and sprintf() is that printf() sends the resulting string directly to output, whereas sprintf() returns the result string as its value. I will demonstrate by example and allow you to learn the details from the PHP manual.

$value = 2.688522;
printf("%f,%10f,%-010f,%2.2f\n", $value, $value, $value, $value);
Advertisements

About samehramzylabib

See About on https://samehramzylabib.wordpress.com
This entry was posted in Code for View, Coding and tagged , , . Bookmark the permalink.

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s