How To Print Variable Name With Dollar Sign In PHP

By Saruque Ahamed Mollick

Echo dollar sign in PHP is very easy if you know where to use the single quote with echo statement and where to use the double quote with echo.

In this tutorial, I will explain each of them with the example so that it becomes easy for you to understand how to print dollar sign using PHP. Though I can directly tell you the method to print dollar sign, but I will not do so. Because as a beginner if you follow my examples one by one, you will remember the use of quotation with PHP for a long time or may me forever.

I assume that you all know how to use echo in PHP. But still here is the small example of echo to print something on the browser.



<?php
     echo "Hello World";
?>

This is a simple echo example. Now suppose you want to print the value of a variable using echo what will you do?

<?php

$website="CodeSpeedy";

echo $website;
?>

OUTPUT:

CodeSpeedy

Echo dollar sign in PHP as a prefix of a variable

Just simply, wrap the variable name in echo statement with the single quotes.

<?php

$website="CodeSpeedy";

echo '$website';
?>

Output:

$website

So using the above PHP program you can print anything you want. You just need to use the single quote with echo statement.

To print, only a single dollar just do this

<?php
     echo '$';
?>

Also Read,

Echo and Print Statement Difference in PHP

 

But what will happen if you use the double quote with echo?

Double quote with echo statement in PHP

<?php

$website="CodeSpeedy";

echo "$website";
?>

It will print:

CodeSpeedy

So I hope you can understand what is the difference between the single quote and double quote with echo in PHP.

Even you can use braces like this,

<?php

$website="CodeSpeedy";

echo ('$website');
?>

OUTPUT:

$website

You can also use print statement instead of echo statement to achieve the same goal,

<?php

$website="CodeSpeedy";

print '$website';
?>

Output:

$website

But Echo and Print in PHP are not same so I will suggest you to read Echo and Print Statement Difference in PHP
to learn more about the internal fundamental difference between echo and print in PHP.

Leave a Reply

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