printCurrently, it is considered as
a language construction, but can also be considered as a
function.
Unlike the echo function, it returns no value for verification other than its own result.
CAN NOT BE USED TO PRINT ARRAYS and/or OBJECTS.
Is intended to print values of STRINGS and/or NUMBERS and information about RESOURCES.
The delimitation by means of parentheses can be dispensed.
Under particular conditions, it can be used to print the types: BOOLEAN and NULL.
<?php
int print ( str $arg );
where,
$arg = ELEMENT to be printed
?>
$arg
The element to be printed.
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int print ( str $arg )
SOME VALID VARIABLE EXAMPLES
STRINGS: INTERNAL
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
print $_SERVER['REMOTE_ADDR'];
// GETS THE REMOTE ADDRES (IP)
print $_SERVER['HTTP_USER_AGENT'];
// GETS THE USER AGENT (BROWSER)
print $_SERVER['HTTP_ACCEPT_LANGUAGE'];
// GETS THE ACEPTED LANGUAGE
?>
RESULT
- - - - - - - - - - - - - - - - - - - - - - - - - - -
::1
- - - - - - - - - - - - - - - - - - - - - - - - - - -
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7
- - - - - - - - - - - - - - - - - - - - - - - - - - -
You may get different results.
Everything will depend on the system involved.
- - - - - - - - - - - - - - - - - - - - - - - - - - -
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SOME VALID VARIABLE EXAMPLES
STRING: USER-DEFINED
All VARIABLES after the value assignment
must end with a SEMICOLON
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$xyz = "STRING DELIMITED BY DOUBLE QUOTATION MARKS";
$str0 = 'STRING DELIMITED BY SINGLE QUOTATION MARKS';
$carÇ = 'STRING DELIMITED BY SINGLE "QUOTATION MARKS"';
$_alo = "STRING DELIMITED BY DOUBLE 'QUOTATION MARKS'";
$rquo = " » ";
$str01 = "1AnVXY234";
$str02 = "1234567";
// HEREDOC
$str03h = <<<EOD
Easy come, easy go.
If you can’t beat them, join them.
Life begins at forty.
Two heads are better than one.
EOD;
$str04h = <<<EOT
In PHP there are TWO types
of variables:
INTERNAL, (predefined).
OF USER: (user-defined).
EOT;
// NOWDOC
$str03n = <<<'EOD'
Easy come, easy go.
If you can’t beat them, join them.
Life begins at forty.
Two heads are better than one.
EOD;
$str04n = <<<'EOT'
In PHP there are TWO types
of variables:
INTERNAL, (predefined).
OF USER: (user-defined).
EOT;
print $xyz . '<br><br>' . $str0 .
'<br><br>' . $carÇ .
'<br><br>' . $_alo . '<br><br>';
print $rquo . '<br><br>' . $str01 .
'<br><br>' . $str02 . '<br><br>';
print $str03h . '<br><br>' . $str04h . '<br><br>';
print $str03n . '<br><br>' . $str04n;
?>
RESULT
- - - - - - - - - -
STRING DELIMITED BY DOUBLE QUOTATION MARKS
- - - - - - - - - -
STRING DELIMITED BY SINGLE QUOTATION MARKS
- - - - - - - - - -
STRING DELIMITED BY SINGLE "QUOTATION MARKS"
- - - - - - - - - -
STRING DELIMITED BY DOUBLE 'QUOTATION MARKS'
- - - - - - - - - -
»
- - - - - - - - - -
1AnVXY234
- - - - - - - - - -
1234567
- - - - - - - - - -
Easy come, easy go. If you can’t beat them, join them. Life begins at forty. Two heads are better than one.
- - - - - - - - - -
In PHP there are TWO types of variables: INTERNAL, (predefined). OF USER: (user-defined).
- - - - - - - - - -
Easy come, easy go. If you can’t beat them, join them. Life begins at forty. Two heads are better than one.
- - - - - - - - - -
In PHP there are TWO types of variables: INTERNAL, (predefined). OF USER: (user-defined).
- - - - - - - - - -
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SOME VALID VARIABLE EXAMPLES
STRING: USER-DEFINED
All VARIABLES after the value assignment
must end with a SEMICOLON.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$xyz = "STRING DELIMITED BY DOUBLE QUOTATION MARKS";
$str0 = 'STRING DELIMITED BY SINGLE QUOTATION MARKS';
$carÇ = 'STRING DELIMITED BY SINGLE "QUOTATION MARKS"';
$_alo = "STRING DELIMITED BY DOUBLE 'QUOTATION MARKS'";
$rquo = " » ";
$str01 = "1AnVXY234";
$str02 = "1234567";
// HEREDOC
$str03h = <<<EOD
Easy come, easy go.
If you can’t beat them, join them.
Life begins at forty.
Two heads are better than one.
EOD;
$str04h = <<<EOT
In PHP there are TWO types
of variables:
INTERNAL, (predefined).
OF USER: (user-defined).
EOT;
// NOWDOC
$str03n = <<<'EOD'
Easy come, easy go.
If you can’t beat them, join them.
Life begins at forty.
Two heads are better than one.
EOD;
$str04n = <<<'EOT'
In PHP there are TWO types
of variables:
INTERNAL, (predefined).
OF USER: (user-defined).
EOT;
print ($xyz . '<br><br>' . $str0 .
'<br><br>' . $carÇ .
'<br><br>' . $_alo . '<br><br>');
print ($rquo . '<br><br>' . $str01 .
'<br><br>' . $str02 . '<br><br>');
print ($str03h . '<br><br>' . $str04h . '<br><br>');
print ($str03n . '<br><br>' . $str04n);
?>
RESULT
The same result as the previous exercise.
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - -
SOME VALID CONSTANT EXAMPLES
STRINGS: INTERNAL
- - - - - - - - - - - - - - - - - - - - - - - - - */
print PHP_VERSION;
// GETS THE ACTUAL PHP VERSION
print PHP_EOL;
// SPECIFIES THE END OF LINE
print (PHP_DATADIR);
// SPECIFIES THE DATA DIRECTORY
?>
RESULT
- - - - - - - - - - - - - - - - - - - - - - - - -
7.4.11
- - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
H:\php
- - - - - - - - - - - - - - - - - - - - - - - - -
Your results for this exercise may be different.
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SOME VALID CONSTANT EXAMPLES
STRINGS: USER-DEFINED
To establish a CONSTANT USER-DEFINED value
is necessary, before, to use de define statement.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
define('KONST01', 'Haste makes waste. ', FALSE);
// FALSE as default KONST01 ≠ konst01
// VERSIONS earlier than 8.0
print KONST01 . '<br><br>';
define('KONST02', 'Haste makes waste. ', TRUE);
// TRUE as default KONST02 = konst02
// VERSIONS earlier than 8.0
// Warning: define(): Argument #3
// ($case_insensitive) is ignored
// since declaration of case-insensitive constants
// is no longer supported
// As of version 8.0
print KONST02 . '<br><br>' . konst02;
// Fatal error: Uncaught Error:
// Undefined constant "konst02"
// As of version 8.0
print KONST03;
// Warning: Use of undefined constant KONST03
// In all versions
?>
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - -
SOME VALID VARIABLE EXAMPLES
NUMBERS: INTERNAL
- - - - - - - - - - - - - - - - - - - - - */
print ($_SERVER['REQUEST_TIME']);
print '<br><br>' . ($_SERVER['REQUEST_TIME_FLOAT']);
?>
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SOME VALID VARIABLE EXAMPLES
NUMBERS: USER-DEFINED
All VARIABLES after the value assignment
must end with a SEMICOLON
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
// INTEGER
$x01p = 220;
$x01n = -220;
print '$x01p = ' . ($x01p) . '<br>$x01n = ' . ($x01n) . '<br><br>';
// FLOAT
$x02p = 123.456;
$x02n = -123.456;
print ('$x02p = ' . $x02p . '<br>$x02n = ' . $x02n . '<br><br>');
// CIENTIFIC NOTATION
$x03p = 1E19;
$x04p = 1.6e-19;
print ('$x03p = ') . ($x03p) . ('<br>$x04p = ') . ($x04p) . ('<br><br>');
// HEXADECIMAL NOTATION ->
// (20b)base 16 = (20B)base 16 = (523)base 10
$x05p = 0x20b;
$x06p = 0X20B;
print '$x05p = ' . $x05p . '<br>$x06p = ' . $x06p . '<br><br>';
// OCTAL NOTATION -> (1023)base 8 = (531)base 10
$x07p = 01023;
print '$x07p = ' . $x07p . '<br><br>';
// BINARY NOTATION -> (0011)base 2 = (3)base 10
$x08p = 0b0011;
// Available since PHP 5.4.0
print '$x08p = ' . $x08p . '<br>';
?>
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - -
SOME VALID CONSTANT EXAMPLES
NUMBERS: INTERNAL
- - - - - - - - - - - - - - - - - - - - - - */
M_E;
// VALUE of e
M_PI;
// VALUE of PI
M_SQRT2;
// SQUARE ROOT of 2
print (M_E . '<br><br>' .
M_PI . '<br><br>' .
M_SQRT2 . '<br><br>');
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SOME VALID CONSTANT EXAMPLES
NUMBERS: USER-DEFINED
All CONSTANT after the value assignment
must end with a SEMICOLON.
To establish a CONSTANT USER-DEFINED value
is necessary, before, to use de define statement.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
// DECIMAL NOTATION
define('KONST03', 123456789);
print 'KONST03 = ' . KONST03 . '<br><br>';
// OCTAL NOTATION ->
// (1023)base 8 = (531)base 10
define('KONST04', 0123);
print 'KONST04 = ' . KONST04 . '<br><br>';
// HEXADECIMAL NOTATION ->
// (20b)base 16 = (20B)base 16 = (523)base 10
define('KONST05', 0X20B);
print 'KONST05 = ' . KONST05 . '<br><br>';
// BINARY NOTATION ->
// (0011)base 2 = (3)base 10
define('KONST06', 0b0011, TRUE);
// Available since PHP 5.4.0
print 'KONST06 = ' . KONST06 . '<br>';
print 'konst06 = ' . konst06 . '<br><br>';
/* - - - - - - - - - - - - - - - - - - - - - - - -
SOME VALID CONSTANT EXAMPLES
NUMBERS: INTERNAL
- - - - - - - - - - - - - - - - - - - - - - - - */
// INTEGER
print "Smallest INTEGER number = " .
PHP_INT_MIN . '<br><br><br>';
// -2147483648(32 bit)
// or
// -9223372036854775808(64 bit)
// Available since PHP 7.0.0
print "Largest INTEGER number = " .
PHP_INT_MAX . '<br><br>';
// 2147483647(32 bit)
// or
// 9223372036854775807(64 bit)
// Available since PHP 5.0.5
print PHP_MINOR_VERSION . '<br><br>';
// Current minor version of PHP
print PHP_MAJOR_VERSION . '<br><br><br>';
// Current major version of PHP
// Available since PHP 5.2.7
// FLOAT
print "Smallest FLOATING POINT number = " .
PHP_FLOAT_MIN . '<br><br>';
// Smallest floating point number
print "Largest floating point number = " .
PHP_FLOAT_MAX . '<br><br>';
// Largest floating point number
// Available since PHP 7.2.0
/* - - - - - - - - - - - - - - - - - - - - - - - - - -
Generally, Boolean data
is used in routines which
involve decision making
We will see how, in the
near future in this tutorial
- - - - - - - - - - - - - - - - - - - - - - - - - - */
$tr01 = true;
$TR01 = TRUE;
print '$TR01 = ' . $TR01 . '<br><br>';
$fl01 = false;
$FL01 = FALSE;
define('tr01a', false, true);
print 'tr01a = ' . tr01a;
define('FL01a', FALSE);
?>
Another way to show a CONSTANT value.
If the constant is not defined an E_WARNING level error or NULL is generated.
<?php
mix constant ( str $name )
where,
$name = The name of the CONSTANT to be found the value
The CONSTANT name must be delimited by
double quotes or single quotes like STRING notation
?>
$name
The name of the CONSTANT to print the value.
EXERCISE
<?php
define('KONST03', 123456789);
define('KONST06', 'constant');
print 'KONST03 = ';
print constant('KONST03') . '<br>';
print 'KONST06 = ';
print constant('KONST06') . '<br><br>';
print "Smallest INTEGER number = ";
print constant ('PHP_INT_MIN') . '<br>';
// -2147483648(32 bit) or -9223372036854775808(64 bit)
// Available since PHP 7.0.0
print "Largest INTEGER number = ";
print constant ('PHP_INT_MAX') . '<br><br>';
// 2147483647(32 bit) or 9223372036854775807(64 bit)
// Available since PHP 5.0.5
print "Smallest FLOATING POINT number = ";
print constant ('PHP_FLOAT_MIN') . '<br>';
// Smallest floating point number
print "Largest floating point number = ";
print constant ('PHP_FLOAT_MAX') . '<br><br>';
// Largest floating point number
// Available since PHP 7.2.0
print "Square root of 2 = ";
print constant('M_SQRT2') . '<br><br>';
?>
EXERCISE
<?php
echo "Testing print() : basic functionality.";
echo "<br><br>Iteration 1<br>";
print("Hello World");
echo "<br><br>Iteration 2<br>";
print "print() also works without parentheses.";
echo "<br><br>Iteration 3<br>";
print "This spans
multiple lines. The newlines will be
output as well";
echo "<br><br>Iteration 4<br>";
print "This also spans\nmultiple lines.
The newlines will be\noutput as well.";
echo "<br><br>Iteration 5<br>";
print "escaping characters is done \"Like this\".";
// You can use variables inside of a print statement
$foo = "foobar";
$bar = "barbaz";
echo "<br><br>Iteration 6<br>";
print "foo is $foo"; // foo is foobar
// You can also use arrays
$bar = array("value" => "foo");
echo "<br><br>Iteration 7<br>";
print "this is {$bar['value']} !"; // this is foo !
// Using single quotes will print
// the variable name, not the value
echo "<br><br>Iteration 8<br>";
print 'foo is $foo'; // foo is $foo
// If you are not using any other characters,
// you can just print variables
echo "<br><br>Iteration 9<br>";
print $foo; // foobar
echo "<br><br>Iteration 10<br>";
$variable = "VARIABLE";
print <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon no extra whitespace!\n
END;
?>