{"id":1628,"date":"2017-03-22T14:58:57","date_gmt":"2017-03-22T09:28:57","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1628"},"modified":"2017-03-29T09:49:05","modified_gmt":"2017-03-29T04:19:05","slug":"converting-integer-string-php","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/converting-integer-string-php\/","title":{"rendered":"PHP &#8211; Converting an integer to a string in PHP"},"content":{"rendered":"<ul>\n<li>There are few ways to change the type of a value from integer to string. In the following you\u2019ll find three ways-<\/li>\n<\/ul>\n<h4 id=\"method-1\"><span style=\"color: #000000;\">Method 1: <\/span><\/h4>\n<h4 id=\"applying-type-casting\"><span style=\"color: #800000;\">Applying type casting<\/span><\/h4>\n<p>In this method, to convert an integer to string, write (string) before the integer, and PHP will convert it to string type. See the following example-<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%24intVal%20%3D%201812%3B%0A%24strVal%20%3D%20(string)%24intVal%3B%0A%20%0Avar_dump(%24strVal)%3B%0A%3F%3E%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<h4 id=\"output\"><span style=\"color: #003300;\"><span style=\"color: #000000;\">Output<\/span>:<\/span><\/h4>\n<p>string(4) \u201c1812\u2033<\/p>\n<h4 id=\"how-it-works\"><span style=\"color: #000000;\">How it works:<\/span><\/h4>\n<table width=\"757\">\n<tbody>\n<tr>\n<td width=\"128\"><b>Line 2<\/b><\/td>\n<td width=\"630\"><b>The variable $<\/b><b>intVal<\/b><b> contains the 1812 which is an integer.<\/b><\/td>\n<\/tr>\n<tr>\n<td width=\"128\">Line 3<\/td>\n<td width=\"630\">Here, we cast the type of the variable $intVal to string. And, after that, we store the value to $strVal variable.<\/td>\n<\/tr>\n<tr>\n<td width=\"128\">Line 5<\/td>\n<td width=\"630\">To test whether the type casting has been successful we use var_dump() function which displays type and value of the variable in the output.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"method-2\"><span style=\"color: #000000;\">Method 2:<\/span><\/h4>\n<h4 id=\"using-strval-function\"><span style=\"color: #800080;\">Using strval() function<\/span><\/h4>\n<p>The strval() function will return the string value of a variable. See the following example-<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%24intVal%20%3D%201812%3B%0A%24strVal%20%3D%20strval(%24intVal)%3B%0A%20%0Avar_dump(%24strVal)%3B%0A%3F%3E%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<h4 id=\"output-2\"><span style=\"color: #000000;\">Output:<\/span><\/h4>\n<h5 id=\"string4-1812%e2%80%b3\">string(4) \u201c1812\u2033<\/h5>\n<h4 id=\"how-it-works-2\"><span style=\"color: #000000;\">How it works:<\/span><\/h4>\n<table width=\"757\">\n<tbody>\n<tr>\n<td width=\"128\"><b>Line 2<\/b><\/td>\n<td width=\"630\">The variable $intVal contains the 2566 which is a integer.<b>.<\/b><\/td>\n<\/tr>\n<tr>\n<td width=\"128\">Line 3<\/td>\n<td width=\"630\">The strval() function change the type of the $intVal variable to string, and stores in the $strVal variable.<\/td>\n<\/tr>\n<tr>\n<td width=\"128\">Line 5<\/td>\n<td width=\"630\">Here, we display the type and value of variable $strVal using var_dump() function. It shows in the outout that the variable is now a string type.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"method-3\"><span style=\"color: #000000;\">Method 3:<\/span><\/h4>\n<h4 id=\"surrounding-quotes\"><span style=\"color: #ff6600;\">Surrounding quotes<\/span><\/h4>\n<p>One of the easy ways to convert an integer to string is to add quotes around the variable.<br \/>\nYou can use either single or double quotes. See the following example<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%24intVal%20%3D%201812%3B%0A%24strVal%20%3D%20%22%24intVal%22%3B%0A%20%0Avar_dump(%24strVal)%3B%0A%3F%3E%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<h4 id=\"output-3\">Output:<\/h4>\n<p>string(4) \u201c1812\u2033<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are few ways to change the type of a value from integer to string. In the following you\u2019ll find three ways- Method 1: Applying type casting In this method, to convert an integer to string, write (string) before the integer, and PHP will convert it to string type. See the following example- [pastacode lang=\u201dphp\u201d [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[2176,2134,3367,1128,2975,3066,3366,2171,3368,2170],"class_list":["post-1628","post","type-post","status-publish","format-standard","hentry","category-php","tag-easiest-way-to-convert-int-to-string-in-c","tag-how-do-i-check-if-a-string-contains-a-specific-word-in-php","tag-how-do-i-convert-a-string-into-an-integer-in-javascript","tag-how-to-check-whether-a-string-contains-a-substring-in-javascript","tag-how-to-convert-a-char-to-a-string","tag-how-to-convert-a-string-to-an-int-in-java","tag-how-to-generate-random-integers-within-a-specific-range-in-java","tag-parse-string-to-float-or-int","tag-php-converting-string-to-integer-and-knowing-when-value-is-not-integer-in-string-format","tag-readconvert-an-inputstream-to-a-string"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1628","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1628"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1628\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}