As you saw in the previous lesson, the PHP command echo is a
means of outputting text to the web browser. Throughout your PHP career
you will be using the echo command more than any other. So let's give
it a solid perusal!
Outputting a String
To output a string, like we have done in previous lessons, use PHP echo. You can place either a string variable or you can use quotes, like we do below, to create a string that the echo function will output.PHP Code:
<?php $myString = "Hello!"; echo $myString; echo "<h5>I love using PHP!</h5>"; ?>
Display:
Hello!
Tip: The echo() function is slightly faster than print().
Tip: The echo() function has the following shortcut syntax. See example 5.
The output of the code above will be:
The output of the code above will be:
The output of the code above will be:
The output of the code above will be:
I love using PHP!
Tips and Notes
Note: The echo() function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo(), using parentheses will generate a parse error.Tip: The echo() function is slightly faster than print().
Tip: The echo() function has the following shortcut syntax. See example 5.
Example 1
<?php
$str = "Who's Kai Jim?";
echo $str;
echo "<br />";
echo $str."<br />I don't know!";
?>
$str = "Who's Kai Jim?";
echo $str;
echo "<br />";
echo $str."<br />I don't know!";
?>
Who's Kai Jim?
Who's Kai Jim?
I don't know!
Who's Kai Jim?
I don't know!
Example 2
<?php
echo "This text
spans multiple
lines.";
?>
echo "This text
spans multiple
lines.";
?>
This text spans multiple lines.
Example 3
<?php
echo 'This ','string ','was ','made ','with multiple parameters';
?>
echo 'This ','string ','was ','made ','with multiple parameters';
?>
This string was made with multiple parameters
Example 4
Difference of single and double quotes. Single quotes will print the variable name, not the value:
<?php
$color = "red";
echo "Roses are $color";
echo "<br />";
echo 'Roses are $color';
?>
$color = "red";
echo "Roses are $color";
echo "<br />";
echo 'Roses are $color';
?>
Roses are red
Roses are $color
Roses are $color
Example 5
Shortcut syntax:
<html>
<body>
<?php
$color = "red";
?>
<p>Roses are <?=$color?></p>
</body>
</html>
<body>
<?php
$color = "red";
?>
<p>Roses are <?=$color?></p>
</body>
</html>
0 comments:
Post a Comment