String Output and var_dump() - Images: Japanese
Hey there, everyone!
Last time we covered 'escape characters' and similar topics. By now you should have a solid feel for how strings behave in PHP, so let's keep that momentum going and look at the different ways to output strings.
PHP has several ways to output strings. You've already seen 'echo' pop up throughout the sample code so far, but there's also something called 'print' that can output strings as well.
And we can't forget the function 'var_dump()'. This might just be the single most frequently written function in PHP development — it's that important.
Let's take a look at all of them.
First up: the basics of 'echo' and 'print'. These two don't differ much in functionality. Let's recall how we've been using 'echo' — the classic 'Hello world' program from the very beginning.
<?php echo 'Hello world';
You can rewrite this using print and it works the same way.
<?php print 'Hello world';
However, 'print' has one feature that 'echo' doesn't: after outputting the string, it always returns the value '1' as a return value.
Note: 'Return values' will be explained in a later article.
<?php $txt = print 'Hello world'; // The value '1' is assigned to the variable '$txt'. print $txt; // Outputs the number '1'.
In practice, you'll rarely need to capture that return value from 'print'. And since 'echo' has no extra overhead from that feature, it tends to be very slightly faster — so 'echo' is generally the go-to choice for outputting strings.
(The speed difference is genuinely tiny, so personally either one is fine to use.)
There's one more difference worth knowing: 'echo' lets you separate multiple strings with ',' (commas), while 'print' does not. You don't see the comma syntax very often, so just keep it in the back of your mind.
<?php // This works. echo 'Hello', ' world';
<?php // This does not work. print 'Hello', ' world';
By the way, both echo and print are classified as language constructs rather than functions.
In programming, a named block of logic is called a function, and the syntax rule for calling a function is to put () at the end. Take a look at the code above.
Note: Functions and how to use them are covered in a later article.
<?php echo 'Hello world'; print 'Hello world';
As you can see, they run just fine without '()'. That's what makes them language constructs. In practice, whether something is a function or a language construct doesn't really matter — it just means you don't need the parentheses.
That said, you can add () if you want, and it'll still work.
<?php
echo('Hello world');
print('Hello world');
In real-world PHP code, leaving out the '()' is more common. And that's the rundown on 'echo' and 'print'!
PHP is a language built specifically for web development, and since that's its main use case, you'll be writing 'echo' constantly. When you're deep in a large PHP file, it starts to feel like all you ever type is 'echo', 'echo', 'echo'...
To help with that, PHP 5.4 added a short tag — <?= and ?> — that works just like echo. Here's how it looks:
<?php $name = 'sakurama'; ?> Hello, <?= $name ?>!
This is equivalent to the following:
<?php $name = 'sakurama'; ?> Hello, <?php echo $name; ?>!
So if you're expecting a lot of 'echo' statements, using the '<?=' short tag is a handy option.
Keep in mind though, some PHP versions have this disabled by default. To enable it, open 'php.ini', find 'short_open_tag = Off', and change it to 'short_open_tag = On'. That should do the trick.
Note: 'php.ini' is covered in a later article.
There's also a common opinion that "PHP short tags should not be used", so if you're writing PHP professionally, check with your team first before reaching for them.
Next up is 'var_dump()'. You pronounce it "var dump". Like 'echo' and 'print', it outputs content — but it gives you a lot more detail. Let's try it out. Take a look at this sample:
<?php
var_dump('Hello world');
This is the Hello world program written using var_dump(). Running it produces the following output:
string(11) "Hello world"
Along with Hello world, you get string(11) prepended to it. This means: "the value is of type string (text) and is 11 characters long."
There's something called an 'array' that we'll cover in a later article — but arrays can't be displayed with 'echo' or 'print'. Take a look at this sample:
<?php $txts = ['初音ミク', 'IA']; echo $txts;
Running this produces the following output:
Array
'Array' just means 'array' — but it doesn't show you what's actually inside. Not very helpful! Now let's rewrite the same code using 'var_dump()':
<?php $txts = ['Miku', 'IA']; var_dump($txts);
Running this gives you:
array(2) {
[0]=>
string(4) "Miku"
[1]=>
string(2) "IA"
}
That's a lot more information! It's telling you: "the value is an array with 2 elements; index 0 is a string, 4 characters long, value 'Miku'..." and so on. It might look like a lot if you're not used to arrays yet, but 'var_dump()' is incredibly thorough and detailed.
'var_dump()' gets used all the time — for inspecting variables and arrays, debugging code, and all sorts of other situations. Get comfortable with it, because it'll be showing up a lot in the articles ahead.
And that's a wrap for this one! Next up we'll cover 'comments' and 'commenting out'. See you in the next article!
This article was written by Sakurama.
Author's beloved small mammal |
桜舞 春人 Sakurama HarutoA Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him. |
If you find any errors or copyright issues, please contact us.