{"id":214,"date":"2018-07-10T11:50:34","date_gmt":"2018-07-10T11:50:34","guid":{"rendered":"https:\/\/www.go4expert.com\/tutorials\/?p=214"},"modified":"2018-08-01T01:18:22","modified_gmt":"2018-08-01T01:18:22","slug":"php-syntax-overview","status":"publish","type":"post","link":"https:\/\/www.go4expert.com\/tutorials\/php-syntax-overview\/","title":{"rendered":"PHP Syntax Overview"},"content":{"rendered":"<p>Like any other programming language PHP has a set of syntax rules which we need to follow while programming in the PHP language. Learning syntax rules is important because it will help programmers increase their speed of programming, accuracy and will help them master the language quickly.<\/p>\n<p>PHP files commonly have a .php as file extension and they are always executed on the server.<\/p>\n<h2>Tags in PHP or Escaping To PHP<\/h2>\n<p>PHP programming logic are written using the PHP tags because PHP scripts can be written in separate files or anywhere in an HTML document. The PHP parsing engine needs a way to differentiate PHP code from other elements. PHP tags help achieve this. This is called 'Escaping to PHP' because the processing must be done by the PHP engine.<\/p>\n<p>We can \u2018Escape to PHP\u2019 by using the following 4 methods:<\/p>\n<h4>1. Canonical PHP tags<\/h4>\n<p>Here PHP scripts start with the tag style &lt;?php.......\u00a0 and end with\u00a0?&gt;<\/p>\n<p>Every PHP command ends with a semi-colon [;].<\/p>\n<p><strong>Example<\/strong>: \"Welcome to PHP\" program<\/p>\n<pre>&lt;?php\r\n# Here the echo command prints the message\r\necho \"Welcome to PHP\";\r\n?&gt;\r\n<\/pre>\n<p><strong>Output<\/strong>: Welcome To PHP<\/p>\n<h4>2. SGML (or) Short HTML Tags<\/h4>\n<p>Here PHP scripts start with short or short-open tags which look like<br \/>\n&lt;?....?&gt;<\/p>\n<p>Usage of Short Tags is the easiest way to initialize PHP code and it is very convenient.<br \/>\nHowever short tags can be used only if the following prerequisites are satisfied:<\/p>\n<ul>\n<li>Select '--enable-short-tags' configuration option when you are building PHP code so that short tags works.<\/li>\n<li>Also keep the short_open_tag 'On' in your php.ini file.<\/li>\n<\/ul>\n<p>Because the short PHP code has issues with parsing of XML file, it is recommended that one doesn't enable the short PHP tags and keeps them disabled.<\/p>\n<p>Here is an example of short PHP code<br \/>\n&lt;?<br \/>\n# Here the\u00a0echo command works due to<br \/>\n# settings being configured in php.ini and during code build<br \/>\necho \"Learn PHP because PHP is the best\";<br \/>\n?&gt;<\/p>\n<p>NOTE: &lt;?= is a syntax that is used as a combination of &lt;?php and echo as the first statement. It is available despite short tags are off.<\/p>\n<h4>3. HTML Script Tags<\/h4>\n<p>Here the PHP code is enclosed using tags.<\/p>\n<p>Example :<\/p>\n<p>&lt;script language=\"php\"&gt;<br \/>\necho \"Learn PHP because PHP is the best\";<br \/>\n&lt;\/script&gt;<\/p>\n<p>The HTML Script tag is actually removed from PHP 7.0 and is no longer supported.<\/p>\n<h4>4. ASP Style Tags<\/h4>\n<p>PHP code can also be written with ASP style tags. We can use the ASP Style tags by setting the configuration option in the \"php.ini\" file.<\/p>\n<p>These tags are called ASP style tags because they are also used by 'Active Server Pages\u2019 to describe code blocks.<\/p>\n<p>Example:<br \/>\n&lt;%<br \/>\n# This can only be written because the\u00a0setting is turned on in php.ini<br \/>\n# to allow %<br \/>\necho \"Learn PHP because PHP is the best\";<br \/>\n%&gt;<\/p>\n<p>Like HTML script tag, ASP style tag support is also dropped from PHP 7.0.<\/p>\n<h2>Comments in PHP<\/h2>\n<p>Comments are statements in a program by developers which describe the intent and purpose of the code. They play an important role in programming because they help make the code more readable and understandable.\u00a0Comments in PHP code are not read or executed by the PHP engine. Therefore they do not affect the output.<\/p>\n<p>We can use two styles of comments in PHP. They are as follows :<\/p>\n<h3>Single Line Comment<\/h3>\n<p>As the name suggests these are a\u00a0single line or short explanations that one can add in their script. in order to add the comment, we need to begin the statement with (\/\/) or (#).<br \/>\nExample:<br \/>\n&lt;?php<br \/>\n\/\/ This is a single line comment<\/p>\n<p>echo \"Learn PHP because PHP is the best!!!\";<\/p>\n<p># This is also a single line comment<br \/>\n?&gt;<\/p>\n<h3>Multi-line or Multiple line Comment<\/h3>\n<p>Sometimes longer explanations need to be given in a program because the code may be complex or in order to help the readers gets a lucid understanding of the code.\u00a0Multi-line comments allow users to write multiple lines of comment by using a single tag \/*\u2026\u2026\u2026\u2026..*\/<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p>&lt;?php<br \/>\n\/* Example for multiline comment<br \/>\nIn PHP, variables are written<br \/>\nby adding a $ sign at the beginning.*\/<\/p>\n<p>$message = \"Learn PHP because PHP is the best!\";<br \/>\necho $message;<br \/>\n?&gt;<\/p>\n<h2>PHP Syntax is Case-sensitive<\/h2>\n<p>In PHP besides variables, all other keywords are not case-sensitive. This includes all keywords, functions and class names. However, the most important point to note is that variables are case-sensitive.<\/p>\n<p>Therefore we have to be very careful while defining and using variables. Let\u2019s look at this example:<\/p>\n<p>&lt;?php<br \/>\n\/\/ In the example here we can see that all echo<br \/>\n\/\/ statements are executed in the same manner because keywords are case-insensitive<\/p>\n<p>$variable = 100;<br \/>\nECHO $variable;<\/p>\n<p>\/\/ This line will show a RUNTIME ERROR because the case of the variable is different than the one defined<br \/>\n\/\/ Hence you will get an \"Undefined Variable\" error<br \/>\n\/\/echo $VARIABLE<\/p>\n<p>echo $variable;<br \/>\n?&gt;<\/p>\n<p><strong>Output<\/strong>:<\/p>\n<p>The two echo statements produce an output of 100 because echo is not case sensitive.<\/p>\n<h2>Blocks in PHP :<\/h2>\n<p>Multiple statements can be executed due to some conditions in PHP (based on a condition or a loop).\u00a0These statements need to be grouped together into blocks. They have to be enclosed within curly-braces ({})because this will help differentiate the block of code from the rest of the program.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p>&lt;?php<br \/>\n$var = 200;<\/p>\n<p>if ($var rb 100){<br \/>\necho (\" Positive because it is greater than 0 \");<br \/>\n}<br \/>\n?&gt;<\/p>\n<p><strong>Output<\/strong>:<br \/>\nPositive because it is\u00a0greater than 0<\/p>\n<h2>Statement Syntax<\/h2>\n<p>A statement in PHP comprises of an expression followed by a semicolon ( ; ). Therefore a group of valid PHP statements ,which are enclosed within PHP tags, form a PHP program.<\/p>\n<p>The example below is a typical statement in PHP which assigns a string of characters to a variable called $greeting.<\/p>\n<p>$greeting = \"Welcome to PHP!\";<\/p>\n<h2>Running a PHP Script from the Command Prompt<\/h2>\n<p>The command prompt can also be used to run PHP scripts. The syntax for running PHP scripts is :<\/p>\n<p>$ php filename.php<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p>&lt;?php<br \/>\necho \"Welcome to PHP !!\";<br \/>\n?&gt;<\/p>\n<p>Now run this script in the command prompt by using the following statement<\/p>\n<p>$ php test.php<\/p>\n<p><strong>Output<\/strong>:<br \/>\nWelcome To PHP !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Like any other programming language PHP has a set of syntax rules which we need to follow while programming in the PHP language. Learning syntax rules is important because it will help programmers increase their speed of programming, accuracy and will help them master the language quickly. PHP files commonly have a .php as file [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"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-214","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-php","7":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP Syntax Overview - Go4Expert Tutorials<\/title>\n<meta name=\"description\" content=\"This article is an overview of syntax rules which are followed in PHP programming . It covers some important PHP syntax rules in-depth. It is a must-read for new PHP programmers.\" \/>\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-syntax-overview\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP Syntax Overview - Go4Expert Tutorials\" \/>\n<meta property=\"og:description\" content=\"This article is an overview of syntax rules which are followed in PHP programming . It covers some important PHP syntax rules in-depth. It is a must-read for new PHP programmers.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.go4expert.com\/tutorials\/php-syntax-overview\/\" \/>\n<meta property=\"og:site_name\" content=\"Go4Expert Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-10T11:50:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-08-01T01:18:22+00:00\" \/>\n<meta name=\"author\" content=\"shabbir\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"shabbir\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 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-syntax-overview\/\",\"url\":\"https:\/\/www.go4expert.com\/tutorials\/php-syntax-overview\/\",\"name\":\"PHP Syntax Overview - Go4Expert Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#website\"},\"datePublished\":\"2018-07-10T11:50:34+00:00\",\"dateModified\":\"2018-08-01T01:18:22+00:00\",\"author\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/187ce4675295e6b09b98a9374ce2fec6\"},\"description\":\"This article is an overview of syntax rules which are followed in PHP programming . It covers some important PHP syntax rules in-depth. It is a must-read for new PHP programmers.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/php-syntax-overview\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.go4expert.com\/tutorials\/php-syntax-overview\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/php-syntax-overview\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.go4expert.com\/tutorials\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP Syntax Overview\"}]},{\"@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\/187ce4675295e6b09b98a9374ce2fec6\",\"name\":\"shabbir\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/303ce933c6bfabf6ac58bc870c696a1d5ef98012ecb820a4a7921853b813406f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/303ce933c6bfabf6ac58bc870c696a1d5ef98012ecb820a4a7921853b813406f?s=96&d=mm&r=g\",\"caption\":\"shabbir\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP Syntax Overview - Go4Expert Tutorials","description":"This article is an overview of syntax rules which are followed in PHP programming . It covers some important PHP syntax rules in-depth. It is a must-read for new PHP programmers.","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-syntax-overview\/","og_locale":"en_US","og_type":"article","og_title":"PHP Syntax Overview - Go4Expert Tutorials","og_description":"This article is an overview of syntax rules which are followed in PHP programming . It covers some important PHP syntax rules in-depth. It is a must-read for new PHP programmers.","og_url":"https:\/\/www.go4expert.com\/tutorials\/php-syntax-overview\/","og_site_name":"Go4Expert Tutorials","article_published_time":"2018-07-10T11:50:34+00:00","article_modified_time":"2018-08-01T01:18:22+00:00","author":"shabbir","twitter_card":"summary_large_image","twitter_misc":{"Written by":"shabbir","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.go4expert.com\/tutorials\/php-syntax-overview\/","url":"https:\/\/www.go4expert.com\/tutorials\/php-syntax-overview\/","name":"PHP Syntax Overview - Go4Expert Tutorials","isPartOf":{"@id":"https:\/\/www.go4expert.com\/tutorials\/#website"},"datePublished":"2018-07-10T11:50:34+00:00","dateModified":"2018-08-01T01:18:22+00:00","author":{"@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/187ce4675295e6b09b98a9374ce2fec6"},"description":"This article is an overview of syntax rules which are followed in PHP programming . It covers some important PHP syntax rules in-depth. It is a must-read for new PHP programmers.","breadcrumb":{"@id":"https:\/\/www.go4expert.com\/tutorials\/php-syntax-overview\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.go4expert.com\/tutorials\/php-syntax-overview\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.go4expert.com\/tutorials\/php-syntax-overview\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.go4expert.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"PHP Syntax Overview"}]},{"@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\/187ce4675295e6b09b98a9374ce2fec6","name":"shabbir","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.go4expert.com\/tutorials\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/303ce933c6bfabf6ac58bc870c696a1d5ef98012ecb820a4a7921853b813406f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/303ce933c6bfabf6ac58bc870c696a1d5ef98012ecb820a4a7921853b813406f?s=96&d=mm&r=g","caption":"shabbir"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9JSSi-3s","_links":{"self":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/214","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/comments?post=214"}],"version-history":[{"count":27,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/214\/revisions"}],"predecessor-version":[{"id":296,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/posts\/214\/revisions\/296"}],"wp:attachment":[{"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/media?parent=214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/categories?post=214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.go4expert.com\/tutorials\/wp-json\/wp\/v2\/tags?post=214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}