<?php
str strval ( mix $var );
where,
$var = Variable to be converted to STRING
$var may be any scalar type or
an object that implements the __toString method.
Cannot be used on arrays or on objects
that do not implement the __toString method.
The __toString method will be studied later.
?>
TYPE | STRING |
SINGLE QUOTED | ' STRING ' |
DOUBLE QUOTED | " STRING " |
HEREDOC SYNTAX | <<<EOT STRING of multiple lines EOT; |
NEWDOC SYNTAX | <<<'EOD' STRING of multiple lines EOD;" |
ed48 |
ESCAPED CHARACTER | MEANING |
\n | Linefeed (LF or 0x0A (10) in ASCII) |
\r | Carriage return (CR or 0x0D (13) in ASCII) |
\t | Horizontal tab (HT or 0x09 (9) in ASCII) |
\v | Vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5) |
\e | Escape (ESC or 0x1B (27) in ASCII) (since PHP 5.4.4) |
\f | Form feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5) |
\\ | BACKSLASH |
\$ | DOLLAR SIGN |
\' | SINGLE-QUOTE |
\" | DOUBLE-QUOTE |
\[0-7]{1,3} | The sequence of characters matching the regular expression is a character in octal notation, which silently overflows to fit in a byte (e.g. "\400" === "\000") |
\x[0-9A-Fa-f]{1,2} | The sequence of characters matching the regular expression is a character in hexadecimal notation |
\u{[0-9A-Fa-f]+} | The sequence of characters matching the regular expression is a Unicode codepoint, which will be output to the string as that codepoint's UTF-8 representation (added in PHP 7.0.0) |
ed48 |
<?php
$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;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
echo strval($carÇ) . '<br><br>' .
strval($_alo) . '<br><br>' .
strval($rquo) . '<br><br>' .
strval($str01) . '<br><br>' .
strval($str02) . '<br><br>' .
strval($str03h) . '<br><br>' .
strval($str04n) . '<br><br>';
?>
<?php
function sstrval ($xvar)
{
if(strval($xvar))
{
var_dump($xvar);
echo '<br>Conversion to STRING is possible<br><br>';
}
else
{
var_dump($xvar);
echo '<br>Conversion to STRING is NOT possible<br><br>';
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$vv01 = $_SERVER['REMOTE_ADDR'];
$vv02 = $_SERVER['HTTP_USER_AGENT'];
$vv03 = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$vv04 = M_E;
$vv05 = 1.6e-19;
$vv06 = [ 'c' => 299792458, 'G' => 6.67428E-11 ];
$vv07 = NULL;
$vv08 = true;
$vv09 = FALSE;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sstrval($vv02);
sstrval($vv04);
sstrval($vv05);
@sstrval($vv06);
sstrval($vv07);
sstrval($vv08);
sstrval($vv09);
?>
<?php
function sstrval ($xvar)
{
if(strval($xvar))
{
var_dump($xvar);
echo '<br>Conversion to STRING is possible<br><br>';
}
else
{
var_dump($xvar);
echo '<br>Conversion to STRING is NOT possible<br><br>';
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$vv06a = [ 'c' => 299792458, 'G' => 6.67428E-11 ];
$vv07a = [ 2, 8, 18, 32, 32, 18, 8 ];
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sstrval($vv06a);
sstrval($vv07a);
?>
<?php
function ssstrval ($xvar)
{
if(strval($xvar))
{
var_dump($xvar);
echo '<br>Conversion to STRING is possible<br><br>';
}
else
{
var_dump($xvar);
echo '<br>Conversion to STRING is NOT possible<br><br>';
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$vv06b = [ 'c' => 299792458, 'G' => 6.67428E-11 ];
$vv07b = [ 2, 8, 18, 32, 32, 18, 8 ];
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
echo '- - - - - - - - - - - - -<br><br>';
foreach($vv06b as $k06b => $v06b)
{
ssstrval($k06b);
}
echo '- - - - - - - - - - - - -<br><br>';
foreach($vv06b as $k06b => $v06b)
{
ssstrval($v06b);
}
echo '- - - - - - - - - - - - -
<br>- - - - - - - - - - - - -<br><br>';
foreach($vv07b as $k07b)
{
ssstrval($k07b);
}
echo '- - - - - - - - - - - - -<br><br>';
?>
<?php
echo "Testing strval() : usage variations.<br>";
//get an unset variable
$unset_var = 10;
unset ($unset_var);
//getting the resource
$file_handle = fopen(__FILE__, "r");
class MyClass
{
function __toString() {
return "MyClass";
}
}
//array of values to iterate over
$values = array(
//Decimal values
/*1*/ 0,
1,
12345,
-12345,
//Octal values
/*5*/ 02,
010,
030071,
-030071,
//Hexadecimal values
/*9*/ 0x0,
0x1,
0xABCD,
-0xABCD,
// float data
/*13*/ 100.5,
-100.5,
100.1234567e10,
100.7654321E-10,
.5,
// array data
/*18*/ array(),
array('color' => 'red', 'item' => 'pen'),
// null data
/*20*/ NULL,
null,
// boolean data
/*22*/ true,
false,
TRUE,
FALSE,
// empty data
/*26*/ "",
'',
// object data
/*28*/ new MyClass(),
// resource
/*29*/ $file_handle,
// undefined data
/*30*/ @$undefined_var,
// unset data
/*31*/ @$unset_var,
);
// loop through each element of the array for strval
$iterator = 1;
foreach($values as $value) {
echo "<br><br>Iteration: $iterator<br>";
var_dump( strval($value) );
$iterator++;
};
?>
<?php
echo "Testing strval() : error conditions<br>";
error_reporting(E_ALL ^ E_NOTICE);
class MyClass
{
// no toString() method defined
}
// Testing strval with a object which has no toString() method
echo "<br>With object which has not toString() method:<br>";
try {
var_dump( strval(new MyClass()) );
} catch (Error $e) {
echo $e->getMessage(), "<br>";
}
?>
<?php
echo "Testing strval() : basic variations<br>";
error_reporting(E_ALL ^ E_NOTICE);
$simple_heredoc =<<<EOT
Simple HEREDOC string
EOT;
//array of values to iterate over
$values = array(
// Simple strings
/*1*/ "Hello World",
'Hello World',
// String with control chars
/*3*/ "String\nwith\ncontrol\ncharacters\r\n",
// String with quotes
/*4*/ "String with \"quotes\"",
//Numeric String
/*5*/ "123456",
// Hexadecimal string
/*6*/ "0xABC",
//Heredoc String
/*7*/ $simple_heredoc
);
// loop through each element of the array for strval
$iterator = 1;
foreach($values as $value) {
echo "<br><br>Iteration: $iterator<br>";
var_dump( strval($value) );
$iterator++;
};
?>
<?php
$foo = 'bar';
var_dump(strval($foo));
echo '<br><br>';
define('FOO', 'BAR');
var_dump(strval(FOO));
echo '<br><br>';
var_dump(strval('foobar'));
echo '<br><br>';
var_dump(strval(1));
echo '<br><br>';
var_dump(strval(1.1));
echo '<br><br>';
var_dump(strval(true));
echo '<br><br>';
var_dump(strval(false));
echo '<br><br>';
var_dump(strval(array('foo')));
?>
<?php
echo "<b><u>Testing str_val() with scalar values:</b></u><br>";
$heredoc_string = <<<EOD
This is a multiline heredoc
string. Numeric = 1232455.
EOD;
/* heredoc string with only numeric values */
$heredoc_numeric_string = <<<EOD
12345
2345
EOD;
/* null heredoc string */
$heredoc_empty_string = <<<EOD
EOD;
/* heredoc string with NULL */
$heredoc_NULL_string = <<<EOD
NULL
EOD;
// different valid scalar values
$scalars = array(
/* integers */
0,
1,
-1,
-2147483648,
// max negative integer value
-2147483647,
2147483647,
// max positive integer value
2147483640,
0x123B,
// integer as hexadecimal
0x12ab,
0Xfff,
0XFA,
/* floats */
-0x80000000,
// max negative integer as hexadecimal
0x7fffffff,
// max positive integer as hexadecimal
0x7FFFFFFF,
// max positive integer as hexadecimal
0123,
// integer as octal
01,
// should be quivalent to octal 1
-020000000000,
// max negative integer as octal
017777777777,
// max positive integer as octal
-2147483649,
// float value
2147483648,
// float value
-0x80000001,
// float value, beyond max negative int
0x800000001,
// float value, beyond max positive int
020000000001,
// float value, beyond max positive int
-020000000001,
// float value, beyond max negative int
0.0,
-0.1,
10.0000000000000000005,
10.5e+5,
1e-5,
.5e+7,
.6e-19,
.05E+44,
.0034E-30,
/* booleans */
true,
TRUE,
FALSE,
false,
/* strings */
"",
'',
" ",
' ',
'0',
"0",
"testing",
"0x564",
"0123",
"new\n",
'new\n',
"@#$$%^&&*()",
" ",
"null",
'null',
'true',
"true",
/*"\0123",
"\0x12FF",*/
$heredoc_string,
$heredoc_numeric_string,
$heredoc_empty_string
);
/* loop to check that strval()
recognizes different scalar values
and returns the string conversion of same */
$loop_counter = 1;
foreach ($scalars as $scalar ) {
echo "<br><br>Iteration: $loop_counter<br>";
$loop_counter++;
var_dump( strval($scalar) );
}
echo "<br><br><br<br><b><u>Testing str_val() with non_scalar values:</b></u>";
// get a resource type variable
$fp = fopen(__FILE__, "r");
$dfp = opendir( __DIR__ );
// unset variable
$unset_var = 10;
unset ($unset_var);
// non_scalar values, objects, arrays,
// resources and boolean
class foo
{
function __toString() {
return "Object";
}
}
$not_scalars = array (
new foo,
//object
$fp,
// resource
$dfp,
array(),
// arrays
array(NULL),
array(1,2,3,4),
array("string"),
NULL,
// nulls
null,
@$unset_var,
// unset variable
@$undefined_var
);
/* loop through the $not_scalars to see working of
strval() on objects, arrays, boolean and others */
$loop_counter = 1;
foreach ($not_scalars as $value ) {
echo "<br><br>Iteration: $loop_counter<br>";
$loop_counter++;
var_dump( strval($value) );
}
// close the resources used
fclose($fp);
closedir($dfp);
?>