How to display text with a PHP script ?
PHP allows us to display the text in various formats using various inbuilt methods.
Using echo command: The echo command can be used to display text, including numerical, strings and arrays.
Example 1:
PHP
<?php
$string = "GeeksforGeeks!!" ;
echo ( "Printing the string: " );
echo $string ;
?>
|
Output
Printing the string: GeeksforGeeks!!
Example 2: Arrays can’t be printed directly into the script. It is compatible for displaying the data stored in string format. Therefore, the json_encode() method is used to simulate array to string conversion.
PHP
<?php
$arr = array (1, 2, 3, 4, 5);
echo ( 'Array : ' );
echo (json_encode( $arr ));
?>
|
Output
Array : [1, 2, 3, 4, 5]
Using print command: The run time of the print function in PHP is slightly more than that of echo function.
Example 1:
PHP
<?php
$string = "GeeksforGeeks!!" ;
print ( "Printing the string: " );
print $string ;
?>
|
Output
Printing the string: GeeksforGeeks!!
Example 2: Arrays can be printed using the print_r method which is used to print the index along with its values.
PHP
<?php
$arr = array (1, 2, 3, 4, 5);
echo ( 'Array : ' );
print_r( $arr );
?>
|
Output
Array : Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
Last Updated :
22 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...