PHP echo and print Statements
echo and
print are more or less the same. They are both used to output data to the
screen.
The
differences are small: echo has no return value while print has a return value
of 1 so it can be used in expressions. echo can take multiple parameters
(although such usage is rare) while print can take one argument. echo is
marginally faster than print. The echo statement can be used with or without parentheses:
echo or echo().
Example
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
The print statement
can be used with or without parentheses: print or print().
Example
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
The
following example shows how to output text and variables with the print
statement:
<?php
$txt1 = "Learn PHP";
$txt2 = "www.shahblogs.com";
$x = 5;
$y = 4;
print "<h2>$txt1</h2>";
print "Study PHP at $txt2<br>";
print $x + $y;
?>
$txt1 = "Learn PHP";
$txt2 = "www.shahblogs.com";
$x = 5;
$y = 4;
print "<h2>$txt1</h2>";
print "Study PHP at $txt2<br>";
print $x + $y;
?>
Constants
A
constant is an identifier (name) for a simple value. The value cannot be
changed during the script. A valid
constant name starts with a letter or underscore (no $ sign before the constant
name).
Note:
Unlike variables, constants are automatically global across the entire script.
Syntax
define(name,
value, case-insensitive)
Parameters:
name: Specifies the name of the constant
value: Specifies the value of the constant
case-insensitive: Specifies whether the constant name
should be case-insensitive. Default is false.
1. <?php
define("A", "Welcome to www.shahblogs.com!");
echo A;
?>
define("A", "Welcome to www.shahblogs.com!");
echo A;
?>
2. <?php
define("GREETING", "Welcome to www.shahblogs.com!", true);
echo greeting;
?>
define("GREETING", "Welcome to www.shahblogs.com!", true);
echo greeting;
?>
3. <?php
define("GREETING", "Welcome to www.shahblogs.com!");
function myTest()
{
echo GREETING;
}
myTest();
?>
define("GREETING", "Welcome to www.shahblogs.com!");
function myTest()
{
echo GREETING;
}
myTest();
?>
0 comments
Post a Comment