Skip to main content

Different Types of Output Methods in PHP


The strength of the web has been the ability for practically anyone to provide information (accurate or not) to anyone who is interested in viewing the web pages that contain that information.  With HTML, the static nature of the information being displayed limits the amount of true customization that can be provided; however, PHP provides truly dynamic content by creating custom content each time the web page is displayed.  To accomplish this display of information, PHP has four functions that make this happen: 


   •  print() -
                    This statement is designed to provide user feedback; in other words, this function "prints" content to the HTML code which in turn will be displayed on the web page when called.  The print() function can display both raw strings and variables. 

<?php 
    print('<p>This is the standard use of the print function.</p>'); 
    print '<p>However, the parentheses are not necessary.</p>'; 
    $var = "print"; 
    print "<p>Variables can also be included with the $var  
     function.</p>"; 
    print '<p>Another way to add a variable to a ' . $var . 
     ' function is to concatenate it to the strings</p>'; 
?> 




As shown above, there are multiple ways to use the print function.  In addition, breaking the function components across multiple lines is acceptable.  The function will continue to read the components until it reaches the semi-colon delimiter (;) which tells the function to complete the process. 


•  echo - 
              The echo function is similar to the print function in that it can "print" content to the HTML code; however, it can not be used in more complex expressions because it returns a void versus the print function which will return a boolean value. Except for this difference, the echo function is no different than the print function.  With that thought in mind, many programmers choose to just use the print function to prevent this limitation as they advance in their programming skills. 


<?php 
    echo '<p>This is the standard use of the echo function.</p>'; 
    $var = 'echo'; 
    echo "<p>The $functionType function can also use embedded  
     variables.</p>"; 
    echo '<p>However, when concatenating with the ', $functionType, 
     ' function, it is best to use commas to concatenate the  
      strings</p>'; 
?> 



•  printf() -
                The  printf function performs the same function as the  print command with the exception that there is greater control over the formatting of the "printed" text.  The printf function works from the standard print approach for "printing" text to HTML: 

<?php 
    printf('<p>This is the standard use of the printf function</p>'); 
?> 

Of course, the above demonstration does not add any value to the standard print function.  But the printf function does not stop there, the following demonstrations show how the format argument can be included to provide a level of formatting to the strings referenced in the printf function: 


<?php 
    printf('<p>%d Euro = $%01.2f US Dollar</p>', 1, 1.3); 
    printf('<p>My %s paid $%01.2f for my $s</p>', 'Mother', 23.4,  
     "Dog"); 
    printf('<p>However, my %3\$s paid $%2\$01.2f to get away from my  
     %1\$s</p>', 'Mother', 100.3, 'Dog'); 
    printf('<p>%11s</p>', 'Long text can be limited to a certain  
     number of characters, in this case 11.'); 
?> 


A complete list of supported type specifiers is located in Appendix D.  In the case of the third printf function listed above, the location of the string argument can be chosen by placing the location (#$) with the format type.  In the case of  %3\$s, the third argument was selected and formatted as a string.  The reason for the escape character (\) preceding the dollar sign ($) is to ensure that it was considered part of the formatting versus an actual dollar sign as used in the first printf function. 

•  sprintf() - 
                  The sprintf function performs the same functions as the printf function except it stores the result of the function into a variable. 

<?php 
    $orderTotal = sprintf('$%01.2f', 123.456); 
    print "The order total is $orderTotal"; 
?> 





Comments

Popular posts from this blog

PHP INTRODUCTION

                     PHP  (recursive acronym for  PHP: Hypertext Preprocessor ) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. PHP stands for  P HP:  H ypertext  P reprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software PHP is free to download and use Why PHP? PHP runs on different platforms (Windows, Linux, Unix, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is FREE to download from the official PHP resource:  www.php.net PHP is easy to learn and runs efficiently on the server side What can PHP do? Anything. PHP is mainly focused on server-side scripting, so you can do anything any other CGI program can do, such as collect form data, generate dynam

MySQL General Architecture

        MySQL operates in a networked environment using a client/server architecture. In other words, a central  program acts as a server, and various client programs connect to the server to make requests. A MySQL  installation has the following major components: MySQL Server, Client programs and MySQL non client  utilities.  MySQL Server MySQL Server, or mysqld, is the database server program. The server manages access to the actual  database (schema) on disk and in memory. MySQL Server is multi-threaded and supports many  simultaneous client connections. Clients can connect via several connection protocols. For managing  database contents, the MySQL server features a modular architecture that supports multiple storage engines  that handle different types of tables (for example, it supports both transactional and non-transactional  tables). Keep in mind the difference between a server and a host. The server is software (the MySQL server  program mysqld). Server characteristi