{"id":431,"date":"2018-08-08T00:41:51","date_gmt":"2018-08-08T00:41:51","guid":{"rendered":"https:\/\/www.go4expert.com\/tutorials\/?p=431"},"modified":"2018-08-14T00:46:33","modified_gmt":"2018-08-14T00:46:33","slug":"php-strings","status":"publish","type":"post","link":"https:\/\/www.go4expert.com\/tutorials\/php-strings\/","title":{"rendered":"PHP Strings"},"content":{"rendered":"<p>A string is a sequence of characters which are enclosed by quotes. Strings consist of characters which may be letters, numbers or special characters. A string is either enclosed by single quotation marks (\u2018) or double quotation marks(\u201c) because the parser needs a method to distinguish the text of the string from the rest of the program. Depending on the type of quotation mark used a string is processed differently.<\/p>\n<h2>Double quotations vs single quotations for strings<\/h2>\n<p>A string within single quotes is treated as a literal. Hence no more processing is done on the string and its value is stored as is.<\/p>\n<p>However, a string within double quotes always gets processed by the pre-processor first. Hence strings within double quotes will be evaluated and will get replaced by the value after pre-processing and special character sequences will get interpreted differently. Therefore a string within double quotes will be evaluated as follows:<\/p>\n<ol>\n<li>Variable names which are a part of the string will get replaced by their value. Hence the value of the variable will be inserted as a string in the main string<\/li>\n<li>Character sequences which begin with a backslash (escape characters)\u00a0 will be identified and\u00a0 then replaced by special characters corresponding to the character sequence.<\/li>\n<\/ol>\n<p>Hence escape-sequences will be replaced by special characters as follows:<\/p>\n<ol>\n<li>\\n is replaced by the newline character<\/li>\n<li>\\r is replaced by the carriage-return character<\/li>\n<li>\\t is replaced by the tab character<\/li>\n<li>\\$ is replaced by the dollar sign itself ($)<\/li>\n<li>\\\" is replaced by a single double-quote (\")<\/li>\n<li>\\\\ is replaced by a single backslash (\\)<\/li>\n<\/ol>\n<h2>String Concatenation Operator<\/h2>\n<p>The dot (.) operator is called as the string concatenation character. It is used between 2 strings when we need to concatenate or join the two strings together.<\/p>\n<p><b>Example:<\/b><\/p>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\n$string1=\"Learn PHP\";\n$string2=\"it is easy.\";\necho $string1 . \" because \" . $string2;\n\/\/ The output is \"Learn PHP because it is easy\"\n\/\/ The concatenation operator is used twice because 3 strings are concatenated.\n?&gt;<\/textarea><\/div>\n<p>In the above example, 3 strings are concatenated by using the concatenation operator twice.<\/p>\n<h2>PHP String functions<\/h2>\n<p>PHP provides us with a comprehensive set of inbuilt functions which perform a range of operations on strings. These functions are very useful to programmers because strings often need to be extensively manipulated in PHP scripts for a variety of reasons. Some of the common string manipulation functions are as follows :<\/p>\n<h3>Strlen()<\/h3>\n<p>The strlen() function is used to find the length of a string. It accepts a string, for which the length needs to be found, as the argument and then returns the length of the string. If the string is empty then it returns\u00a0 a 0.<\/p>\n<p><b>Syntax:<\/b><br \/>\nstrlen($string_name);<\/p>\n<p><b>Example:<\/b><\/p>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\necho strlen(\"Learn PHP because it is the best!\");\n\/\/ outputs 33 because the length of the string or the number of characters in the above string is 33\n?&gt;<\/textarea><\/div>\n<h3>Str_word_count()<\/h3>\n<p>The str_word_count() function provides us with the number of words in a string. This is different from the strlen() function because the strlen() function counts characters including spaces whereas the str_word_count() counts the number of words in the string.<\/p>\n<p><b>Example: <\/b><\/p>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\necho str_word_count(\"Learn PHP because it is fun\");\n\/\/ The output is 6 because there are 6 words in the string\n?&gt;<\/textarea><\/div>\n<h3>Strrev()<\/h3>\n<p>The strrev() function accepts a string as an argument and then returns the reverse of the string. In short it reverses a string .<\/p>\n<p><b>Example: <\/b><br \/>\n&lt;?php<br \/>\necho strrev(\"Learn PHP!\");<br \/>\n\/\/The output will be !PHP nraeL because the string 'Learn PHP!' gets reversed.<br \/>\n?&gt;<\/p>\n<h3>Strpos() function<\/h3>\n<p>The strpos() function is used to find out if a substring exists within a string and then to get the exact position where the substring starts .<\/p>\n<p>The function accepts 2 arguments, the string and the substring which we are trying to locate. If the substring is found then the strpos() function will return the exact position of the first occurrence of the substring within the string. If the substring is not found in the string then the strpos() function returns a FALSE.<\/p>\n<p><b>Syntax: <\/b><br \/>\nThe syntax of the strpos function is as follows :<br \/>\nStrpos($string, $substring);<br \/>\nwhere $ string is the string in which we are trying to locate a substring and $substring is the substring<\/p>\n<p><b>Example:<\/b><\/p>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\necho strpos(\"Learn PHP because it is easy!\", \"because\");\n\/\/ outputs 10 because the first index position starts at 10.\n?&gt;<\/textarea><\/div>\n<h3>Str_replace() function<\/h3>\n<p>The str_replace function will replace a substring within a string with a user-defined string.<\/p>\n<p><b>Syntax:<\/b><br \/>\nThe syntax of the str_replace() function is as follows :<br \/>\nstr_replace ($substring, $string_replace , $string);<br \/>\nwhere $substring is the substring within the main string which needs to be replaced. $string_replace is the string which needs to be inserted in place of $substring. $string is the main string on which operations need to be performed.<\/p>\n<p><b>Example:<\/b><br \/>\n<div class=\"bbCodeBlock\"><div class=\"type\">Code: <\/div><textarea readonly=\"readonly\">&lt;?php\necho str_replace(\"Please learn PHP\", \"PHP is fun\", \"Please learn PHP because it is easy\");\n\/\/ outputs \"PHP is fun because it is easy\" because the first string \"Please learn PHP\" gets replaced by \"PHP is fun\".\n?&gt;<\/textarea><\/div><\/p>\n<p>PHP consists several other prebuilt string processing functions. More detailed documentation about the string functions, usage and functions can be found <a href=\"http:\/\/in.php.net\/manual\/en\/ref.strings.php\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A string is a sequence of characters which are enclosed by quotes. Strings consist of characters which may be letters, numbers or special characters.<\/p>\n","protected":false},"author":6,"featured_media":210,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[7],"tags":[],"class_list":{"0":"post-431","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-php","8":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP Strings - Go4Expert Tutorials<\/title>\n<meta name=\"description\" content=\"A string is a sequence of characters which are enclosed by quotes. Strings consist of characters which may be letters, numbers or special characters.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.go4expert.com\/tutorials\/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 - Go4Expert Tutorials\" \/>\n<meta property=\"og:description\" content=\"A string is a sequence of characters which are enclosed by quotes. Strings consist of characters which may be letters, numbers or special characters.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.go4expert.com\/tutorials\/php-strings\/\" \/>\n<meta property=\"og:site_name\" content=\"Go4Expert Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2018-08-08T00:41:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-08-14T00:46:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"770\" \/>\n\t<meta property=\"og:image:height\" content=\"385\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"vnlaks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"vnlaks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/php-strings\/\",\"url\":\"https:\/\/www.go4expert.com\/tutorials\/php-strings\/\",\"name\":\"PHP Strings - Go4Expert Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#website\"},\"datePublished\":\"2018-08-08T00:41:51+00:00\",\"dateModified\":\"2018-08-14T00:46:33+00:00\",\"author\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/7f62b1e7d9b60341013fb7bd42ca5aae\"},\"description\":\"A string is a sequence of characters which are enclosed by quotes. Strings consist of characters which may be letters, numbers or special characters.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/php-strings\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.go4expert.com\/tutorials\/php-strings\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/php-strings\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.go4expert.com\/tutorials\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP Strings\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#website\",\"url\":\"https:\/\/www.go4expert.com\/tutorials\/\",\"name\":\"Go4Expert Tutorials\",\"description\":\"Programming And Web Development Tutorials\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.go4expert.com\/tutorials\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/7f62b1e7d9b60341013fb7bd42ca5aae\",\"name\":\"vnlaks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d1b367cbd6665aad2f29e76d2ab4f94adb74ab0f6e2422cd2c75f8645c59f903?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d1b367cbd6665aad2f29e76d2ab4f94adb74ab0f6e2422cd2c75f8645c59f903?s=96&d=mm&r=g\",\"caption\":\"vnlaks\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP Strings - Go4Expert Tutorials","description":"A string is a sequence of characters which are enclosed by quotes. Strings consist of characters which may be letters, numbers or special characters.","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:\/\/www.go4expert.com\/tutorials\/php-strings\/","og_locale":"en_US","og_type":"article","og_title":"PHP Strings - Go4Expert Tutorials","og_description":"A string is a sequence of characters which are enclosed by quotes. Strings consist of characters which may be letters, numbers or special characters.","og_url":"https:\/\/www.go4expert.com\/tutorials\/php-strings\/","og_site_name":"Go4Expert Tutorials","article_published_time":"2018-08-08T00:41:51+00:00","article_modified_time":"2018-08-14T00:46:33+00:00","og_image":[{"width":770,"height":385,"url":"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg","type":"image\/jpeg"}],"author":"vnlaks","twitter_card":"summary_large_image","twitter_misc":{"Written by":"vnlaks","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.go4expert.com\/tutorials\/php-strings\/","url":"https:\/\/www.go4expert.com\/tutorials\/php-strings\/","name":"PHP Strings - Go4Expert Tutorials","isPartOf":{"@id":"https:\/\/www.go4expert.com\/tutorials\/#website"},"datePublished":"2018-08-08T00:41:51+00:00","dateModified":"2018-08-14T00:46:33+00:00","author":{"@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/7f62b1e7d9b60341013fb7bd42ca5aae"},"description":"A string is a sequence of characters which are enclosed by quotes. Strings consist of characters which may be letters, numbers or special characters.","breadcrumb":{"@id":"https:\/\/www.go4expert.com\/tutorials\/php-strings\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.go4expert.com\/tutorials\/php-strings\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.go4expert.com\/tutorials\/php-strings\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.go4expert.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"PHP Strings"}]},{"@type":"WebSite","@id":"https:\/\/www.go4expert.com\/tutorials\/#website","url":"https:\/\/www.go4expert.com\/tutorials\/","name":"Go4Expert Tutorials","description":"Programming And Web Development Tutorials","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.go4expert.com\/tutorials\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/7f62b1e7d9b60341013fb7bd42ca5aae","name":"vnlaks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d1b367cbd6665aad2f29e76d2ab4f94adb74ab0f6e2422cd2c75f8645c59f903?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d1b367cbd6665aad2f29e76d2ab4f94adb74ab0f6e2422cd2c75f8645c59f903?s=96&d=mm&r=g","caption":"vnlaks"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.go4expert.com\/tutorials\/wp-content\/uploads\/2018\/07\/PHP.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9JSSi-6X","_links":{"self":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/comments?post=431"}],"version-history":[{"count":14,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/431\/revisions"}],"predecessor-version":[{"id":482,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/431\/revisions\/482"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/media\/210"}],"wp:attachment":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/media?parent=431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/categories?post=431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/tags?post=431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}