PHP echo and print statements are language constructs and not functions. You are free to use them without parentheses.
Both of these constructs are for outputting strings. Differences between print and echo constructs are minor but echo can be seen as a more time-efficient option.
Additionally, echo can accept several parameters, while print can be used as an expression since it can return a value of 1.
PHP echo and print: Main Tips
echostatements are one of the most commonly used constructs to output data (text, variables, etc.).
- You don't need to use parentheses with them since they are technically not functions.
print_ris a function, mostly used for debugging.
echo Language Construct: Proper Use
By using PHP
echo, you can print one or multiple strings. Let's look at a PHP example of how you can achieve that:
echo "<h2>Am I ready?!</h2>"; echo "Howdy!<br>"; echo "I'm ready!<br>"; echo "I ", "made ", "this ", "string ", "with my handy hands!";
In the code below, you can learn how to display both text and values of variable values by using
echo. You can see 4 different variables in this PHP example:
"I am "; $text2 = "Ready!"; $var1 = 4; $var2 = 3; echo "<h2>" . $text1 . $text2 . "</h2>"; echo $var1 + $var2;$text1 =
Note: when you assign parameters to echo, you need to write them within parentheses.
- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features
- Nanodegree programs
- Suitable for enterprises
- Paid certificates of completion
- Professional service
- Flexible timetables
- A variety of features to choose from
- Professional certificates of completion
- University-level courses
- Multiple Online degree programs
- Great user experience
- Offers quality content
- Very transparent with their pricing
- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable
How print Is Used
This PHP example below shows how you can display text using PHP
print "<h2>I am ready to learn PHP!</h2>"; print "Hey there!<br>"; print "I will learn ALL the PHP!";
The code of the PHP example below shows how you can display both text and a PHP variable using PHP
"Learning PHP"; $text2 = "ALL the PHP"; $var1 = 58; $var2 = 4; print "<h6>" . $text1 . $text2 . "</h6>"; print $var1 + $var2;$text1 =
How print_r Is Different from print and echo
The use cases of
print_r in PHP are different.
print_r in PHP returns details about variables in a more human-readable form. It provides data type of variables. When used on arrays,
print_r returns all the elements in the list.
In this code example, we are making PHP print arrays by using the
array ('a' => 'airplane', 'b' => 'train', 'c' => array ('o', 'k', 'f')); print_r ($a);$a =
Note: to show information in multiple lines, apply \n with the print_r. Also, include <pre> to indicate several lines.
PHP echo and print: Summary
echoPHP statements are for displaying text or variables.
- Parentheses are not required with these language constructs.
echooutput information to the screen, while
print_rin PHP shows information about variables in a more human-readable way.
Latest Udacity Coupon Found:
75% OFF COURSES
Udacity Black Friday Offer
The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!