{"id":7334,"date":"2019-10-21T06:36:42","date_gmt":"2019-10-21T06:36:42","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=7334"},"modified":"2021-11-16T05:44:21","modified_gmt":"2021-11-16T05:44:21","slug":"complete-reference-guide-of-php-operators-control-structures","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/complete-reference-guide-of-php-operators-control-structures\/","title":{"rendered":"Complete reference guide of PHP Operators &amp; Control Structures!"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What is Operators in PHP<\/h2>\n\n\n\n<p>Operators are symbols that tell the PHP processor to perform certain actions. For example, the addition (<code>+<\/code>) symbol is an operator that tells PHP to add two variables or values, while the greater-than (<code>&gt;<\/code>) symbol is an operator that tells PHP to compare two values.<\/p>\n\n\n\n<p>The following lists describe the different operators used in PHP.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">PHP Arithmetic Operators<\/h2>\n\n\n\n<p>The arithmetic operators are used to perform common arithmetical operations, such as addition, subtraction, multiplication etc. Here&#8217;s a complete list of PHP&#8217;s arithmetic operators:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Operator<\/th><th>Description<\/th><th>Example<\/th><th>Result<\/th><\/tr><tr><td><code>+<\/code><\/td><td>Addition<\/td><td><code>$x + $y<\/code><\/td><td>Sum of $x and $y<\/td><\/tr><tr><td><code>-<\/code><\/td><td>Subtraction<\/td><td><code>$x - $y<\/code><\/td><td>Difference of $x and $y.<\/td><\/tr><tr><td><code>*<\/code><\/td><td>Multiplication<\/td><td><code>$x * $y<\/code><\/td><td>Product of $x and $y.<\/td><\/tr><tr><td><code>\/<\/code><\/td><td>Division<\/td><td><code>$x \/ $y<\/code><\/td><td>Quotient of $x and $y<\/td><\/tr><tr><td><code>%<\/code><\/td><td>Modulus<\/td><td><code>$x % $y<\/code><\/td><td>Remainder of $x divided by $y<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\n\nThe following example will show you these arithmetic operators in action:\n\n<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n$x = <span class=\"hljs-number\">10<\/span>;\n$y = <span class=\"hljs-number\">4<\/span>;\n<span class=\"hljs-keyword\">echo<\/span>($x + $y); <span class=\"hljs-comment\">\/\/ 0utputs: 14<\/span>\n<span class=\"hljs-keyword\">echo<\/span>($x - $y); <span class=\"hljs-comment\">\/\/ 0utputs: 6<\/span>\n<span class=\"hljs-keyword\">echo<\/span>($x * $y); <span class=\"hljs-comment\">\/\/ 0utputs: 40<\/span>\n<span class=\"hljs-keyword\">echo<\/span>($x \/ $y); <span class=\"hljs-comment\">\/\/ 0utputs: 2.5<\/span>\n<span class=\"hljs-keyword\">echo<\/span>($x % $y); <span class=\"hljs-comment\">\/\/ 0utputs: 2<\/span>\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">PHP Assignment Operators<\/h2>\n\n\n\n<p> The assignment operators are used to assign values to variables. <\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Operator<\/th><th>Description<\/th><th>Example<\/th><th>Is The Same As<\/th><\/tr><tr><td><code>=<\/code><\/td><td>Assign<\/td><td><code>$x = $y<\/code><\/td><td><code>$x = $y<\/code><\/td><\/tr><tr><td><code>+=<\/code><\/td><td>Add and assign<\/td><td><code>$x += $y<\/code><\/td><td><code>$x = $x + $y<\/code><\/td><\/tr><tr><td><code>-=<\/code><\/td><td>Subtract and assign<\/td><td><code>$x -= $y<\/code><\/td><td><code>$x = $x - $y<\/code><\/td><\/tr><tr><td><code>*=<\/code><\/td><td>Multiply and assign<\/td><td><code>$x *= $y<\/code><\/td><td><code>$x = $x * $y<\/code><\/td><\/tr><tr><td><code>\/=<\/code><\/td><td>Divide and assign quotient<\/td><td><code>$x \/= $y<\/code><\/td><td><code>$x = $x \/ $y<\/code><\/td><\/tr><tr><td><code>%=<\/code><\/td><td>Divide and assign modulus<\/td><td><code>$x %= $y<\/code><\/td><td><code>$x = $x % $y<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\n\nThe following example will show you these assignment operators in action:\n\n<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n$x = <span class=\"hljs-number\">10<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> $x; <span class=\"hljs-comment\">\/\/ Outputs: 10<\/span>\n \n$x = <span class=\"hljs-number\">20<\/span>;\n$x += <span class=\"hljs-number\">30<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> $x; <span class=\"hljs-comment\">\/\/ Outputs: 50<\/span>\n \n$x = <span class=\"hljs-number\">50<\/span>;\n$x -= <span class=\"hljs-number\">20<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> $x; <span class=\"hljs-comment\">\/\/ Outputs: 30<\/span>\n \n$x = <span class=\"hljs-number\">5<\/span>;\n$x *= <span class=\"hljs-number\">25<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> $x; <span class=\"hljs-comment\">\/\/ Outputs: 125<\/span>\n \n$x = <span class=\"hljs-number\">50<\/span>;\n$x \/= <span class=\"hljs-number\">10<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> $x; <span class=\"hljs-comment\">\/\/ Outputs: 5<\/span>\n \n$x = <span class=\"hljs-number\">100<\/span>;\n$x %= <span class=\"hljs-number\">15<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> $x; <span class=\"hljs-comment\">\/\/ Outputs: 10<\/span>\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">PHP Comparison Operators<\/h2>\n\n\n\n<p> The comparison operators are used to compare two values in a Boolean fashion. <\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Operator<\/th><th>Name<\/th><th>Example<\/th><th>Result<\/th><\/tr><tr><td><code>==<\/code><\/td><td>Equal<\/td><td><code>$x == $y<\/code><\/td><td>True if $x is equal to $y<\/td><\/tr><tr><td><code>===<\/code><\/td><td>Identical<\/td><td><code>$x === $y<\/code><\/td><td>True if $x is equal to $y, and they are of the same type<\/td><\/tr><tr><td><code>!=<\/code><\/td><td>Not equal<\/td><td><code>$x != $y<\/code><\/td><td>True if $x is not equal to $y<\/td><\/tr><tr><td><code>&lt;&gt;<\/code><\/td><td>Not equal<\/td><td><code>$x &lt;&gt; $y<\/code><\/td><td>True if $x is not equal to $y<\/td><\/tr><tr><td><code>!==<\/code><\/td><td>Not identical<\/td><td><code>$x !== $y<\/code><\/td><td>True if $x is not equal to $y, or they are not of the same type<\/td><\/tr><tr><td><code>&lt;<\/code><\/td><td>Less than<\/td><td><code>$x &lt; $y<\/code><\/td><td>True if $x is less than $y<\/td><\/tr><tr><td><code>&gt;<\/code><\/td><td>Greater than<\/td><td><code>$x &gt; $y<\/code><\/td><td>True if $x is greater than $y<\/td><\/tr><tr><td><code>&gt;=<\/code><\/td><td>Greater than or equal to<\/td><td><code>$x &gt;= $y<\/code><\/td><td>True if $x is greater than or equal to $y<\/td><\/tr><tr><td><code>&lt;=<\/code><\/td><td>Less than or equal to<\/td><td><code>$x &lt;= $y<\/code><\/td><td>True if $x is less than or equal to $y<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p> The following example will show you these comparison operators in action: <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n$x = <span class=\"hljs-number\">25<\/span>;\n$y = <span class=\"hljs-number\">35<\/span>;\n$z = <span class=\"hljs-string\">\"25\"<\/span>;\nvar_dump($x == $z);  <span class=\"hljs-comment\">\/\/ Outputs: boolean true<\/span>\nvar_dump($x === $z); <span class=\"hljs-comment\">\/\/ Outputs: boolean false<\/span>\nvar_dump($x != $y);  <span class=\"hljs-comment\">\/\/ Outputs: boolean true<\/span>\nvar_dump($x !== $z); <span class=\"hljs-comment\">\/\/ Outputs: boolean true<\/span>\nvar_dump($x &lt; $y);   <span class=\"hljs-comment\">\/\/ Outputs: boolean true<\/span>\nvar_dump($x &gt; $y);   <span class=\"hljs-comment\">\/\/ Outputs: boolean false<\/span>\nvar_dump($x &lt;= $y);  <span class=\"hljs-comment\">\/\/ Outputs: boolean true<\/span>\nvar_dump($x &gt;= $y);  <span class=\"hljs-comment\">\/\/ Outputs: boolean false<\/span>\n<span class=\"hljs-meta\">?&gt;<\/span><\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">PHP Incrementing and Decrementing Operators<\/h2>\n\n\n\n<p>The increment\/decrement operators are used to increment\/decrement a variable&#8217;s value.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Operator<\/th><th>Name<\/th><th>Effect<\/th><\/tr><tr><td><code>++$x<\/code><\/td><td>Pre-increment<\/td><td>Increments $x by one, then returns $x<\/td><\/tr><tr><td><code>$x++<\/code><\/td><td>Post-increment<\/td><td>Returns $x, then increments $x by one<\/td><\/tr><tr><td><code>--$x<\/code><\/td><td>Pre-decrement<\/td><td>Decrements $x by one, then returns $x<\/td><\/tr><tr><td><code>$x--<\/code><\/td><td>Post-decrement<\/td><td>Returns $x, then decrements $x by one<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following example will show you these increment and decrement operators in action:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n$x = <span class=\"hljs-number\">10<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> ++$x; <span class=\"hljs-comment\">\/\/ Outputs: 11<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $x;   <span class=\"hljs-comment\">\/\/ Outputs: 11<\/span>\n \n$x = <span class=\"hljs-number\">10<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> $x++; <span class=\"hljs-comment\">\/\/ Outputs: 10<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $x;   <span class=\"hljs-comment\">\/\/ Outputs: 11<\/span>\n \n$x = <span class=\"hljs-number\">10<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> --$x; <span class=\"hljs-comment\">\/\/ Outputs: 9<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $x;   <span class=\"hljs-comment\">\/\/ Outputs: 9<\/span>\n \n$x = <span class=\"hljs-number\">10<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> $x--; <span class=\"hljs-comment\">\/\/ Outputs: 10<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $x;   <span class=\"hljs-comment\">\/\/ Outputs: 9<\/span>\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">PHP Logical Operators<\/h2>\n\n\n\n<p>The logical operators are typically used to combine conditional statements.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Operator<\/th><th>Name<\/th><th>Example<\/th><th>Result<\/th><\/tr><tr><td><code>and<\/code><\/td><td>And<\/td><td><code>$x and $y<\/code><\/td><td>True if both $x and $y are true<\/td><\/tr><tr><td><code>or<\/code><\/td><td>Or<\/td><td><code>$x or $y<\/code><\/td><td>True if either $x or $y is true<\/td><\/tr><tr><td><code>xor<\/code><\/td><td>Xor<\/td><td><code>$x xor $y<\/code><\/td><td>True if either $x or $y is true, but not both<\/td><\/tr><tr><td><code>&amp;&amp;<\/code><\/td><td>And<\/td><td><code>$x &amp;&amp; $y<\/code><\/td><td>True if both $x and $y are true<\/td><\/tr><tr><td><code>||<\/code><\/td><td>Or<\/td><td><code>$x || $y<\/code><\/td><td>True if either $$x or $y is true<\/td><\/tr><tr><td><code>!<\/code><\/td><td>Not<\/td><td><code>!$x<\/code><\/td><td>True if $x is not true<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following example will show you these logical operators in action:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n$year = <span class=\"hljs-number\">2014<\/span>;\n<span class=\"hljs-comment\">\/\/ Leap years are divisible by 400 or by 4 but not 100<\/span>\n<span class=\"hljs-keyword\">if<\/span>(($year % <span class=\"hljs-number\">400<\/span> == <span class=\"hljs-number\">0<\/span>) || (($year % <span class=\"hljs-number\">100<\/span> != <span class=\"hljs-number\">0<\/span>) &amp;&amp; ($year % <span class=\"hljs-number\">4<\/span> == <span class=\"hljs-number\">0<\/span>))){\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"$year is a leap year.\"<\/span>;\n} <span class=\"hljs-keyword\">else<\/span>{\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"$year is not a leap year.\"<\/span>;\n}\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">PHP String Operators<\/h2>\n\n\n\n<p>There are two operators which are specifically designed for&nbsp;<a href=\"https:\/\/www.tutorialrepublic.com\/php-tutorial\/php-strings.php\" target=\"_blank\" rel=\"noopener\">strings<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Operator<\/th><th>Description<\/th><th>Example<\/th><th>Result<\/th><\/tr><tr><td><code>.<\/code><\/td><td>Concatenation<\/td><td><code>$str1 . $str2<\/code><\/td><td>Concatenation of $str1 and $str2<\/td><\/tr><tr><td><code>.=<\/code><\/td><td>Concatenation assignment<\/td><td><code>$str1 .= $str2<\/code><\/td><td>Appends the $str2 to the $str1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following example will show you these string operators in action:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n$x = <span class=\"hljs-string\">\"Hello\"<\/span>;\n$y = <span class=\"hljs-string\">\" World!\"<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> $x . $y; <span class=\"hljs-comment\">\/\/ Outputs: Hello World!<\/span>\n \n$x .= $y;\n<span class=\"hljs-keyword\">echo<\/span> $x; <span class=\"hljs-comment\">\/\/ Outputs: Hello World!<\/span>\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">PHP Array Operators<\/h2>\n\n\n\n<p>The array operators are used to compare arrays:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Operator<\/th><th>Name<\/th><th>Example<\/th><th>Result<\/th><\/tr><tr><td><code>+<\/code><\/td><td>Union<\/td><td><code>$x + $y<\/code><\/td><td>Union of $x and $y<\/td><\/tr><tr><td><code>==<\/code><\/td><td>Equality<\/td><td><code>$x == $y<\/code><\/td><td>True if $x and $y have the same key\/value pairs<\/td><\/tr><tr><td><code>===<\/code><\/td><td>Identity<\/td><td><code>$x === $y<\/code><\/td><td>True if $x and $y have the same key\/value pairs in the same order and of the same types<\/td><\/tr><tr><td><code>!=<\/code><\/td><td>Inequality<\/td><td><code>$x != $y<\/code><\/td><td>True if $x is not equal to $y<\/td><\/tr><tr><td><code>&lt;&gt;<\/code><\/td><td>Inequality<\/td><td><code>$x &lt;&gt; $y<\/code><\/td><td>True if $x is not equal to $y<\/td><\/tr><tr><td><code>!==<\/code><\/td><td>Non-identity<\/td><td><code>$x !== $y<\/code><\/td><td>True if $x is not identical to $y<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following example will show you these array operators in action:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n$x = <span class=\"hljs-keyword\">array<\/span>(<span class=\"hljs-string\">\"a\"<\/span> =&gt; <span class=\"hljs-string\">\"Red\"<\/span>, <span class=\"hljs-string\">\"b\"<\/span> =&gt; <span class=\"hljs-string\">\"Green\"<\/span>, <span class=\"hljs-string\">\"c\"<\/span> =&gt; <span class=\"hljs-string\">\"Blue\"<\/span>);\n$y = <span class=\"hljs-keyword\">array<\/span>(<span class=\"hljs-string\">\"u\"<\/span> =&gt; <span class=\"hljs-string\">\"Yellow\"<\/span>, <span class=\"hljs-string\">\"v\"<\/span> =&gt; <span class=\"hljs-string\">\"Orange\"<\/span>, <span class=\"hljs-string\">\"w\"<\/span> =&gt; <span class=\"hljs-string\">\"Pink\"<\/span>);\n$z = $x + $y; <span class=\"hljs-comment\">\/\/ Union of $x and $y<\/span>\nvar_dump($z);\nvar_dump($x == $y);   <span class=\"hljs-comment\">\/\/ Outputs: boolean false<\/span>\nvar_dump($x === $y);  <span class=\"hljs-comment\">\/\/ Outputs: boolean false<\/span>\nvar_dump($x != $y);   <span class=\"hljs-comment\">\/\/ Outputs: boolean true<\/span>\nvar_dump($x &lt;&gt; $y);   <span class=\"hljs-comment\">\/\/ Outputs: boolean true<\/span>\nvar_dump($x !== $y);  <span class=\"hljs-comment\">\/\/ Outputs: boolean true<\/span>\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h1 class=\"wp-block-heading\"> Control Structures&nbsp;.<\/h1>\n\n\n\n<p> Control Structures&nbsp; <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is a control structure?<\/h3>\n\n\n\n<p>Code execution can be grouped into categories as shown below<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Sequential&nbsp;<\/strong>\u2013 this one involves executing all the codes in the order in which they have been written.<\/li><li><strong>Decision<\/strong>&nbsp;\u2013 this one involves making a choice given a number of options. The code executed depends on the value of the condition.<\/li><\/ul>\n\n\n\n<p>A control structure is a block of code that decides the execution path of a program depending on the value of the set condition.<\/p>\n\n\n\n<p>Let\u2019s now look at some of the control structures that PHP supports.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">PHP IF Else<\/h2>\n\n\n\n<p>If\u2026 then&#8230; else is the&nbsp;<strong>simplest<\/strong><strong>control<\/strong><strong>structure<\/strong>. It evaluates the conditions using Boolean logic<br><strong>When to use if\u2026 then\u2026 else<\/strong><br><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>You have a block of code that should be executed only if a certain condition is true<\/li><li>You have two options, and you have to select one.<\/li><li>If\u2026 then\u2026 else if\u2026 is used when you have to select more than two options and you have to select one or more<\/li><\/ul>\n\n\n\n<p><strong>Syntax<\/strong>&nbsp;The syntax for if\u2026 then\u2026 else is;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-keyword\">if<\/span> (condition is <span class=\"hljs-keyword\">true<\/span>) {\n\nblock one\n\n<span class=\"hljs-keyword\">else<\/span>\n\nblock two\n\n}\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>&nbsp;<strong>HERE,<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u201c<strong>if (condition is true)\u201d<\/strong>&nbsp;is the control structure<\/li><li>\u201c<strong>block one<\/strong>\u201d is the code to be executed if the condition is true<\/li><li><strong>{\u2026else\u2026}&nbsp;<\/strong>is the fallback if the condition is false<\/li><li>\u201c<strong>block two<\/strong>\u201d is the block of code executed if the condition is false<\/li><\/ul>\n\n\n\n<p><strong>How it works<\/strong>&nbsp;The flow chart shown below illustrates how the if then\u2026 else control structure works<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"420\" height=\"378\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/10\/if_then_flowchart.png\" alt=\"\" class=\"wp-image-7337\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/10\/if_then_flowchart.png 420w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/10\/if_then_flowchart-300x270.png 300w\" sizes=\"auto, (max-width: 420px) 100vw, 420px\" \/><\/figure>\n\n\n\n<p><strong>Let\u2019s see this in action<\/strong>&nbsp;The code below uses \u201cif\u2026 then\u2026 else\u201d to determine the larger value between two numbers. <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$first_number = <span class=\"hljs-number\">7<\/span>;\n\n$second_number = <span class=\"hljs-number\">21<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> ($first_number &gt; $second_number){\n\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"$first_number is greater than $second_number\"<\/span>;\n\n}<span class=\"hljs-keyword\">else<\/span>{\n\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"$second_number is greater than $first_number\"<\/span>;\n\n}\n\n<span class=\"hljs-meta\">?&gt;<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">21 is greater than 7<\/pre>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_64918\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_61760\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_64918\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Operators in PHP Operators are symbols that tell the PHP processor to perform certain actions. For example, the addition (+) symbol is an operator that tells PHP to add two variables or values, while the greater-than (&gt;) symbol is an operator that tells PHP to compare two values. The following lists describe the&#8230;<\/p>\n","protected":false},"author":14,"featured_media":7343,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5150],"tags":[5439,177,5438],"class_list":["post-7334","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-control-structures","tag-php","tag-php-operators"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=7334"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7334\/revisions"}],"predecessor-version":[{"id":25369,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7334\/revisions\/25369"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/7343"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=7334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=7334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=7334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}