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
Practice here online:
Previous: Echo statement
Next: $GLOBALS
PHP: Tips of the Day
How to Sort Multi-dimensional Array by Value?
Try a usort, If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:
Example:
function sortByOrder($a, $b) { return $a['order'] - $b['order']; } usort($myArray, 'sortByOrder');
Starting in PHP 5.3, you can use an anonymous function:
usort($myArray, function($a, $b) { return $a['order'] - $b['order']; });
And finally with PHP 7 you can use the spaceship operator:
usort($myArray, function($a, $b) { return $a['order'] <=> $b['order']; });
To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.
usort($myArray, function($a, $b) { $retval = $a['order'] <=> $b['order']; if ($retval == 0) { $retval = $a['suborder'] <=> $b['suborder']; if ($retval == 0) { $retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder']; } } return $retval; });
If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual
Ref : https://bit.ly/3i77vCC
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework