What are the methods to convert PHP to strings?

  1. Wrap the variables with double quotation marks or single quotation marks.
$var = 123;
$str = "This is a string with number $var";
  1. Use the dot operator (.) to concatenate strings and variables.
$var = 123;
$str = "This is a string with number " . $var;
  1. Use the sprintf function.
$var = 123;
$str = sprintf("This is a string with number %d", $var);
  1. Utilize the implode function
$arr = array('Hello', 'World');
$str = implode(' ', $arr);
  1. Utilize the json_encode function
$arr = array('Hello', 'World');
$str = json_encode($arr);
  1. Use the serialize function.
$arr = array('Hello', 'World');
$str = serialize($arr);
Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds