PHP Variables, Echo, and Print Explained
Understand what PHP variables are, how to name them, and when to use echo or print to display values. Clear examples make the differences easy to see.

Contents

PHP VARIABLES
As we start this lesson on PHP variables along with echo and print, I’m assuming your PHP development environment is ready. The thing you’ll use most often when building with PHP is, of course, variables. But the first thing you need to pay attention to when using variables is naming — that is, the names you give your variables. Make sure these names don’t repeat within the code and that they stay semantically clear when someone reads the code later. Otherwise, believe me, you may end up having some rough hours down the road. 🙂 Beyond that, the other thing to watch out for is that you found a good name for your variable, but how it’s written matters too. Let’s make this clearer with a few examples below;
Example:
$kitapSayisi = 1; // Correct spelling
$kitapAdedi = 1; // Correct spelling
Both examples above are spelled correctly, but since they mean the same thing, use only one of them during development. Otherwise it’ll be hard to tell which parameter is which. Now let’s continue with a few different examples.
Example:
$kitapsayisi = 1; // Correct spelling but not readable
$kitapadedi = 1; // Correct spelling but not readable
$kitap Sayisi= 1; // Incorrect spelling
$kitap Adedi 1; //
In this example I’ve given both a correct and an incorrect version. If you look at the $kitapSayisi and $kitapsayisi variables we wrote in the first example, you’ll notice that $kitapSayisi is easier to read. What you need to do is always start the first word of the variable with lowercase letters, and capitalize the first letter of each following word. This way, the variables and functions you use in your code will be both more readable and more elegant, and by following PHP variable naming rules you’ll get used to a more professional style. Please follow the variable naming and declaration rules in your future work.
Now let’s talk about the other naming rules;
- In PHP, variables never start with a numeric value.
- In PHP variables you can only use letters, numbers and the underscore ( _ ). Special characters are not used.
- Non-ASCII characters can’t be used in variable names.
- Variables are case-sensitive, so the $abcde variable and the $ABCDE variable are not the same.
- When declaring a PHP variable, it must always have the $ sign at the beginning.
What Are Variables?
You can think of PHP variables as boxes you put things into. We can empty these boxes whenever we want and update their contents whenever we want. I’ll also explain what you can do with these variables when we get to advanced levels. Now let’s slowly start printing things to the screen and get a bit more comfortable with variables.
<?php
$kitapSayisi = 12;
echo $kitapSayisi ;
// the screen output will be 12.
?>
When you run the example above, you saw 12 printed on the screen, but you also saw a command I hadn’t mentioned before: ” echo ” . If we want to print something to the screen in PHP, we use echo or print, and PHP’s echo and print are more or less very similar to each other. There’s a very small difference that sets them apart. Both statements are used to output data to the screen.
The differences between print and echo are as follows;
- Print has a return value of 1, but echo has no return value.
- Echo can take more than one argument (possible, though rarely used), while print can take only one argument.
- Echo is by far faster than print.
You can review the examples below.
<?php
$txt1 = "PHP Öğreniyorum";
$txt2 = "serhatalpaslan.com";
$x = 8;
$y = 2;
print "<h2>" . $txt1 . "</h2>";
print "PHP Öğreniyorum " . $txt2 . "<br>";
print $x + $y;
?>
If you’d like to examine how PHP variables are used at an advanced level on php.net as well, you’ll see for yourself that there are many different usage patterns. I’ll also try to explain advanced variable usage to you in a separate lesson or by updating this article. Our next lesson will be about external variables, so please do a bit of practice on this topic and study from a few different sources as well and follow my next PHP lesson.
Share it if it helped


