{"id":51,"date":"2021-03-07T16:11:22","date_gmt":"2021-03-07T16:11:22","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=51"},"modified":"2025-04-05T13:59:45","modified_gmt":"2025-04-05T13:59:45","slug":"php-syntax","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-syntax\/","title":{"rendered":"PHP Syntax"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn basic PHP syntax, including case sensitivity, statements, and whitespaces.<\/p>\n\n\n\n<p>As a programming language, PHP has a set of rules that govern how you write programs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-code'>PHP code <a href=\"#php-code\" class=\"anchor\" id=\"php-code\" title=\"Anchor for PHP code\">#<\/a><\/h2>\n\n\n\n<p>Like HTML, you need to have the opening tag to start PHP code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you mix PHP code with HTML, you need to have the enclosing tag:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">?&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">&lt;!DOCTYPE html&gt;\n&lt;html lang=<span class=\"hljs-string\">\"en\"<\/span>&gt;\n&lt;head&gt;\n    &lt;meta charset=<span class=\"hljs-string\">\"UTF-8\"<\/span>&gt;\n    &lt;title&gt;PHP Syntax&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n        &lt;h1&gt;<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'PHP Syntax'<\/span>; <span class=\"hljs-meta\">?&gt;<\/span>&lt;\/h1&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, if a file contains only PHP code, the enclosing tag is optional:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'PHP Syntax'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmVjaG8gJ1BIUCBTeW50YXgnOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='case-sensitivity'>Case sensitivity <a href=\"#case-sensitivity\" class=\"anchor\" id=\"case-sensitivity\" title=\"Anchor for Case sensitivity\">#<\/a><\/h2>\n\n\n\n<p>PHP is <strong>partially<\/strong> case-sensitive. Knowing what is case-sensitive and what is not is very important to avoid syntax errors.<\/p>\n\n\n\n<p>If you have a function such as <code>count<\/code>, you can use it as <code>COUNT<\/code>. It would work properly.<\/p>\n\n\n\n<p>The following are case-insensitive in PHP:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP constructs such as <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-if\/\">if<\/a>, <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-if-else\/\">if-else<\/a>, <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-if-elseif\/\">if-elseif<\/a>, <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-switch\/\">switch<\/a>, <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-while\/\">while<\/a>, <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-do-while\/\">do-while<\/a>, etc.<\/li>\n\n\n\n<li>Keywords such as <code>true<\/code> and <code>false<\/code>.<\/li>\n\n\n\n<li><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-functions\/\">User-defined function<\/a> &amp; <a href=\"https:\/\/phptutorial.net\/php-oop\/php-objects\/\">class names<\/a>.<\/li>\n<\/ul>\n\n\n\n<p>On the other hand, <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-variables\/\">variables<\/a> are case-sensitive. e.g., <code>$message<\/code> and <code>$MESSAGE<\/code> are different variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='statements'>Statements <a href=\"#statements\" class=\"anchor\" id=\"statements\" title=\"Anchor for Statements\">#<\/a><\/h2>\n\n\n\n<p>A PHP script typically consists of one or more statements. A statement is a code that does something, such as assigning a value to a variable or calling a function.<\/p>\n\n\n\n<p>A statement always ends with a semicolon (<code>;<\/code>). The following shows a statement that assigns a literal string to the <code>$message<\/code> variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$message = <span class=\"hljs-string\">\"Hello\"<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The above example is a simple statement. PHP also has a compound statement comprising one or more simple statements. A compound statement uses curly braces to mark a block of code. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">if<\/span>( $is_new_user ) {\n    send_welcome_email();\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You don&#8217;t need to place the semicolon after the curly brace (<code>}<\/code>).<\/p>\n\n\n\n<p>The closing tag of a PHP block (<code>?&gt;<\/code>) automatically implies a semicolon (<code>;<\/code>). Therefore, you don&#8217;t need to place a semicolon in the last statement in a PHP block. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">echo<\/span> $name <span class=\"hljs-meta\">?&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the statement <code>echo $name<\/code> doesn&#8217;t need a semicolon. However, using a semicolon for the last statement in a block should work fine. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">echo<\/span> $name; <span class=\"hljs-meta\">?&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"note\">Note that it&#8217;s OK if the code does not make sense to you now because you&#8217;ll learn more about it in the upcoming tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='whitespace-line-breaks'>Whitespace &amp; line breaks <a href=\"#whitespace-line-breaks\" class=\"anchor\" id=\"whitespace-line-breaks\" title=\"Anchor for Whitespace &amp; line breaks\">#<\/a><\/h2>\n\n\n\n<p>In most cases, whitespace and line breaks don&#8217;t have special meaning in PHP. Therefore, you can place a statement in one line or span it across multiple lines.<\/p>\n\n\n\n<p>For example, the following code snippets are equivalent:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">login( $username, $password );<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">login( \n   $username,\n   $password\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP is partially case-sensitive.<\/li>\n\n\n\n<li>PHP constructs, function names, and class names are case-insensitive, whereas variables are case-sensitive.<\/li>\n\n\n\n<li>A statement ends with a semicolon (;).<\/li>\n\n\n\n<li>Whitespace and line breaks don&#8217;t matter in PHP; leverage them to make the code more readable.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"51\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-syntax\/\"\n\t\t\t\tdata-post-title=\"PHP Syntax\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"51\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-syntax\/\"\n\t\t\t\tdata-post-title=\"PHP Syntax\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn basic PHP syntax including case sensitivity, statements, and whitespaces.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-51","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/51","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=51"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/51\/revisions"}],"predecessor-version":[{"id":2936,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/51\/revisions\/2936"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}