
Please support if you like my work by payment through upi: sinhamit@icici or payment by bank
account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB
Displaying DATA in Category: PHP by amit
🕙 Posted on 2023-04-28 at 21:27:12 Read in Hindi ...
Displaying DATA into WEB-PAGES
In previous page, you have seen some PHP code. You have also learnt in previous page, how to comment inside PHP. Indentation is not important in PHP, and you can write all your program, without any comment, and also in one single line!
<?php echo 123; echo '<h1>Hello World</h1>'; ?>
However, indentation and comments make your CODE more readable. In the above example, the CODE consists of only two statments inside the OPENING and CLOSING php tags. But, when your CODE become more complex, and large enough, you will script the above example as following:
<?php
/* echo is a built-in function, which is used to display result in the browser. You will learn more about echo and its fine difference with other similar functions, which are also used to display results, hereinbelow. */
echo 123; echo '<h1>Hello World</h1>';
?>
The HTML element, '<br />' or '<br>' within a pair of single quotes, is a STRING data-type in PHP. The PHP interpreter will insert a line break HTML element, <br /> in the WEB-PAGE, (for example, http://echo
function.
You should TEST each of following examples in this page or next pages, in a blank PHP file. To do this, you can either delete all codes previously written in the index.php file, or create new file with .php
extension, as explained in previous page.
echo always returns one or more string (data-type) values, for one or more expressions. (These expressions should be separated by , comma). The word 'expression' means anything which can be evaluated, that is, 123 (is a numeric data-type) and it has a literal value of 123. The word 'statement' means a PHP line of code, which must be terminated with ; (semi-colon). In following example, there is one statement, containing two expressions. However, the example at the top of this page, contains two statements (each has only one expression) in one line.
<?php echo 123, '<h1>Hello World</h1>'; ?>
A data-type which can hold more than one value (explained in next page, compound data-types), when displayed with echo
function, it will show ERROR. The echo
function will try to display the result, as you can see in second line of OUTPUT of below example that, the word 'Array' is displayed. There are different types of ERRORS, which will only be displayed when you are working in local machine. For example,
<?php echo array(); // this is an empty array, but shows ERROR ?>
Warning: Array to string conversion in C:\xampp\
Array
LOCAL SERVER (apache installed by XAMPP software) is by default, designed to show ERRORS, but HOST (remote) SERVER, (where you had taken some plan/subscription, and you can generally run your website) is, by default designed NOT to show ERRORS. You have to manually input PHP codes in your files, to display ERRORS on remote server, if any one occurs.
Primitive data-type values stored in a variable name, that is, in a memory location, when displayed with echo
function, then that primitive data-type value is converted into a string data-type.
There are many other built-in functions other than echo
which are oftenly used in development mode (while learning PHP on local PC), but following of these built-in functions should not be used in production mode (when PHP files are uploaded on the HOST SERVER).
<?php
print '<h1>Hello World</h1>'; echo '<br />';
print(123);
?>
The above line is the output of above PHP code (when you see it, in WEB-BROWSER by pressing Ctrl U keys simultaneously or typing view-source:print()
function, when you see them in WEB-BROWSER (because <h1> </h1> is a BLOCK level HTML element, and <br /> element is added after it).
Hello World
123
print or print()
is a built-in function, which also displays the output in the WEB-BROWSER. However, you should use it carefully, because it also returns 1 on valid execution of code. For example, <?php echo print 123; ?> or <?php echo print( 123 ); ?> will output 1231 (first three digits for 123 value, and fourth digit 1 for valid execution of code) when you run either of these codes.
There are also other built-in functions, such as printf()
and print_r()
. printf() is a string function, which produces output according to format. print_r() is generally used to display compound data-types, (e.g. arrays) in human-readable output, and it also returns 1 on valid execution of the code.
<?php
var_dump( '<h1>Hello World</h1>' ); echo '<br />';
var_dump( 123 );
?>
<br />int(123)
The above two lines are output from var_dump()
function (when you see it, in WEB-BROWSER by pressing Ctrl U keys simultaneously or typing view-source:
The <br /> HTML element in second line is output from echo
function. Thereafter, int(123) explains that data-type is integer, and the value is 123. You will see following as end-result in your WEB-BROWSER (because <h1> </h1> is a BLOCK level HTML element, and <br /> element is added after it):
Hello World
"int(123)
var_dump() is the built-in function, which displays structured information about one or more expressions that includes its type and value. var_dump()
first displays the data-type of value, and thereafter, the value, itself. (var_dump()
will not convert the data-type, as echo
does.)
ERRORS
When you run either of these codes, <?php print echo 123; ?> or <?php print( echo 123 ); ?> these will output ERROR.
Parse error: syntax error, unexpected token "echo" in C:\xampp\
In this website, you will learn that a statement / expression written in PHP, should always be tested in local file, when you have any confusion or curiosity about something! For example, when I was learning PHP, I have been told that boolean values can only be either in UPPERCASE or lowercse letters. But, today when I tested them in PHP 8.2.0 (the latest version till date), following code also does not show any ERROR.
<?php
echo TrUe; echo '<br />';
echo FaLsE; echo '<br />';
echo True; echo '<br />';
echo False; echo '<br />';
echo '<h2>These four are boolean values, but only first and third show output as 1 </h2>';?>
Thus, there can be different result for same code, you have written five years ago, or ten years ago; or codes written today may be incorrect or illegal and could show ERRORS whenever you or your hosting provider update the PHP to some latest versions. You should be careful and vigilant, as I had also experienced such trouble, when I did not updated my older WORDPRESS website (a PHP framework), and my previous HOSTING PROVIDER has updated the PHP version installed on the SERVER.
I want to explain all built-in function and codes used in examples, I used in any page, but there are many important things, which should not be left, and therefore, I always try to explain them in next pages as possible. Therefore, have patience, and read all WEB-PAGES in totality as soon as I upload them. You will learn about different data-types in next page.
Leave a Comment:

Amit Sinha March 2nd, 2023 at 9:30 PM
😃 😄 😁 😆 😅 😂 😉 😊 😇 😍 😘 😚 😋 😜 😝 😶 😏 😒 😌 😔 😪 😷 😵 😎 😲 😳 😨 😰 😥 😢 😭 😱 😖 😣 😞 😓 😩 😫 😤

Ribhu March 3rd, 2023 at 9:30 PM
🐵 🐒 🐶 🐩 🐺 🐱 🐯 🐅 🐆 🐴 🐎 🐮 🐂 🐃 🐄 🐷 🐖 🐗 🐽 🐏 🐑 🐐 🐪 🐫 🐘 🐭 🐁 🐀 🐹 🐰 🐇