Difference between echo and print statement in php ?

In this article, We’ll see what is the difference between echo and print statement in php ?

echo and print both are used to display the output.

echo accepts an argument list and doesn’t have a return value. it is slightly faster than print and can be used with or without parentheses: echo or echo(). read more




Below an example:

	$user_name = "Saarthak Singh";
	echo $user_name;   // Output  Saarthak Singh
	// or        
	echo ($user_name); // Output  Saarthak Singh
	$name         = "Saarthak Singh";
	$address      = " New Delhi, India";
	echo $name, $address; // Output  Saarthak Singh New Delhi, India

print only accepts a single argument and always returns 1. print is marginally slower than echo and can be used with or without parentheses: print or print(). read more

Below an example:

	$user_name = "Saarthak Singh";
	print $user_name; // Output  Saarthak Singh
	// or        
	print ($user_name); // Output  Saarthak Singh
	$user_name    = "Saarthak Singh";
	$address      = " New Delhi, India";
	print $user_name, $address; // Output  syntax error, unexpected ','
	$user_name = "Saarthak Singh";
	$return    =  print $user_name;
	echo $return; // Output Saarthak Singh1
	// "print $user_name" store in a $return variable and it will show username with return value i.e 1

That’s it!. Please share your thoughts or suggestions in the comments below.

Posted in PHP

2 thoughts on “Difference between echo and print statement in php ?

Leave a Reply

Your email address will not be published. Required fields are marked *