1
0
mirror of https://github.com/restoreddev/phpapprentice.git synced 2025-10-23 10:56:08 +02:00
Files
phpapprentice/docs/conditionals.html
2018-09-02 10:57:36 -05:00

120 lines
6.3 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Conditionals</title>
<meta name="description" content="">
<link rel="stylesheet" href="/css/site.css">
<link rel="icon" href="/favicon-32.png">
<script src="/js/site.js"></script>
</head>
<body>
<div class="menu">
<button class="menu-button" title="Open Menu">
<div class="icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M0 3h20v2H0V3zm0 6h20v2H0V9zm0 6h20v2H0v-2z"/></svg></div>
Menu
</button>
</div>
<div class="container center">
<h1>Conditionals</h1>
<h3 class="subtitle">Checking the if before the what</h3>
<div class="grid-code"><div class="doc"></div><div class="code"><pre><code class="language-php">&lt;?php
</code></pre></div><div class="doc">When writing code, there will be times when you need to perform actions only under certain circumstances. There are several ways to control execution in PHP. We will start with an if statement.</div><div class="code"><pre><code class="language-php">$animal = 'cow';
if ($animal == 'cow') {
echo &quot;Moooooo.....\n&quot;;
}
</code></pre></div><div class="doc">All conditionals check to see if a statement evaluates to true or false. In the case above, since $animal equals 'cow', the statement returns true and the contents of the if statement are executed.</div><div class="code"><pre><code class="language-php">
</code></pre></div><div class="doc">An if statement can have multiple conditions chained together. If the first if statement returns false, then PHP will check each elseif. If none of the checks return true, then the else block will be executed.</div><div class="code"><pre><code class="language-php">$animal = 'bird';
if ($animal == 'dog') {
echo &quot;Woof! 🐶\n&quot;;
} elseif ($animal == 'cat') {
echo &quot;Meow!? 🐱\n&quot;;
} elseif ($animal == 'bird') {
echo &quot;Chirp! 🐦\n&quot;;
} else {
echo &quot;I am not a dog, cat or bird\n&quot;;
}
</code></pre></div><div class="doc">An alternative to the if statement is the switch. A switch statement has multiple cases to check if the value in parentheses equals something. In this statement, since $food equals 'apples', the switch will echo &quot;Eating an apple&quot;. The default case will be run if no other case evaluates to true, like an else statement.</div><div class="code"><pre><code class="language-php">$food = 'apples';
switch ($food) {
case 'apples':
echo &quot;Eating an apple\n&quot;;
break;
case 'oranges':
echo &quot;Eating an orange\n&quot;;
break;
case 'peaches':
echo &quot;Eating a peach\n&quot;;
break;
default:
echo &quot;No food, I am hungry\n&quot;;
}
</code></pre></div><div class="doc">Breaks are a special keyword that tell PHP to stop execution once a case passes. If you do not use a break, PHP will continue to execute all following cases. In this switch, both &quot;Drinking water&quot; and &quot;Drinking tea&quot; will be executed since there is no break in the 'water' case.</div><div class="code"><pre><code class="language-php">$drink = 'water';
switch ($drink) {
case 'water':
echo &quot;Drinking water\n&quot;;
case 'tea':
echo &quot;Drinking tea\n&quot;;
break;
}
</code></pre></div><div class="doc">PHP also supports single line conditionals called a ternary. In a ternary, the condition is followed by a question mark before the value that should be returned if the condition is true and then another colon and a value to return if the condition is false.</div><div class="code"><pre><code class="language-php">$language = 'english';
echo $language == 'spanish' ? &quot;hola\n&quot; : &quot;hello\n&quot;;
</code></pre></div><div class="doc">Lastly, there is another form of a ternary that checks if a value is set and then returns the value to the right of two question marks if value is null.</div><div class="code"><pre><code class="language-php">echo $IDoNotExist ?? &quot;Variable not set\n&quot;;
</code></pre></div><div class="doc">You can also chain multiple checks in a row.</div><div class="code"><pre><code class="language-php">$IExist = &quot;Variable exists\n&quot;;
echo $IDoNotExist ?? $IExist ?? &quot;Neither variable is set\n&quot;;
</code></pre></div></div>
<div class="clearfix"></div>
<div class="navigate-links">
<a href="/boolean-logic.html" title="Previous">
<div class="icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M10 20a10 10 0 1 1 0-20 10 10 0 0 1 0 20zm8-10a8 8 0 1 0-16 0 8 8 0 0 0 16 0zM7.46 9.3L11 5.75l1.41 1.41L9.6 10l2.82 2.83L11 14.24 6.76 10l.7-.7z"/></svg></div>
Previous
</a>
<a href="/loops.html" title="Next">
Next
<div class="icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M10 0a10 10 0 1 1 0 20 10 10 0 0 1 0-20zM2 10a8 8 0 1 0 16 0 8 8 0 0 0-16 0zm10.54.7L9 14.25l-1.41-1.41L10.4 10 7.6 7.17 9 5.76 13.24 10l-.7.7z"/></svg></div>
</a>
</div>
</div>
<div class="modal closed">
<div class="modal-content">
<button class="modal-button right" title="Close">
<div class="icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M2.93 17.07A10 10 0 1 1 17.07 2.93 10 10 0 0 1 2.93 17.07zm1.41-1.41A8 8 0 1 0 15.66 4.34 8 8 0 0 0 4.34 15.66zm9.9-8.49L11.41 10l2.83 2.83-1.41 1.41L10 11.41l-2.83 2.83-1.41-1.41L8.59 10 5.76 7.17l1.41-1.41L10 8.59l2.83-2.83 1.41 1.41z"/></svg></div>
</button>
<div class="table-of-contents">
<h4>Table of Contents</h4>
<a href="/index.html">Preface</a>
<ol>
<li><a href="/basics.html">Basics</a></li>
<li><a href="/variables.html">Variables</a></li>
<li><a href="/arithmetic.html">Arithmetic</a></li>
<li><a href="/comparisons.html">Comparisons</a></li>
<li><a href="/boolean-logic.html">Boolean Logic</a></li>
<li><a href="/conditionals.html">Conditionals</a></li>
<li><a href="/loops.html">Loops</a></li>
<li><a href="/arrays.html">Arrays</a></li>
<li><a href="/functions.html">Functions</a></li>
<li><a href="/classes.html">Classes</a></li>
</ol>
<a href="/credits.html">Credits</a>
</div>
</div>
</div>
</body>
</html>