You are advised to check corresponding YouTube video at the end of this article.
<?php
$name = "John";
$last_name = "Smith";
$street = "Main Street";
$house_number = 54;
//One liner
echo "<h1>Name: $name </h1>";
//Concatenation
echo "<h1>" . "Name: $name" . "</h1>";
//Pure Mess
echo "<h1>";
echo "Name: ";
echo $name;
echo "</h1>";
?>
This code sets several variables with strings and numbers. It then demonstrates three different ways to output a string with the value of the $name
variable.
-
The first method uses a one-liner with the
echo
statement and double quotes. It concatenates the string "Name: " with the value of the$name
variable and outputs it within an HTML heading element. -
The second method uses concatenation with the dot (.) operator to combine the string "Name: " with the value of the
$name
variable. It then uses theecho
statement to output the resulting string within an HTML heading element. -
The third method uses separate
echo
statements to output the string "Name: " and the value of the$name
variable, each within their own heading element. This method is less concise than the previous two, but can be easier to read for longer strings or more complex output.
In all three methods, the output will be an HTML heading element with the text "Name: John".
<?php
$name = "John";
$last_name = "Smith";
$street = "Main Street";
$house_number = 54;
//One liner
print "<h1>Name: $name </h1>";
//Concatenation
print "<h1>" . "Name: $name" . "</h1>";
//Pure Mess
print "<h1>";
print "Name: ";
print $name;
print "</h1>";
?>
The above code in PHP is using the print statement to output some HTML code to the web page. It creates four variables: $name
, $last_name
, $street
, and $house_number
with values assigned to them.
The code then uses the print
statement to output the variables $name
in three different ways:
- One liner: using double quotes to include the variable directly in the string.
- Concatenation: using the
.
operator to concatenate the string literal "Name: " with the value of$name
. - Pure mess: using separate
print
statements to output the string literal "Name: " and the value of$name
with HTML tags.
Each of the three ways produces the same output:
Name: John
The print
statement in PHP works similarly to the echo
statement and can be used to output strings to the web page. However, print
is a bit slower than echo.
print can be used as part of a more complex expression where echo cannot.
<?php
$name = "John";
$last_name = "Smith";
$street = "Main Street";
$house_number = 54;
//One liner
print("<h1>Name: $name </h1>");
//Concatenation
print("<h1>" . "Name: $name" . "</h1>");
//Pure Mess
print("<h1>");
print("Name:");
print($name);
print("</h1>");
?>
The first print()
statement outputs the value of the $name
variable within an HTML heading element (<h1>
).
The second print()
statement concatenates the string "Name: " with the value of the $name
variable using the dot (.
) operator, then outputs the resulting string within an HTML heading element.
The third print()
statement outputs the string "Name:" using the print()
function, then outputs the value of the $name
variable using another print()
function. Finally, it outputs a closing HTML heading element (</h1>
).
All of these statements accomplish the same task of outputting the value of the $name
variable within an HTML heading element, but they use different methods to do so.
No comments:
Post a Comment