articles

Home / DeveloperSection / Articles / PHP Syntax and Echo or Print

PHP Syntax and Echo or Print

Royce Roy 2035 04-Jun-2016
How to Write PHP Code?

A PHP script starts with <?php and ends with ?>.

It can be placed anywhere in the program.

First we will start with a simple PHP code to print “This is my first PHP program”.

The extension of the file is given as .php.

<html>
<body>
<?php echo “This is my first PHP program.”?>
</body>
</html>

  Result: This is my first PHP program.

 Note: echo is used to give the output.

Comments in PHP

A comment is the portion of aprogration which only meant for human reader.Its only purpose is to read by someone who is looking to your program.

Comments can be used to:

1-  Let others understand what you are doing

2- Remind yourself what you did.

<?php

# This is single line cmment
// This is also single line comment
/* This is multi lines comment
Author:XYZ
Purpose:Multi lines Comment*/
?>


PHP is Case sensitive

Yes PHP is case sensitive. Try Out yourself:

<?php

$text=50;
print(“Variable capital is $text<br>”);
print(“Variable capital is $Text<br>”);
?>

 

Result:

Variable capital is 50

Variable capital is

 Echo/Print

In PHP echo and print both are used to display output.Both of them more or less are same.

The main difference between echo and print is that:

Echo does not return any value but print return value 1 in expression. Let’s see some examples:

Display text using echo:  
<?php

echo "Hello world!";
?>

Output: 
“Hello world”
 Display Variable using echo
<?php

$a= 5;
$b=4;
echo $a+ $b;
?> 
Output:
9
Display text using print:
<?php

print "Hello world!<br>";
?> 
Output:
“Hello world”
 Display Variable using print:
<?php

$a= 5;
$b=4;
print $a+ $b;
?>

  Output: 

9


php php 
Updated 12-Jan-2019

Leave Comment

Comments

Liked By