w3resource
php Programming Exercies

PHP String Exercises: Convert the value of a PHP variable to string

PHP String: Exercise-4 with Solution

Write a PHP script to convert the value of a PHP variable to string.

Pictorial Presentation:

PHP String Exercises: Convert the value of a PHP variable to string

Sample Solution:

PHP Code:

<?php
$x =  20;            // $x is an integer
$str1 = (string)$x; // $str1 is a string now
// Check whether $x and $str1 are equal or not
if ($x === $str1) 
{
  echo "They are the same"."\n";
}
else
{
echo "They are not same"."\n";
}
?>

Sample Output:

They are not same

Flowchart :

Flowchart: Convert the value of a PHP variable to string

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP script to check if a string contains a specific string?
Next: Write a PHP script to extract the file name from the specific string.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming