{"id":79,"date":"2015-04-16T16:33:30","date_gmt":"2015-04-16T08:33:30","guid":{"rendered":"http:\/\/woohuiren.me\/blog\/?p=79"},"modified":"2015-09-26T20:50:23","modified_gmt":"2015-09-26T12:50:23","slug":"php-tutorial-basic-syntax","status":"publish","type":"post","link":"https:\/\/woohuiren.me\/blog\/php-tutorial-basic-syntax\/","title":{"rendered":"PHP Tutorial: Basic syntax"},"content":{"rendered":"<p>Hello everyone, this is a continuation to the first PHP article that introduces you to PHP. In this article, we will be going through the basic syntax of PHP. <\/p>\n<hr\/>\n<p><\/p>\n<h2>Recap<\/h2>\n<p>In the <a href=\"http:\/\/woohuiren.me\/blog\/introduction-to-php\/\" title=\"Introduction to PHP\">previous article<\/a>, you&#8217;ve been exposed to how PHP works, PHP handlers, PHP interpreter and the simple Hello World code. <\/p>\n<hr\/>\n<p><\/p>\n<h2>Variable Types<\/h2>\n<p>There are several variables type in PHP and these variables are present in most programming languages too. This article will briefly explain the variable types. Some might be very simple and familar while others might be quite new to you. <\/p>\n<h3>Integers<\/h3>\n<p>An integer is a number that is the range of &#8230;, -2, -1, 0, 1, 2, &#8230;. They are usually declared as decimals but can also be declared as negative, octal, hexadecimal or binary as well.<\/p>\n<h3>Booleans<\/h3>\n<p>A very simple variable type, it can either be TRUE or FALSE. <\/p>\n<h3>Strings<\/h3>\n<p>A string is series of characters. For PHP, the supported set is the 256-character set and the maximum size of a string variable is 2GB. There are 4 different type of strings &#8211; single quoted, double quoted, heredoc syntax and nowdoc syntax.<\/p>\n<h3>Floats<\/h3>\n<p>Floats, also known as doubles\/real numbers\/floating point numbers, are platform dependent variables that has a precision of roughly 14 decimal digits. Due to its limited precision, it should never be trusted for complicated calculations and exact rounding. <\/p>\n<h3>Arrays<\/h3>\n<p>An array in PHP is an ordered map, which associates values to keys. Due to nature of this, it can be treated as an array, list, hash table, dictionary, collection and many more. Arrays in PHP makes it stand out against other programming languages as it makes multidimensional arrays possible and really easy\/simple to use.<\/p>\n<h3>Objects<\/h3>\n<p>Objects are created from instantiating a class through the &#8220;new&#8221; statement. <\/p>\n<h3>NULL<\/h3>\n<p>NULL means a variable with no value. NULL is the only possible value of type null. <\/p>\n<h3>Resources<\/h3>\n<p>Resources are special variable type that holds a reference to another external resource.<\/p>\n<h3>Callbacks \/ Callables<\/h3>\n<p>We won&#8217;t be touching on this for now.<\/p>\n<h3>Pseudo-types<\/h3>\n<p>We won&#8217;t be touching on this for now.<\/p>\n<h3>Juggling (Default)<\/h3>\n<p>This will be touched on in the later part &#8211; Initializing Variables.<\/p>\n<hr\/>\n<p><\/p>\n<h2>Operators<\/h2>\n<p>Operators are used in programming languages to carry out arithmetical calculations and bitwise operations. It is the typical plus + and minus -.<\/p>\n<p>So here are some of the basic operators: <\/p>\n<ul>\n<li>+ &#8211; Addition<\/li>\n<li>&minus; &#8211; Subtraction<\/li>\n<li>% &#8211; Modulus<\/li>\n<li>* &#8211; Multiply<\/li>\n<li>\/ &#8211; Divide<\/li>\n<li>^ &#8211; To the power of<\/li>\n<li>= &#8211; Set<\/li>\n<li>== &#8211; Is equal to comparison (loose variable type comparison)<\/li>\n<li>=== &#8211; Is equal to comparison (strict variable type comparison)<\/li>\n<li>+= &#8211; Add to<\/li>\n<li>-= &#8211; Remove from<\/li>\n<li>++ &#8211; Increase by 1<\/li>\n<li>&minus;&minus; &#8211; Decrease by 1<\/li>\n<li>&#038;&#038; &#8211; AND operator<\/li>\n<li>|| &#8211; OR operator<\/li>\n<\/ul>\n<hr\/>\n<p><\/p>\n<h2>Initializing Variables<\/h2>\n<p>Variables are used in programming to store information so that it can be re-used again later. It is fairly easy to initialize a variable in PHP. Variables start with a dollar sign $ and the default declaration of variables makes the variables not limited to types. This means that when declared using a dollar sign $ without explicit type declaration, it will be a &#8220;Type Juggling&#8221; variable. <\/p>\n<h3>Example #1<\/h3>\n<pre>&lt;?php\r\n$x = 1;\r\necho $x; \/\/this should output \"1\" on the web page\r\n$x = \"abc d e\"; \/\/as it isn't limited to variable types, it can be changed from a integer variable to string variable without any conversion or creation of new variable\r\necho $x; \/\/this should output \"abc d e\" on the web page\r\n?&gt;<\/pre>\n<p>Result:<\/p>\n<pre>1abc d e<\/pre>\n<p>That was easy, wasn&#8217;t it? Now we&#8217;re going to take this one step further. <\/p>\n<h3>Example #2<\/h3>\n<pre>&lt;?php\r\n$x = 1;\r\necho $x;\r\n$x++;\r\n$b = \"abc d e\";\r\n$b += $x;\r\necho $b;\r\n?&gt;<\/pre>\n<p>Result:<\/p>\n<pre>1abc d e2<\/pre>\n<hr\/>\n<p><\/p>\n<h2>If Else?<\/h2>\n<p>If and else conditional statements are used in programming to see if the statement is true before executing what is in-between its curly braces {}. <\/p>\n<h3>Example<\/h3>\n<pre>&lt;?php\r\n$intVariable = 5; \/\/declare the variable\r\n\r\nif($intVariable == 5) {\r\n\techo \"lol\";\r\n} elseif($intVariable == 6) {\r\n\techo \"rofl\";\r\n} else {\r\n\techo \"lmao\";\r\n}\r\n?&gt;<\/pre>\n<p>Result:<\/p>\n<pre>lol<\/pre>\n<hr\/>\n<p><\/p>\n<h2>Loops<\/h2>\n<p>Loops are used in programming to execute repetitive\/continuous actions or functions within its scope as long as the statement in the loop is &#8220;true&#8221; or &#8220;1&#8221;. There are 3 main loops in PHP, the while loop, for loop and foreach loop.<\/p>\n<h3>Example<\/h3>\n<pre>&lt;?php\r\nfor($i = 0; $i < 10; $i++) {\r\n\techo $i; \/\/echo the value of $i\r\n}\r\n?&gt;<\/pre>\n<p>Result:<\/p>\n<pre>0123456789<\/pre>\n<hr\/>\n<p><\/p>\n<h2>Common Practices<\/h2>\n<h3>Camel Case<\/h3>\n<p>Camel casing is a common practice in most programming languages. It is not mandatory to put variables in camel case format but it should be done. Variables need to be in a camel case format - the first letter of the first word is in lowercase while the first letter of the other words are in uppercase. <\/p>\n<h4>Example<\/h4>\n<pre>&lt;?php\r\n$veryLongVariableNameThatWillShowThisCamelCaseFormatting = true;\r\n$intVariable = 12;\r\n$floatVariable = 3.14;\r\n?&gt;<\/pre>\n<p><\/p>\n<h3>Identation<\/h3>\n<p>Identations are not required in PHP but help keeps the code neat and readable. Depending on personal preferences, indentations can be either spaces or tabs. However, I recommend tabs indentations because on most text-editors and IDEs, it is easier to delete tab indentations than space indentations. Indentations should be made when your code enters one layer down or into another scope. <\/p>\n<h4>Example<\/h4>\n<pre>&lt;?php\r\n$intVariable = 12; \/\/declare the variable\r\n$intVariable2 = 5; \/\/declare the variable\r\n\r\nif($intVariable == 5) {\r\n\techo \"lol\";\r\n} elseif($intVariable == 12) {\r\n\tif($intVariable2 == 5) {\r\n\t\techo \"toplel\";\r\n\t} else {\r\n\t\techo \"rofl\";\r\n\t}\r\n} else {\r\n\techo \"lmao\";\r\n}\r\n?&gt;<\/pre>\n<p>Result:<\/p>\n<pre>toplel<\/pre>\n<hr\/>\n<p><\/p>\n<h2>Conclusion<\/h2>\n<p>And that's it for now. We've gone through operators, variables, conditional statements, loops and common practices. Thanks for being a great reader and I've hope you have enjoyed learning the basic syntax of PHP. If you have any questions, feel free to leave a comment below and I'll get back to you as soon as possible. <\/p>\n<p>Next Tutorial: <a href=\"https:\/\/woohuiren.me\/blog\/php-tutorial-requests\/\">PHP Requests<\/a><\/p>\n<p>Tutorial Overview: <a href=\"https:\/\/woohuiren.me\/blog\/php-fast-track-learning\/\">Fast Track Learning<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone, this is a continuation to the first PHP article that introduces you to PHP. In this article, we will be going through the basic syntax of PHP. Recap In the previous article, you&#8217;ve been exposed to how PHP works, PHP handlers, PHP interpreter and the simple Hello World code. Variable Types There are &hellip; <a href=\"https:\/\/woohuiren.me\/blog\/php-tutorial-basic-syntax\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;PHP Tutorial: Basic syntax&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":94,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[2,29],"tags":[30,35],"class_list":["post-79","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-information-technology","category-tutorials","tag-php","tag-syntax"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/i0.wp.com\/woohuiren.me\/blog\/wp-content\/uploads\/2015\/04\/basic-php-syntax.jpg?fit=1920%2C1080&ssl=1","_links":{"self":[{"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/posts\/79"}],"collection":[{"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/comments?post=79"}],"version-history":[{"count":18,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/posts\/79\/revisions"}],"predecessor-version":[{"id":364,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/posts\/79\/revisions\/364"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/media\/94"}],"wp:attachment":[{"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/media?parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/categories?post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/woohuiren.me\/blog\/wp-json\/wp\/v2\/tags?post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}