Echo/Print Strings Variables & Commenting in PHP Tutorial

Echo is used to print a string .
  1. <?php
  2. echo "Hello World" ;
  3. // output Hello World
  4. ?>
  5. you might be wondering what is this
PS: Anything coming after "//" will be ignored by PHP compiler "//" used to type notes / comments between the your php code "//" will work for single line for multiple lines you can use "/* */"
  1. <?php
  2. // echo "Hello World" ;
  3. echo "Hello World" ;
  4. /* echo "Hello World" ; */
  5. ?>
Above Code should print "Hello World" THREE Times but since two of them are inside comments First Hello World "//" and Third Hello World "/* */" are inside comments so it will be ignored by php compiler and only second Hello World present in code will print in output. You can separate two strings using a dot(.) . It is also known as concatenation .
  1. <?php
  2. echo "Hello" . "World" ;
  3. // output Hello World
  4. ?>
You can assign a string/value to a variable. You simply need to do this.
  1. <?php
  2. $abc = "Hello World" ;
  3. echo $abc ;
  4. // output Hello World
  5. ?>
PS: While Printing a variable you DONT need to place them in double quotes (" ") if you use double ("$abc") output will be $abc and NOT Hello World.
  1. <?php
  2. $abc = "25" ;
  3. $xyz = "World"
  4. echo $abc . $xyz ;
  5. // output 25 World
  6. ?>
Tags: 

If You Think This Post has helped you buy us a Coffee :)

Make Donation

  • Other: $

Donate now