{"id":46,"date":"2020-12-09T16:30:27","date_gmt":"2020-12-09T16:30:27","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/php\/?p=46"},"modified":"2026-10-29T01:27:17","modified_gmt":"2026-10-29T01:27:17","slug":"php-basic-syntax","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/php\/php-basic-syntax\/","title":{"rendered":"PHP &#8211; Basic Syntax"},"content":{"rendered":"<p>In this tutorial, we would learn the basic syntax of a PHP program.<\/p>\n<ul>\n<li><a href=\"#t1\">Escaping in PHP Code Block<\/a><\/li>\n<li><a href=\"#t2\">Comments in a PHP Code<\/a><\/li>\n<li><a href=\"#t3\">Case Sensitivity in PHP Code<\/a><\/li>\n<li><a href=\"#t4\">Statements, Expressions and Blocks<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Escaping PHP Code Block<\/strong><\/h4>\n<p>Since PHP can be embedded into a HTML page, we need a way to separate he PHP code from other page elements.\u00a0 You achieve this using a method called &#8216;escaping to PHP&#8217;.\u00a0 This can be achieved using four methods:<\/p>\n<p><strong>Canonical PHP Tags<\/strong><\/p>\n<p>This is the most common PHP tag as shown below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span>\r\n  <span style=\"color: #333333;\">....<\/span>\r\n  <span style=\"color: #333333;\">....<\/span>\r\n<span style=\"color: #333333;\">&lt;?<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Short-Open Tags<\/strong><\/p>\n<p>This is simply a short form of the canonical tag and is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?<\/span>\r\n  <span style=\"color: #333333;\">....<\/span>\r\n<span style=\"color: #333333;\">&lt;?<\/span>\r\n<\/pre>\n<p>To enable PHP to recognize\u00a0 the short-open tags, you can do one of two things:<\/p>\n<ul>\n<li>choose <em>enable-short-tags<\/em> configuration option when building PHP<\/li>\n<li>set the <em>short_open_tag<\/em> setting in the php.ini file<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>ASP-style Tags<\/strong><\/p>\n<p>This is a kind of tag that mimics ASP.net code style. It looks like this:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">&lt;%\r\n  ....\r\n&lt;%\r\n<\/pre>\n<p>Similarly, for this to work, you also need to set this option in your php.ini file<\/p>\n<p>&nbsp;<\/p>\n<p><strong>HTML Script Tags<\/strong><\/p>\n<p>This is similar to the script tags we normally use when embedding JavaScript\u00a0 in HTML pages. It goes this way:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">language =<\/span> <span style=\"background-color: #fff0f0;\">\"PHP\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n    ...\r\n<span style=\"color: #007700;\">&lt;\/script&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Comments in a PHP Code<\/strong><\/h4>\n<p>A comment in programming is a piece of code that is ignored by the compiler or interpreter. It exists only to provide some readability to the program by humans. Comments are not included in the final results.<\/p>\n<p>PHP supports two styles of comment formats:<\/p>\n<p><em>Single-line comments<\/em> &#8211; this style of comment is used for single line remarks. Some examples are given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span>\r\n    <span style=\"color: #888888;\"># An example of single-line comment<\/span>\r\n    <span style=\"color: #888888;\"># With a second line as you can see<\/span>\r\n\r\n    <span style=\"color: #888888;\">\/\/ You can also use this C++ style<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">print<\/span> <span style=\"background-color: #fff0f0;\">'A good example of single-line comments'<\/span>\r\n<span style=\"color: #557799;\">?&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><em>Multi-line printing<\/em> &#8211; in this style, the comments span two or more lines. Se examples below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?<\/span>\r\n   <span style=\"color: #888888;\"># Our 1st Example<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">print<\/span> <span style=\"color: #333333;\">&lt;&lt;&lt;<\/span>END\r\n   <span style=\"color: #008800; font-weight: bold;\">This<\/span> example the <span style=\"background-color: #fff0f0;\">\"here document\"<\/span> syntax to pring\r\n   multiple lines with <span style=\"color: #996633;\">$variable<\/span> interpolation<span style=\"color: #333333;\">.<\/span> \r\n   The here document terminator must appear on a\r\n   line with just a semicolon <span style=\"color: #008800; font-weight: bold;\">and<\/span> extra whitespace<span style=\"color: #333333;\">!<\/span>\r\n   END;\r\n   \r\n   <span style=\"color: #888888;\"># The 2nd Example<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">print<\/span> <span style=\"background-color: #fff0f0;\">\"This spans<\/span>\r\n<span style=\"background-color: #fff0f0;\">   multiple lines terminated with semicolon. <\/span>\r\n<span style=\"background-color: #fff0f0;\">   The newlines will be<\/span>\r\n<span style=\"background-color: #fff0f0;\">   output as well\"<\/span>;\r\n<span style=\"color: #557799;\">?&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><em>Multi-line comments<\/em> &#8211; They are generally used for providing a pseudocodelike explanation to the code. The multi-line comments in PHP is the same as multi-line comments in C and C++. An example is given here<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?<\/span>\r\n   <span style=\"color: #888888;\">\/* <\/span>\r\n<span style=\"color: #888888;\">   This is an C++ type of <\/span>\r\n<span style=\"color: #888888;\">   multiple lines terminated with <\/span>\r\n<span style=\"color: #888888;\">   *\/<\/span>\r\n<span style=\"color: #557799;\">?&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Case-Sensitivity in PHP<\/strong><\/h4>\n<p>First note that PHP is case sensitive similar to Java. This means that <em>$Value<\/em> is not the same as <em>$value<\/em>.<\/p>\n<p>Heres is an example to prove this<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;html&gt;<\/span>\r\n   <span style=\"color: #007700;\">&lt;body&gt;<\/span>\r\n      \r\n      <span style=\"color: #557799;\">&lt;?php<\/span>\r\n         <span style=\"color: #996633;\">$value<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">38<\/span>;\r\n         <span style=\"color: #008800; font-weight: bold;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Variable value is <\/span><span style=\"background-color: #eeeeee;\">$capital<\/span><span style=\"background-color: #fff0f0;\">&lt;br&gt;\"<\/span>);\r\n         <span style=\"color: #008800; font-weight: bold;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Variable Value is <\/span><span style=\"background-color: #eeeeee;\">$CaPiTaL<\/span><span style=\"background-color: #fff0f0;\">&lt;br&gt;\"<\/span>);\r\n      <span style=\"color: #557799;\">?&gt;<\/span>\r\n      \r\n   <span style=\"color: #007700;\">&lt;\/body&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/html&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Handling Whitespaces &#8211;<\/strong> PHP is whitespace insensitive. This means that whitespace characters does not matter to the PHP interpreter. These includes tabs and newlines.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Statements, Expressions and Blocks<\/strong><\/h4>\n<p>Statements is PHP is terminated with a semicolon (;)<\/p>\n<p>Expressions are a combination of tokens (letters, words, symbols etc)<\/p>\n<p>Blocks of code are placed inside curly braces. See example below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">if (10 == 5 + 5)\r\n   print(\"I bet you, this is very correct!.&lt;br&gt;\");\r\n   \r\nif (20 == 10 + 10) {\r\n   print(\"I bet you\");\r\n   print(\"this is very correct&lt;br&gt;\");\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we would learn the basic syntax of a PHP program. Escaping in PHP Code Block Comments in a PHP Code Case Sensitivity &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":[],"class_list":["post-46","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP - Basic Syntax | Kindson The Genius<\/title>\n<meta name=\"description\" content=\"PHP - Basic Syntax \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-basic-syntax\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP - Basic Syntax | Kindson The Genius\" \/>\n<meta property=\"og:description\" content=\"PHP - Basic Syntax \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-basic-syntax\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Tutorial\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-09T16:30:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-29T01:27:17+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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-basic-syntax\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-basic-syntax\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\"},\"headline\":\"PHP &#8211; Basic Syntax\",\"datePublished\":\"2020-12-09T16:30:27+00:00\",\"dateModified\":\"2026-10-29T01:27:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-basic-syntax\\\/\"},\"wordCount\":414,\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-basic-syntax\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-basic-syntax\\\/\",\"name\":\"PHP - Basic Syntax | Kindson The Genius\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#website\"},\"datePublished\":\"2020-12-09T16:30:27+00:00\",\"dateModified\":\"2026-10-29T01:27:17+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\"},\"description\":\"PHP - Basic Syntax \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-basic-syntax\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-basic-syntax\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-basic-syntax\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP &#8211; Basic Syntax\"}]},{\"@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 - Basic Syntax | Kindson The Genius","description":"PHP - Basic Syntax \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-basic-syntax\/","og_locale":"en_US","og_type":"article","og_title":"PHP - Basic Syntax | Kindson The Genius","og_description":"PHP - Basic Syntax \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-basic-syntax\/","og_site_name":"PHP Tutorial","article_published_time":"2020-12-09T16:30:27+00:00","article_modified_time":"2026-10-29T01:27:17+00:00","author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kindsonthegenius.com\/php\/php-basic-syntax\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-basic-syntax\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60"},"headline":"PHP &#8211; Basic Syntax","datePublished":"2020-12-09T16:30:27+00:00","dateModified":"2026-10-29T01:27:17+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-basic-syntax\/"},"wordCount":414,"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/php\/php-basic-syntax\/","url":"https:\/\/kindsonthegenius.com\/php\/php-basic-syntax\/","name":"PHP - Basic Syntax | Kindson The Genius","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/php\/#website"},"datePublished":"2020-12-09T16:30:27+00:00","dateModified":"2026-10-29T01:27:17+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60"},"description":"PHP - Basic Syntax \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-basic-syntax\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/php\/php-basic-syntax\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/php\/php-basic-syntax\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/php\/"},{"@type":"ListItem","position":2,"name":"PHP &#8211; Basic Syntax"}]},{"@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\/46","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=46"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"predecessor-version":[{"id":47,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/46\/revisions\/47"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}