strval


string 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.
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>';

?> 

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


  2 EXERCISE   

<?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);

?>

 RESULT   

* string(115) "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"

* You can get another result for this item.
Conversion to STRING is possible

float(2.718281828459)
Conversion to STRING is possible

float(1.6E-19)
Conversion to STRING is possible

array(2) { ["c"]=> int(299792458) ["G"]=> float(6.67428E-11) }
Conversion to STRING is possible

NULL
Conversion to STRING is NOT possible


bool(true)
Conversion to STRING is possible

bool(false)
Conversion to STRING is NOT possible



  3 EXERCISE   

<?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 = [ 2818323218];
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 
 
sstrval($vv06a);
 
 
sstrval($vv07a);

?>

  4 EXERCISE   

<?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 = [ 2818323218];
 
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

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>';

?>

  5 EXERCISE   

<?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_dumpstrval($value) );
      
$iterator++;
};

?>

  6 EXERCISE   

<?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_dumpstrval(new MyClass()) );
} catch (
Error $e) {
    echo 
$e->getMessage(), "<br>";
}

?>

  7 EXERCISE   

<?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_dumpstrval($value) );
      
$iterator++;
};

?>

  8 EXERCISE   

<?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')));

?>

  9 EXERCISE   

<?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_dumpstrval($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_dumpstrval($value) );
}


// close the resources used
fclose($fp);
closedir($dfp);

?>