Echo, PHP Statement, Print, Concat and Comments in PHP
Categories - PHP Tags - PHP   Maniruzzaman Akash   5 months ago   363   0

Echo, PHP Statement, Print, Concat and Comments in PHP

In Previous chapter, we’ve learned about variable and it’s data types. Today, we’ll learn also some important thing in PHP programming language.

  1. echo
  2. print &
  3. commenting in PHP code

echo

In PHP, to output something to display/console/browser, we can use echo or print. So, echo is used to print / output something in browser.

Let’s we want to show Welcome to learn PHP, so that’s simple -

<?php
  echo "Welcome to learn PHP";
?>

Output:

Welcome to learn PHP

Echo/Print Variable in PHP

Example 1:

Suppose, we’ve a $price variable and it’s value is 10 and we want to print that price -

<?php
  $price = 10;
  echo $price;
?>

Output:

10

Example 2:

Suppose, we’ve a $name variable and it’s value is Jhon and we want to print that name with Hello Jhon -

<?php
  $name = "Jhon";
  echo "Hello " . $name;
?>

Output:

Hello Jhon

Note: In the above example, we’ve added a dot (.) sign after "Hello " string. This is called Concat or adding in the aspect of PHP programming language.

In our, next examples, we’ll use this echo and concat a lot for our every example.

Example 3:

Suppose, we’ve two variables $number1 and $number2 and we want to print the summation after keeping those in a $summation variable -

<?php
  $number1 = 10;
  $number2 = 20;
  $summation = $number1 + $number2;
  echo "Summation = " . $summation;
?>

Output:

Summation = 30

Wow, great, we’ve made a simple summation calculator using this kind of PHP knowledge. So, we can print - text, variable, concat two strings or variables and many more.

Example 4: echo()

We can also use echo() instead of echo. But echo is preferred.

<?php
  $number1 = 10;
  $number2 = 20;
  $summation = $number1 + $number2;
  echo ("Summation = " . $summation);
?>

Output:

Summation = 30

Run PHP Code Online

print & Difference between echo and print

print and echo are almost same. print is also used for output in PHP programming. Only difference between them is -

  1. echo has no return value
  2. print has a return value of 1

So, what we’ve done using echo, we can do that using print. Let’s do the first example with print -

<?php
  print "Welcome to learn PHP";
?>

Output:

Welcome to learn PHP

And rest of the examples are also valid for print in PHP.

comment

In our HTML class, we seen how to comment in HTML. That is like -

<!-- HTML Comment & it'll not be visible in Browser -->

In PHP, there is also a commenting system in Code. There are some approach for commenting in PHP -

  1. Inline commenting - // Comment after this Hash sign. Use // sign and after that add our comment.
  2. Multiline commenting - /* Comment inside here */

1. Inline commenting - // Comment

Let’s add a comment in our first example of what it’ll do -

<?php
  print "Welcome to learn PHP"; // This will print Welcome to learn PHP
?>

2. Multi-line commenting - /* Comment */

<?php
  $number1 = 10; // Assign number1 to 10
  $number2 = 20; // Assign number2 to 20
  
  /*
  * Let's make summation of those two number.
  * By adding those two number.
  * Summation = number 1 + number 2;
  */
  
  $summation = $number1 + $number2;
  echo ("Summation = " . $summation);
?>

Output:

Summation = 30

PHP Statement

I think, you’ve already looked, we use semicolon ; after every line in our above examples. In PHP, to terminate any statement, we use semicolon - ;. So, after any print or echo we must have to use semicolon as, the statement finishes after an echo or print.

<?php
  $number1 = 10;
  $number2 = 20;
  $summation = $number1 + $number2;
  echo "Number1 = " . $number1 . "<br>";
  echo "Number2 = " . $number2. "<br>";
  echo "Summation = " . $summation;
?>

Output:

Number1 = 10
Number2 = 20
Summation = 30

Previous
Data Types in PHP - Variable Types in PHP
Next
PHP Operators - Arithmetic operator, Assignment Operator, Increment-decrement Operator