w3resource

PHP Print

Description

Print is used to display a string.

Syntax

print(string $val)

Parameters

val : The input data.

Return Values

Returns 1, always.

Print() is not actually a real function, it is a language construct like echo. There is some difference between the two, echo is marginally faster compare to print as echo does not return any value. Echo can accept multiple parameters without parentheses but print can accept only one parameter.

Example to display simple string with print()

<?php
print 'One line simple string.<br />';
print 'Two lines simple
string example<br />';
print 'Tomorrow I \'ll learn PHP global variables.<br />';
print 'This is a bad command : del c:\\*.* <br />';
?>

All the above print commands simply display the corresponding string, here we have used an additional html command <br /> at end of each print statement to generate a line break. 

Output:

One line simple string.
Two lines simple  string example
Tomorrow I 'll learn PHP global variables.
This is a   bad command : del c:\*.*

View the example in the browser

Advance example of PHP print()

<?php
// Variables inside an echo statement.
$abc='We are learning PHP';
$xyz='w3resource.com';
print "$abc at $xyz <br />";
// Simple variable display
print $abc;
print "<br />"; // creating a new line
print $xyz;
print "<br />"; // creating a new line
// Displaying arrays
$fruits=array('fruit1'=>'Apple','fruit2'=>'Banana');
print "Fruits are : {$fruits['fruit1']} and {$fruits['fruit2']}" ;
?>

Output:

  We are learning PHP at w3resource.com
  We are learning PHP
  w3resource.com
  Fruits are : Apple and Banana 

View example in the browser

Practice here online:

Previous: Echo statement
Next: $GLOBALS



PHP: Tips of the Day

PHP: How to convert string to boolean?

Strings always evaluate to boolean true unless they have a value that's considered "empty" by PHP (taken from the documentation for empty):

  1. "" (an empty string);
  2. "0" (0 as a string)

If you need to set a boolean based on the text value of a string, then you'll need to check for the presence or otherwise of that value.

$test_mode_mail = $string === 'true'? true: false;

EDIT: the above code is intended for clarity of understanding. In actual use the following code may be more appropriate:

$test_mode_mail = ($string === 'true');

or maybe use of the filter_var function may cover more boolean values:

filter_var($string, FILTER_VALIDATE_BOOLEAN);

filter_var covers a whole range of values, including the truthy values "true", "1", "yes" and "on".

Ref : https://bit.ly/33SQ3eP