PHP : Two simple ways to print your "Hello World!"


Hello everyone, today I would like to show you two different ways to print your "Hello World!" string on screen. First, way is to use "echo" and second one is to use "print" keyword to print or display your string. Now, lets get started!!

Note: Both "print" and "echo" are more or less the same; they are both language constructs that display strings. The differences are subtle: print has a return value of 1 so it can be used in expressions whereas echo has a void return type; echo can take multiple parameters, although such usage is rare; echo is slightly faster than print. (Personally, I always use echo, never print.)

Second thing, PHP is not case sensitive , you can use both single quotes and double quotes to print your string but still there are few differences in using both single and double. Change double to single to see differences.

Using "echo"

<?php
$name="World!"; // declaring string variable
echo "Hello $name"; // Output - Hello World!
?>

Using "print"

<?php
$name="World!"; // declaring string variable
print "Hello $name"; // Output - Hello World!
?>

It depends on you , you can either use "echo" or "print" to display your string. Now , what if we change double to single? Any guesses? No? I'll tell you , if you change this to single quotes then instead of printing "Hello World!" , it display "Hello $name" where $name is a variable. But as we said you can both use single and double and display your string. See below code and learn how to display your string using "single quote".

<?php
$name="World!"; // declaring string variable
echo 'Hello' . $name; // Output - Hello World!
?>

Now, as you see to display your string using single quote there is a small difference, you have to use period(.) symbol to concatenate and display your string using single quotes. Unlike, other languages you use "+" symbol to concatenate but here in PHP you use period symbol to concatenate your string and display result.

Now, I hope you enjoy today's post learnt these such simple things about singles and doubles, echo and print. Stay tuned for next PHP posts and sessions, it will be published soon. Thank You!

SHARE

Raunak Hajela

Hi. I’m CEO/Founder of Kodestat. I’m Student, Geek, Web Designer, Developer, Blog Enthusiast and Player. Inspired to make things looks better. I like playing with codes plus I am marvel fan :-P You can check my design portfolio here. For consulation fell free to drop an email at raunakhajela@gmail.com.

  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment