Friday, October 10, 2025
HomeLanguagesHow to display text with a PHP script ?

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 = "neveropen!!";
  
// Printing the string directly
echo ("Printing the string: ");
  
// Printing the variable
echo $string;
?>


Output

Printing the string: neveropen!!

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
  
// Declaring an array
$arr = array(1, 2, 3, 4, 5);
echo('Array : ');
  
// Printing 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 = "neveropen!!";
  
// Printing the string directly
print("Printing the string: ");
  
// Printing the variable
print $string;
?>


Output

Printing the string: neveropen!!

Example 2: Arrays can be printed using the print_r method which is used to print the index along with its values. 

PHP




<?php
  
// Declaring an array
$arr = array(1, 2, 3, 4, 5);
echo('Array : ');
  
// Printing array
print_r($arr);
?>


Output

Array : Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6717 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6792 POSTS0 COMMENTS