{"id":122,"date":"2011-12-24T14:42:36","date_gmt":"2011-12-24T14:42:36","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=122"},"modified":"2026-01-26T12:25:43","modified_gmt":"2026-01-26T12:25:43","slug":"working-with-strings","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/strings\/working-with-strings","title":{"rendered":".Using Strings-Basics"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Escape Characters<\/h4>\n\n\n\n<p>In both types of string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  '  needs to be  \\'<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>  \\  needs to be  \\\\<\/code><\/pre>\n\n\n\n<p>In &#8221; &#8221; strings:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  \"  needs to be  \\\"<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>  \\t    tab<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>  \\n    new line<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>  \\r    return<\/code><\/pre>\n\n\n\n<p><em>Note, using say &#8216;\\n&#8217; will not create a newline character, it will create the characters \\n !!!!!  \\n only works in a &#8220;&#8221; string!!<\/em><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Joining strings<\/h4>\n\n\n\n<p>Use the period character &#8216;.&#8217;<\/p>\n\n\n\n<p>You can also use .=<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    $body = \"Some\" . \" text\";\n    $body .= \"Some more text\";<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Is String<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  if (\n    isset($MyString) &amp;&amp;\n    is_string($MyString) &amp;&amp;\n    strlen($MyString) > 0\n  )\n  {\n  }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Is String Empty<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\t$my_string = trim($my_string);\n\tif (!empty($my_string))\n\t\t\/\/Do something<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Length<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  $length = strlen($MyString);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u2018 vs \u201c<\/h4>\n\n\n\n<p>\u2018 single quotes assigns a string using its exact contents. So if you want to include variables you have to do this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   echo 'His name is ' . $name . ' and his age is' . $age';<\/code><\/pre>\n\n\n\n<p>\u201d double quotes assigns a string but if the string contains a variable name the php parser will attempt to evaluate it, so you can use it like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   echo \"His name is $name and his age is $age\";<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Variables and arrray values in a string<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code> echo \"His name is $name\";\n echo \"His name is ${name}y\";  \/\/&lt;&lt;Curley braces can be used if you need to force PHP to see a variable name within other characters\n echo \"His name is ${MyArray&#91;'name']}\";   \/\/&lt;&lt;Also allows array values to be included<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Echo A Block Of Text<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"Some\n\ntext\n\nblah blah\";<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Abbreviated way of inserting an echo in HTML<\/h5>\n\n\n\n<p>\u201c&lt;?=\u201d can be used in place of \u201c&lt;?php echo\u201d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?=\"&lt;p&gt;Hello world&lt;\/p&gt;\";?&gt;\n\nIs the same as:\n&lt;?php echo \"&lt;p&gt;Hello world&lt;\/p&gt;\";?&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Heredoc string<\/h4>\n\n\n\n<p><a href=\"https:\/\/ibex.tech\/cloud\/php\/strings\/heredoc-string\">See here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Escape Characters In both types of string: In &#8221; &#8221; strings: Note, using say &#8216;\\n&#8217; will not create a newline character, it will create the characters \\n !!!!! \\n only works in a &#8220;&#8221; string!! Joining strings Use the period character &#8216;.&#8217; You can also use .= Is String Is String Empty Length \u2018 vs [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-122","post","type-post","status-publish","format-standard","hentry","category-strings"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":52,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"predecessor-version":[{"id":5266,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/122\/revisions\/5266"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}