{"id":65,"date":"2020-12-10T22:59:27","date_gmt":"2020-12-10T22:59:27","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/php\/?p=65"},"modified":"2026-10-29T01:28:20","modified_gmt":"2026-10-29T01:28:20","slug":"php-strings","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/php\/php-strings\/","title":{"rendered":"PHP &#8211; Strings"},"content":{"rendered":"<p>In this tutorial, we would cover string manipulation in PHP.<\/p>\n<p>Just like in other languages, string in PHP are a sequence of characters. Some examples of strings are given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">$string1 = \"You can learn PHP\";\r\n$string0 = \"\"; \/\/this is an empty string\r\n$string2 = 'This is a singly quoted string';\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>About singly and double-quoted strings<\/strong><\/p>\n<p>Singly quoted strings are just output literally. However, double quoted strings would replace any containing variables with the actual values of those variables. An example is given below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span>\r\n    <span style=\"color: #996633;\">$name<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Kindson\"<\/span>;\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">echo<\/span> <span style=\"background-color: #fff0f0;\">\"My name is <\/span><span style=\"background-color: #eeeeee;\">$name<\/span><span style=\"background-color: #fff0f0;\"> &lt;br&gt;\"<\/span>;\r\n    <span style=\"color: #008800; font-weight: bold;\">echo<\/span> <span style=\"background-color: #fff0f0;\">'My name is $name'<\/span>;\r\n<span style=\"color: #557799;\">?&gt;<\/span> \r\n<\/pre>\n<p>The result of the above code is shown below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">My name is Kindson\r\nMy name is $name\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Escape Sequences<\/strong><\/p>\n<p>This are special combination of characters used certain operations such as:<\/p>\n<ul class=\"list\">\n<li>\\n represents the newline character<\/li>\n<li>\\r represents the carriage-return character<\/li>\n<li>\\t represents the tab character<\/li>\n<li>\\$ represents the dollar sign itself ($)<\/li>\n<li>\\&#8221; represents a single double-quote (&#8220;)<\/li>\n<li>\\\\ represents a single backslash (\\)<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>String Concatenation Operator<\/strong><\/p>\n<p>The dot (.) operator is used to concatenate two variables.<\/p>\n<p><strong>The strlen() function<\/strong><\/p>\n<p>This function is used to find the length of a string. For example<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">echo<\/span> <span style=\"color: #007020;\">strlen<\/span>(<span style=\"background-color: #fff0f0;\">\"Learning PHP is fun\"<\/span>);\r\n<span style=\"color: #557799;\">?&gt;<\/span>  \r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>The strpos() function<\/strong><\/p>\n<p>This function is used to search for a string or character within a string. If it finds a match, then the position of the first match is found, else, it will return false.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Some useful String functions<\/strong><\/p>\n<p>I have made a list of some other string functions you should know. See the table below:<\/p>\n<table>\n<thead>\n<tr style=\"background-color: #f7f6f3; font-weight: bold;\">\n<td>SN<\/td>\n<td>Name and Description<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td><strong>trim()<\/strong> &#8211; removes whitespaced from the left and right of a string<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td><strong>ltrim()<\/strong> &#8211; removes whitespaces from the beginning of a string<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td><strong>rtrim()<\/strong> &#8211; removes whitespaces from the end of a string<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td><strong>substr(str, start, stop)<\/strong> &#8211; finds a substring from<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td><strong>strncmp()<\/strong> &#8211; compares two strings<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td><strong>explode(int sep, string str)<\/strong> &#8211; splits a string into an array of strings based on a separator<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td><strong>strrev()<\/strong> &#8211; reverses a string<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td><strong>strstr()<\/strong> &#8211; finds the first occurrence of a string within a string<\/td>\n<\/tr>\n<tr>\n<td>9<\/td>\n<td><strong>preg_match(pattern, str)<\/strong> &#8211; finds the first occurrence string pattern in string str.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Complete list of string functions can be found <a href=\"https:\/\/www.php.net\/manual\/en\/function.substr.php\" target=\"_blank\" rel=\"noopener\">here<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we would cover string manipulation in PHP. Just like in other languages, string in PHP are a sequence of characters. Some examples &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[5],"class_list":["post-65","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-strings"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP - Strings | Kindson The Genius<\/title>\n<meta name=\"description\" content=\"PHP - Strings \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius. Free online.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kindsonthegenius.com\/php\/php-strings\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP - Strings | Kindson The Genius\" \/>\n<meta property=\"og:description\" content=\"PHP - Strings \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius. Free online.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/php\/php-strings\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Tutorial\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-10T22:59:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-29T01:28:20+00:00\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-strings\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-strings\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\"},\"headline\":\"PHP &#8211; Strings\",\"datePublished\":\"2020-12-10T22:59:27+00:00\",\"dateModified\":\"2026-10-29T01:28:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-strings\\\/\"},\"wordCount\":315,\"keywords\":[\"Strings\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-strings\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-strings\\\/\",\"name\":\"PHP - Strings | Kindson The Genius\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#website\"},\"datePublished\":\"2020-12-10T22:59:27+00:00\",\"dateModified\":\"2026-10-29T01:28:20+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\"},\"description\":\"PHP - Strings \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius. Free online.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-strings\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-strings\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-strings\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP &#8211; Strings\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/\",\"name\":\"PHP Tutorial\",\"description\":\"Beginner to Expert With Practicals\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/author\\\/kindsonthegenius-3\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP - Strings | Kindson The Genius","description":"PHP - Strings \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius. Free online.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kindsonthegenius.com\/php\/php-strings\/","og_locale":"en_US","og_type":"article","og_title":"PHP - Strings | Kindson The Genius","og_description":"PHP - Strings \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius. Free online.","og_url":"https:\/\/kindsonthegenius.com\/php\/php-strings\/","og_site_name":"PHP Tutorial","article_published_time":"2020-12-10T22:59:27+00:00","article_modified_time":"2026-10-29T01:28:20+00:00","author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kindsonthegenius.com\/php\/php-strings\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-strings\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60"},"headline":"PHP &#8211; Strings","datePublished":"2020-12-10T22:59:27+00:00","dateModified":"2026-10-29T01:28:20+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-strings\/"},"wordCount":315,"keywords":["Strings"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/php\/php-strings\/","url":"https:\/\/kindsonthegenius.com\/php\/php-strings\/","name":"PHP - Strings | Kindson The Genius","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/php\/#website"},"datePublished":"2020-12-10T22:59:27+00:00","dateModified":"2026-10-29T01:28:20+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60"},"description":"PHP - Strings \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius. Free online.","breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-strings\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/php\/php-strings\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/php\/php-strings\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/php\/"},{"@type":"ListItem","position":2,"name":"PHP &#8211; Strings"}]},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/php\/#website","url":"https:\/\/kindsonthegenius.com\/php\/","name":"PHP Tutorial","description":"Beginner to Expert With Practicals","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kindsonthegenius.com\/php\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/kindsonthegenius.com\/php\/author\/kindsonthegenius-3\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/65","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/comments?post=65"}],"version-history":[{"count":3,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"predecessor-version":[{"id":69,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/65\/revisions\/69"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}