1$number = 11;
2// This
3echo strval($number);
4// Or This
5echo (String) $number;
6// Output
7// "11"
8// "11"
1$var = 5;
2
3// Inline variable parsing
4echo "I'd like {$var} waffles"; // = "I'd like 5 waffles
5
6// String concatenation
7echo "I'd like ".$var." waffles"; // I'd like 5 waffles
8
9// Explicit cast
10$items = (string)$var; // $items === "5";
11
12// Function call
13$items = strval($var); // $items === "5";
14
1phpCopy<?php
2$variable = 10;
3$string1 = strval($variable);
4echo "The variable is converted to a string and its value is $string1.";
5?>
6
1<?php
2class StrValTest
3{
4 public function __toString()
5 {
6 return __CLASS__;
7 }
8}
9
10// Prints 'StrValTest'
11echo strval(new StrValTest);
12?>
1phpCopy<?php
2$variable = 10;
3$string1 = (string)$variable;
4echo "The variable is converted to a string and its value is $string1.";
5?>
6