
PHP has several functions to print text to the screen:
- function
echo(); - function
print(); - function
printf().
These functions will often be used in PHP programming.
Then, what are the differences between these three functions?
Let's discuss ...
Echo function ()
Fungi
echo()is a function to display text to the screen. This function can be used with parentheses or without parentheses.
Example:
<?php
echo "<h2>Belajar PHP itu mudah!</h2>";
echo("Hello world!<br>");
echo "Aku sedang belajar PHP!<br>";
echo "Ini ", "teks ", "yang ", "dibuat ", "terpisah.";
?>
The result:

The function
echo()will return nothing after being executed. He is only in charge of displaying text.Print () function
Functions are the
print()same as functions echo(). He is also used to display text to the screen. The function print()can also be used without parentheses.
The difference with
echo():- The function
print()will always return the value1when executed, whileecho()not returning anything. - Functions
print()can only be given one parameter, whileecho()may be more than one.
Example:
<?php
print "<h2>Belajar PHP dari Nol!</h2>";
print "Hello world!<br>";
print "Belajar mecetak teks di PHP!";
?>
The result:

If we create a variable then fill it with a function
print()like this:$cetak = print("Hello World!");
Then the variable
$cetakwill be worth 1.
If we give two parameters to the function
print(), an error will occur.print("Hello", "World"); // <-- ini akan error
Experiment on the PHP console:

Combining Strings or Text in PHP
In functions
echo(), we combine text or strings by giving as arguments (separated by commas).echo "Ini ", "teks ", "yang ", "dibuat ", "terpisah.";
Besides this method, we can also do it with a period (
.).
Point is an operator to combine two texts in PHP.
Why not use a plus (
+) symbol ?
In PHP, plus symbols are used for addition, not for combining strings.
So:
echo "1" + "1"; // akan menghasilkan 2
echo "1" . "1"; // akan menghasilkan 11
Example:
<?php
$txt1 = "Tutorial PHP";
$txt2 = "petanikode.com";
$x = 5;
$y = 4;
echo "<h2>" . $txt1 . "</h2>";
echo "Aku belajar pemrograman PHP di " . $txt2 . "<br>";
echo $x + $y;
?>
The result:

Printf () function for Text Formatting
Function
printf()is a function for formatting text or strings. This function will return the length of the text when executed.
Usually when we use a function
echo, we will write like this:$txt = "petanikode.com";
echo "Aku belajar pemrograman PHP di " . $txt . "<br>";
If we want to use a function
printf(), we can do it like this:$txt = "petanikode.com";
printf("Aku belajar pemrograman PHP di %s<br>", $txt);
A symbol
%sis a placeholder for text (strings). Besides symbols %sthere are also symbols:%dfor decimal numbers (integers);%ffor fractions (float);%bfor boolean.
One that we should try is
%f, because with this we can set how fractions are displayed.
Example:
// misalkan kita punya bilangan dengan notasi E seperti ini
$harga = 100000;
// jika kita cetak dengan echo:
echo "Harganya adalah Rp $harga";
// jika kita cetak dengan printf
printf("Harganya adalah Rp %.2f", $harga);
The result:
Harganya adalah Rp 100000
Harganya adalah Rp 100000.00
Symbol
%.2fmeans we will place a number with two numbers behind the comma.Print quotation marks
Sometimes we want to place quotes like this:
echo 'hari ini hari jum'at';
Then an error will occur!
Because there is considered to be a closing text ending in the text
jum.
Then how do we print quotes?
We can use the back slash (
\) sign .
Example:
echo 'hari ini hari jum\'at';
Then it will produce:
hari ini hari jum'at
Reference: https://www.petanikode.com/php-echo-print/
0 Komentar untuk "Learning PHP: Differences in echo (), print (), and printf () functions in PHP"
Silahkan berkomentar sesuai artikel