strval


php128 apg

GETS the STRING value of a VARIABLE.



<?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.

?>

$var

Variable to be converted to STRING.

A STRING literal can be specified in four different ways:

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


This function returns a STRING value when it tests the following variable types:

STRING, NUMERIC, BOOLEAN, ARRAY, NULL, RESOURCE or OBJECT.

A STRING, as we know, is any set of characters, each character occupying 1 byte.

This means that PHP does not offer native UNICODE support.

Since PHP 7.0.0, there are no particular restrictions regarding the length of a string on 64-bit builds.

On 32-bit builds and in earlier versions, a string can be as large as up to 2GB, (2147483647 bytes maximum).

If the STRING is delimited by DOUBLE QUOTES, PHP will interpret the following escape sequences as special characters:

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

This function performs no formatting on the returned value.

If you are looking for a way to format a NUMERIC value as a STRING should wait for the moment when we will study other more appropriate functions.



  1 EXERCISE   

<?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 "&nbsp;&nbsp;&raquo;&nbsp;&nbsp;"
  
$str01 "1AnVXY234";  
  
$str02 "1234567";

// HEREDOC
$str03h = <<<EOD
Easy come, easy go.\n\r<br>
If you can’t beat them, join them.\n\r<br>
Life begins at forty.\n\r<br>
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:<br>
INTERNAL, (predefined)<br>
and<br>
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>';

?> 

 RESULT   

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)
and
OF USER: (user-defined).


  2 EXERCISE   

<?php

$vv02a 
= [ 'c' => 299792458'G' => 6.67428E-11 ]; 

var_dump(strval($vv02a));
echo 
'<br><br>';

$vv02b = [ 2818323218];

var_dump(strval($vv02b));
echo 
'<br><br>';

$vv02c $_SERVER['HTTP_ACCEPT_LANGUAGE'];

var_dump(strval($vv02c));
echo 
'<br><br>';

$vv02d M_E;

var_dump(strval($vv02d));
echo 
'<br><br>';

$vv02e 1.6e-19

var_dump(strval($vv02e));
echo 
'<br><br>';

$vv02f null;

var_dump(strval($vv02f));
echo 
'<br><br>';

$vv02g FALSE

var_dump(strval($vv02g));

?>


 RESULT   

PHP 7.4.XX

Notice: ...

Notice: ...


string(35) "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3"

string(14) "2.718281828459"

string(7) "1.6E-19"

string(0) ""

string(0) ""




PHP 8.0.XX

Warning: ...

Warning: ...


string(35) "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3"

string(14) "2.718281828459"

string(7) "1.6E-19"

string(0) ""

string(0) ""